1、Unity自动保存项目
Posted on 2013年06月23日 by U3d / Unity3D 基础教程/被围观 34 次
很多时候,在编写项目时,遇见Unity 的Buge导致强制退出,这时根本来不及保存hierarchy视图中的东西,这是一件很糟糕的事情,所以这篇自动保存项目场景的源码就帮你解决了这一问题。
01
using UnityEngine;
02
using UnityEditor;
03
using System;
04
05
public class AutoSave : EditorWindow
06
2、 {
07
08
private bool autoSaveScene = true;
09
private bool showMessage = true;
10
private bool isStarted = false;
11
private int intervalScene;
12
private DateTime lastSaveTimeScene = DateTime.Now;
13
14
private string projectPath = Application.d
3、ataPath;
15
private string scenePath;
16
17
[MenuItem("Window/AutoSave")]
18
static void Init()
19
{
20
AutoSave saveWindow = (AutoSave)EditorWindow.GetWindow(typeof(AutoSave));
21
saveWindow.Show();
22
}//UnityD教程手册:
23
24
25
4、 void OnGUI()
26
{
27
GUILayout.Label("Info:", EditorStyles.boldLabel);
28
EditorGUILayout.LabelField("Saving to:", "" + projectPath);
29
EditorGUILayout.LabelField("Saving scene:", "" + scenePath);
30
GUILayout.Label("Options:", EditorStyles.bold
5、Label);
31
autoSaveScene = EditorGUILayout.BeginToggleGroup("Auto save", autoSaveScene);
32
intervalScene = EditorGUILayout.IntSlider("Interval (minutes)", intervalScene, , );
33
if (isStarted)
34
{
35
EditorGUILayout.LabelField("Last save:"
6、 "" + lastSaveTimeScene);
36
}
37
EditorGUILayout.EndToggleGroup();
38
showMessage = EditorGUILayout.BeginToggleGroup("Show Message", showMessage);
39
EditorGUILayout.EndToggleGroup();
40
}
41
42
43
void Update()
44
{
45
7、 scenePath = EditorApplication.currentScene;
46
if (autoSaveScene)
47
{
48
if (DateTime.Now.Minute >= (lastSaveTimeScene.Minute + intervalScene) || DateTime.Now.Minute == && DateTime.Now.Second == )
49
{
50
saveScene();
51
8、
}
52
}
53
else
54
{
55
isStarted = false;
56
}
57
58
}
59
//UnityD教程手册:
60
void saveScene()
61
{
62
EditorApplication.SaveScene(scenePath);
63
lastSaveTimeScene = DateTime.Now;
9、
64
isStarted = true;
65
if (showMessage)
66
{
67
Debug.Log("AutoSave saved: " + scenePath + " on " + lastSaveTimeScene);
68
}
69
AutoSave repaintSaveWindow = (AutoSave)EditorWindow.GetWindow(typeof(AutoSave));
70
repaintSaveWindow.Repaint();
71
}
}