Unity自动场景保存脚本

Posted 太息花色

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity自动场景保存脚本相关的知识,希望对你有一定的参考价值。

新建一个名为AutoSave的编辑器脚本,并放于Assets/Editor下。

 1 using System;
 2 using UnityEditor;
 3 using UnityEditor.SceneManagement;
 4 using UnityEngine;
 5 using UnityEngine.SceneManagement;
 6 
 7 public class AutoSave : EditorWindow
 8 {
 9     private bool _autoSaveScene;
10     private bool _showMessage;
11     private bool _isStarted;
12     private int _intervalScene;
13     private DateTime _lastSaveTimeScene = DateTime.Now;
14 
15     private readonly string _projectPath = Application.dataPath;
16     private string _scenePath;
17 
18     [MenuItem("Window/AutoSave")]
19     private static void Init()
20     {
21         AutoSave saveWindow = (AutoSave) GetWindow(typeof (AutoSave));
22         saveWindow.Show();
23     }
24 
25     private void OnGUI()
26     {
27         GUILayout.Label("Info:", EditorStyles.boldLabel);
28         EditorGUILayout.LabelField("Saving to:", "" + _projectPath);
29         EditorGUILayout.LabelField("Saving scene:", "" + _scenePath);
30         GUILayout.Label("Options:", EditorStyles.boldLabel);
31         _autoSaveScene = EditorGUILayout.BeginToggleGroup("Auto save", _autoSaveScene);
32         _intervalScene = EditorGUILayout.IntSlider("Interval (minutes)", _intervalScene, 1, 10);
33         if (_isStarted)
34         {
35             EditorGUILayout.LabelField("Last save:", "" + _lastSaveTimeScene);
36         }
37         EditorGUILayout.EndToggleGroup();
38         _showMessage = EditorGUILayout.BeginToggleGroup("Show Message", _showMessage);
39         EditorGUILayout.EndToggleGroup();
40     }
41 
42     private void Update()
43     {
44         _scenePath = SceneManager.GetActiveScene().path;
45         if (_autoSaveScene)
46         {
47             if (DateTime.Now.Minute >= (_lastSaveTimeScene.Minute + _intervalScene) ||
48                 DateTime.Now.Minute == 59 && DateTime.Now.Second == 59)
49             {
50                 SaveScene();
51             }
52         }
53         else
54         {
55             _isStarted = false;
56         }
57 
58     }
59 
60     private void SaveScene()
61     {
62         EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
63         _lastSaveTimeScene = DateTime.Now;
64         _isStarted = true;
65         if (_showMessage)
66         {
67             Debug.Log("AutoSave saved: " + _scenePath + " on " + _lastSaveTimeScene);
68         }
69         AutoSave repaintSaveWindow = (AutoSave) GetWindow(typeof (AutoSave));
70         repaintSaveWindow.Repaint();
71     }
72 }

在Window/AutoSave可以打开该面板,该脚本将自动识别项目路径并定时保存场景。

以上是关于Unity自动场景保存脚本的主要内容,如果未能解决你的问题,请参考以下文章

unity工程自动保存的脚本

Unity 保存场景物体修改到Prefab

Unity3DUnity 脚本 ⑤ ( 编辑模式与播放模式 )

Unity简单的场景跳转管理

如何在Unity3D使用 WWW 加载场景并显示进度条

unity中怎样让一个对象切换脚本