unity插件--自动保存场景

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

自动保存场景:防止因项目没有保存引起Hierarchy视图游戏对象与游戏资源的关系丢失。 

插件存放路径:

unity插件--自动保存场景_第1张图片

代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

using System.Collections;
using System.Collections.Generic;
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;
    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(){
        if(projectPath==null){
            projectPath = Application.dataPath;
        }
        scenePath = EditorApplication.currentScene;

        if (EditorApplication.isPlaying) {
                return;    
            if ((DateTime.Now.Minute >= (lastSaveTimeScene.Minute+intervalScene) || DateTime.Now.Minute == 59) && DateTime.Now.Second == 59) {
                autoSaveScene = true;
            }

        }
        
        if(autoSaveScene) {
                saveScene();
        } else {
            isStarted = false;
        }

    }

    void saveScene() {
        autoSaveScene = false;
        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();
    }
}
 

   

你可能感兴趣的:(Unity,unity,autoSave,scenes,MenuItem)