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

Unity自动场景保存脚本

2016-07-05 21:40 405 查看
新建一个名为AutoSave的编辑器脚本,并放于Assets/Editor下。

using System;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AutoSave : EditorWindow
{
private bool _autoSaveScene;
private bool _showMessage;
private bool _isStarted;
private int _intervalScene;
private DateTime _lastSaveTimeScene = DateTime.Now;

private readonly string _projectPath = Application.dataPath;
private string _scenePath;

[MenuItem("Window/AutoSave")]
private static void Init()
{
AutoSave saveWindow = (AutoSave) GetWindow(typeof (AutoSave));
saveWindow.Show();
}

private 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();
}

private void Update()
{
_scenePath = SceneManager.GetActiveScene().path;
if (_autoSaveScene)
{
if (DateTime.Now.Minute >= (_lastSaveTimeScene.Minute + _intervalScene) ||
DateTime.Now.Minute == 59 && DateTime.Now.Second == 59)
{
SaveScene();
}
}
else
{
_isStarted = false;
}

}

private void SaveScene()
{
EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
_lastSaveTimeScene = DateTime.Now;
_isStarted = true;
if (_showMessage)
{
Debug.Log("AutoSave saved: " + _scenePath + " on " + _lastSaveTimeScene);
}
AutoSave repaintSaveWindow = (AutoSave) GetWindow(typeof (AutoSave));
repaintSaveWindow.Repaint();
}
}


在Window/AutoSave可以打开该面板,该脚本将自动识别项目路径并定时保存场景。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: