1. 程式人生 > >unity 場景自動儲存

unity 場景自動儲存

最近發現Unity老有自動崩潰的BUG。 每次崩潰的時候由於專案沒有儲存所以Hierarchy檢視遊戲物件與遊戲資源的關係就會丟失。所以想到自動儲存場景。

本來想自己寫一個這樣的指令碼,但是發現維基百科上已經有了。。。

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(); } }

因為這個編輯視窗必須在啟用狀態,所以 你可以把它附屬在某個視窗下面 比如Project檢視。

2_320_a155bad1a281d9f

為了方便你還可以把這個佈局儲存起來,方便下次使用。。