如何让Unity自动保存场景?
我们在Unity开发过程中会发现总是会有自动崩溃的bug,而且每次崩溃可能因为没有保存项目,所以Hierarchy视图游戏对象与相关游戏资源会丢失,这个时候你就需要通过自动保存来保证你的项目进度不会丢失了。如何在Untiy中实现自动保存场景呢,我们来看一下教程。
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")]
unity 教程 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){
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论