U3D工程自动保存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JeanShaw/article/details/78553142

是不是经常为电脑突然断电而烦恼,是不是经常为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();
    }
}

如你所见,加入这个脚本就可以自动保存你编辑的当前场景了,时间间隔可以自己设定,代码自动保存的功能会的跟我交流一下,VS好像没有这样的插件,哈哈。

猜你喜欢

转载自blog.csdn.net/JeanShaw/article/details/78553142