很多时候,在编写项目时,遇见Unity 的Buge导致强制退出,这时根本来不及保存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();
}//UnityD教程手册:www.unitymanual.com
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, , );
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 == && DateTime.Now.Second == )
{
saveScene();
}
}
else
{
isStarted = false;
}
}
//UnityD教程手册:www.unitymanual.com
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之家部分内容来源于互联网,如有侵权,请联系我们,本站将立即进行处理。