游戏开发论坛

 找回密码
 立即注册
搜索
查看: 13026|回复: 0

Unity插件研究院之自动保存场景

[复制链接]

1万

主题

1万

帖子

2万

积分

管理员

中级会员

Rank: 9Rank: 9Rank: 9

积分
20468
发表于 2013-5-19 13:24:00 | 显示全部楼层 |阅读模式

  最近发现Unity老有自动崩溃的BUG。 每次崩溃的时候由于项目没有保存所以Hierarchy视图游戏对象与游戏资源的关系就会丢失。所以想到自动保存场景。

  本来想自己写一个这样的脚本,但是发现维基百科上已经有了。。。

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;

  4. public class AutoSave : EditorWindow {

  5.     private bool autoSaveScene = true;
  6.     private bool showMessage = true;
  7.     private bool isStarted = false;
  8.     private int intervalScene;
  9.     private DateTime lastSaveTimeScene = DateTime.Now;

  10.     private string projectPath = Application.dataPath;
  11.     private string scenePath;

  12.     [MenuItem ("Window/AutoSave")]
  13.     static void Init () {
  14.         AutoSave saveWindow = (AutoSave)EditorWindow.GetWindow (typeof (AutoSave));
  15.         saveWindow.Show();
  16.     }

  17.     void OnGUI () {
  18.         GUILayout.Label ("Info:", EditorStyles.boldLabel);
  19.         EditorGUILayout.LabelField ("Saving to:", ""+projectPath);
  20.         EditorGUILayout.LabelField ("Saving scene:", ""+scenePath);
  21.         GUILayout.Label ("Options:", EditorStyles.boldLabel);
  22.         autoSaveScene = EditorGUILayout.BeginToggleGroup ("Auto save", autoSaveScene);
  23.         intervalScene = EditorGUILayout.IntSlider ("Interval (minutes)", intervalScene, 1, 10);
  24.         if(isStarted) {
  25.             EditorGUILayout.LabelField ("Last save:", ""+lastSaveTimeScene);
  26.         }
  27.         EditorGUILayout.EndToggleGroup();
  28.         showMessage = EditorGUILayout.BeginToggleGroup ("Show Message", showMessage);
  29.         EditorGUILayout.EndToggleGroup ();
  30.     }

  31.     void Update(){
  32.         scenePath = EditorApplication.currentScene;
  33.         if(autoSaveScene) {
  34.             if(DateTime.Now.Minute >= (lastSaveTimeScene.Minute+intervalScene) || DateTime.Now.Minute == 59 && DateTime.Now.Second == 59){
  35.                 saveScene();
  36.             }
  37.         } else {
  38.             isStarted = false;
  39.         }

  40.     }

  41.     void saveScene() {
  42.         EditorApplication.SaveScene(scenePath);
  43.         lastSaveTimeScene = DateTime.Now;
  44.         isStarted = true;
  45.         if(showMessage){
  46.             Debug.Log("AutoSave saved: "+scenePath+" on "+lastSaveTimeScene);
  47.         }
  48.         AutoSave repaintSaveWindow = (AutoSave)EditorWindow.GetWindow (typeof (AutoSave));
  49.         repaintSaveWindow.Repaint();
  50.     }
  51. }
复制代码

因为这个编辑窗口必须在激活状态,所以 你可以把它附属在某个窗口下面 比如Project视图。


为了方便你还可以把这个布局保存起来,方便下次使用。。

   2_320_53108826434864f.png

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-26 18:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表