当前位置:   article > 正文

Unity 运行之前自动保存的方法。_unity saveon playmode

unity saveon playmode

本文固定链接,转发请先评论点赞

一、起因:

没有起因,只是想写点东西。

二、实现方式:

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

  • 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

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
    }
}
  • 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

说白了就是进入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");
        }
    }
}

  • 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

结果如下,是不是跟我们预想的一样。(试验过程就是从Editor模式运行一下,然后关闭运行状态)并没有什么特别的事情。
在这里插入图片描述

四、关于UnityTips

最近如果闲,可能会出一系列这种。有兴趣的可以持续关注一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/141567
推荐阅读
相关标签
  

闽ICP备14008679号