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

Unity之自动保存场景

2015-07-24 17:56 561 查看
今天把http://www.xuanyusong.com/的网站又粗略浏览看有没有需要的,然后发现了“Unity插件研究院之自动保存场景”

unity老是闪退让我这种老忘保存的人情何以堪,也是吃不少亏。

using UnityEngine;
using System.Collections;
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();
}
}


贴上此代码,放到\Assets\Editor下

然后在unity界面的Window下寻找到AutoSave就可以啦

然后他就会隔了时间给你保存下还提示下(当然啦,提示也可以选择性的啦)

还有在unity界面LayerOut上还可以Save Layerout,呀呀呀,保存了

不过如果在程序运行的时候,我对程序中的模型做了改动的话,停止程序我改动的不就没有作用啦。。。。得想想怎么做,可以如果我在程序执行时对场景的模型啥的做了改动可以提示是否保存就好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: