首页 >Unity3D频道 >【Unity杂文】 > Unity插件研究院之自动保存场景
2013
05-08

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

原文: http://wiki.unity3d.com/index.php?title=AutoSave 

最近发现Unity老有自动崩溃的BUG。 每次崩溃的时候由于项目没有保存所以Hierarchy视图游戏对象与游戏资源的关系就会丢失。所以想到自动保存场景。 
本来想自己写一个这样的脚本,但是发现维基百科上已经有了。。。

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

 

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

 

2_320_a155bad1a281d9f

 

 

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

 

2_320_53108826434864f

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

Unity插件研究院之自动保存场景》有 24 条评论

  1. lq说:

    运行时 大量修改场景后保存场景,然后继续编辑场景(都是在运行时)。雨松大神有办法?

  2. 林七佛说:

    如果是运行时能够保存场景就厉害了,可惜上面的代码是不支持的。不过,官方版本是要准备推出这个功能了。

  3. thw0609说:

    看到这里我笑了

  4. Info说:

    Info…

    […]that could be the finish of this write-up. Here you will locate some web pages that we consider you?ll appreciate, just click the hyperlinks over[…]…

  5. 想起以前flash pro下就有个自动保存的插件,给美术MM装过

  6. viagra说:

    One important thing…

    howdy, I?m havin…

  7. 初学说:

    请教一个可能很初级的问题如何把脚本附属到Project视图啊

  8. MOMO老师,我用了这个插件发现有个BUG不知道是不是我没设置正确的原因。反正有时候切换了场景后,它自动保存会把刚才切换之前那个场景保存到切换后的场景中。

  9. 欧宇龙说:

    还有 广告什么的能不能 放点 正能量广告啊 总是些 “肝病 大三阳小三阳” 什么的

  10. TRsiew说:

    momol老师….你有研究过在unity实现GPS定位吗? 能不能指导讲解下啊…

  11. 欧宇龙说:

    momo,试试PoolManager吧。讲解一下那个啊

  12. 欧宇龙说:

    佩服 佩服 好插件 好插件

留下一个回复

你的email不会被公开。 必填项已用 * 标注。