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

unity 场景自动保存

2014-01-31 19:10 495 查看
最近发现Unity老有自动崩溃的BUG。每次崩溃的时候由于项目没有保存所以Hierarchy视图游戏对象与游戏资源的关系就会丢失。所以想到自动保存场景。

本来想自己写一个这样的脚本,但是发现维基百科上已经有了。。。

usingUnityEngine;


usingUnityEditor;


usingSystem;




publicclassAutoSave:EditorWindow{




privateboolautoSaveScene=
true
;


privateboolshowMessage=
true
;


privateboolisStarted=
false
;


privateintintervalScene;


privateDateTimelastSaveTimeScene=DateTime.Now;




privatestringprojectPath=Application.dataPath;


privatestringscenePath;




[MenuItem(
"Window/AutoSave"
)]


staticvoidInit(){


AutoSavesaveWindow=(AutoSave)EditorWindow.GetWindow(
typeof
(AutoSave));


saveWindow.Show();


}




voidOnGUI(){


GUILayout.Label(
"Info:"
,
EditorStyles.boldLabel);


EditorGUILayout.LabelField(
"Savingto:"
,
""
+projectPath);


EditorGUILayout.LabelField(
"Savingscene:"
,
""
+scenePath);


GUILayout.Label(
"Options:"
,
EditorStyles.boldLabel);


autoSaveScene=EditorGUILayout.BeginToggleGroup(
"Auto
save"
,autoSaveScene);


intervalScene=EditorGUILayout.IntSlider(
"Interval
(minutes)"
,intervalScene,1,10);


if
(isStarted){


EditorGUILayout.LabelField(
"Lastsave:"
,
""
+lastSaveTimeScene);


}


EditorGUILayout.EndToggleGroup();


showMessage=EditorGUILayout.BeginToggleGroup(
"Show
Message"
,showMessage);


EditorGUILayout.EndToggleGroup();


}






voidUpdate(){


scenePath=EditorApplication.currentScene;


if
(autoSaveScene){


if
(DateTime.Now.Minute>=(lastSaveTimeScene.Minute+intervalScene)
||DateTime.Now.Minute==59&&DateTime.Now.Second==59){


saveScene();


}


}
else
{


isStarted=
false
;


}




}




voidsaveScene(){


EditorApplication.SaveScene(scenePath);


lastSaveTimeScene=DateTime.Now;


isStarted=
true
;


if
(showMessage){


Debug.Log(
"AutoSavesaved:"
+scenePath+
"
on"
+lastSaveTimeScene);


}


AutoSaverepaintSaveWindow=(AutoSave)EditorWindow.GetWindow(
typeof
(AutoSave));


repaintSaveWindow.Repaint();


}


}


因为这个编辑窗口必须在激活状态,所以你可以把它附属在某个窗口下面比如Project视图。





为了方便你还可以把这个布局保存起来,方便下次使用。。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: