您的位置:首页 > 移动开发 > Unity3D

Unity场景自动保存工具

2017-06-01 14:50 435 查看

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就可以进行方便的自动保存了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 工具类