Unity 運行之前自動保存的方法。

本文固定鏈接,轉發請先評論點贊

一、起因:

沒有起因,只是想寫點東西。

二、實現方式:

1、使用的Unity版本如圖:
在這裏插入圖片描述

2、代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

[InitializeOnLoad]
public class AutoSave 
{
    static AutoSave()
    {
        EditorApplication.playModeStateChanged += SaveOnPlay;
    }

    private static void SaveOnPlay(PlayModeStateChange state)
    {
        if(state==PlayModeStateChange.ExitingEditMode)
        {
            Debug.Log("Auto-saving ...");

            EditorSceneManager.SaveOpenScenes();

            AssetDatabase.SaveAssets();
        }
    }
}

3、稍作解釋:
a、InitializeOnLoad 意思就是說當你的Unity初始化和重編譯的時候順帶也初始化這個編輯器類。
b、一個靜態方法,監聽當play 模式發生改變的時候執行某個方法,此處執行的是SaveOnPlay方法。
c、SaveOnPlay方法中就是監聽狀態發生改變,改變成退出Edit模式的時候(不是退出Unity)執行場景保存和保存AssetData。

三、附加:

順帶看一下這個枚舉:

namespace UnityEditor
{
    //
    // 摘要:
    //     Enumeration specifying a change in the Editor's play mode state. See Also: PauseState,
    //     EditorApplication.playModeStateChanged, EditorApplication.isPlaying.
    public enum PlayModeStateChange
    {
        //
        // 摘要:
        //     Occurs during the next update of the Editor application if it is in edit mode
        //     and was previously in play mode.
        EnteredEditMode = 0,
        //
        // 摘要:
        //     Occurs when exiting edit mode, before the Editor is in play mode.
        ExitingEditMode = 1,
        //
        // 摘要:
        //     Occurs during the next update of the Editor application if it is in play mode
        //     and was previously in edit mode.
        EnteredPlayMode = 2,
        //
        // 摘要:
        //     Occurs when exiting play mode, before the Editor is in edit mode.
        ExitingPlayMode = 3
    }
}

說白了就是進入Editor模式、退出Editor模式、進入Play模式和退出Play模式。至於他們的順序,我們可以試驗驗證下,代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

[InitializeOnLoad]
public class AutoSave 
{
    static AutoSave()
    {
        EditorApplication.playModeStateChanged += SaveOnPlay;
    }

    private static void SaveOnPlay(PlayModeStateChange state)
    {
        if(state==PlayModeStateChange.ExitingEditMode)
        {
            Debug.Log("ExitingEditMode");

            Debug.Log("Auto-saving ...");

            EditorSceneManager.SaveOpenScenes();

            AssetDatabase.SaveAssets();
        }

        if (state == PlayModeStateChange.EnteredEditMode)
        {
            Debug.Log("EnteredEditMode");
        }

        if (state == PlayModeStateChange.EnteredPlayMode)
        {
            Debug.Log("EnteredPlayMode");
        }

        if (state == PlayModeStateChange.ExitingPlayMode)
        {
            Debug.Log("ExitingPlayMode");
        }
    }
}

結果如下,是不是跟我們預想的一樣。(試驗過程就是從Editor模式運行一下,然後關閉運行狀態)並沒有什麼特別的事情。
在這裏插入圖片描述

四、關於UnityTips

最近如果閒,可能會出一系列這種。有興趣的可以持續關注一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章