unity工程自动保存的脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity工程自动保存的脚本相关的知识,希望对你有一定的参考价值。
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();
}
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();
}
}
注:1.脚本如上,保存在assets中就完事了。 要想设置自动保存的时间间隔,选项在Windows下拉菜单下的AutoSave 中
2.现在所知,每一个工程都得安装一次脚本。(有点尴尬)
以上是关于unity工程自动保存的脚本的主要内容,如果未能解决你的问题,请参考以下文章
Unity3D中,如何把图像文件保存为数组并通过输入键盘信息调用?
unity2020 mac 禁止Visual Studio保存代码自动编译
unity2020 mac 禁止Visual Studio保存代码自动编译