Unity场景自动保存工具

来源:互联网 发布:马鞍山电视网络大学 编辑:程序博客网 时间:2024/06/06 07:46

Unity场景自动保存工具

难得闲下来,好好整理一下,今天主要分享一下在Unity中,开启场景的自动保存功能,是从网上买的源码中找到的,还是挺有用的,这样就算突然断电或者忘记保存了,不会导致太严重的后果,因为可以设置1-10分钟自动保存一次。

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工程中的Editor文件夹中,然后点击window下的AutoSave就可以进行方便的自动保存了。

原创粉丝点击