U3DC.COM | 优三帝研究院

Menu

自动保存场景

img024

http://wiki.unity3d.com/index.php?title=AutoSave

自动保存当前scenes,预防因为unity意外崩溃导致的丢失。

using UnityEngine;
using UnityEditor;
using System;
 
public class AutoSave : EditorWindow {
 
	private bool autoSaveScene = true;
	private bool showMessage = true;
	private bool isStarted = false;
	private int intervalScene;	
	private DateTime lastSaveTimeScene = DateTime.Now;
 
	private string projectPath = Application.dataPath;
	private string scenePath;
 
	[MenuItem ("Window/AutoSave")]
	static void Init () {
		AutoSave saveWindow = (AutoSave)EditorWindow.GetWindow (typeof (AutoSave));
		saveWindow.Show();
	}
 
	void OnGUI () {	
		GUILayout.Label ("Info:", EditorStyles.boldLabel);
		EditorGUILayout.LabelField ("Saving to:", ""+projectPath);
		EditorGUILayout.LabelField ("Saving scene:", ""+scenePath);
		GUILayout.Label ("Options:", EditorStyles.boldLabel);
		autoSaveScene = EditorGUILayout.BeginToggleGroup ("Auto save", autoSaveScene);
		intervalScene = EditorGUILayout.IntSlider ("Interval (minutes)", intervalScene, 1, 10);
		if(isStarted) {
			EditorGUILayout.LabelField ("Last save:", ""+lastSaveTimeScene);
		}
		EditorGUILayout.EndToggleGroup();
		showMessage = EditorGUILayout.BeginToggleGroup ("Show Message", showMessage);
		EditorGUILayout.EndToggleGroup ();
	}
 
 
	void Update(){
		scenePath = EditorApplication.currentScene;
		if(autoSaveScene) {
			if(DateTime.Now.Minute >= (lastSaveTimeScene.Minute+intervalScene) || DateTime.Now.Minute == 59 && DateTime.Now.Second == 59){
				saveScene();
			}
		} else {
			isStarted = false;
		}
 
	}
 
	void saveScene() {
		EditorApplication.SaveScene(scenePath);
		lastSaveTimeScene = DateTime.Now;
		isStarted = true;
		if(showMessage){
			Debug.Log("AutoSave saved: "+scenePath+" on "+lastSaveTimeScene);
		}
		AutoSave repaintSaveWindow = (AutoSave)EditorWindow.GetWindow (typeof (AutoSave));
		repaintSaveWindow.Repaint();
	}
}
打赏
— 于 共写了1628个字
— 文内使用到的标签:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据