原文: http://wiki.unity3d.com/index.php?title=AutoSave
最近发现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();
- }
- }
锐亚教育,游戏开发论坛|游戏制作人|游戏策划|游戏开发|独立游戏|游戏产业|游戏研发|游戏运营| unity|unity3d|unity3d官网|unity3d 教程|金融帝国3|8k8k8k|mcafee8.5i|游戏蛮牛|蛮牛 unity|蛮牛
- 还没有人评论,欢迎说说您的想法!