Posts 유니티 - 플레이모드 상태 변경에 따른 동작 구현하기
Post
Cancel

유니티 - 플레이모드 상태 변경에 따른 동작 구현하기

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
#if UNITY_EDITOR

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

namespace Rito
{
    [InitializeOnLoad]
    public class PlayModeStateChangeHandler
    {
        public static event Action OnExitEditMode;
        public static event Action OnEnterPlayMode;
        public static event Action OnExitPlayMode;
        public static event Action OnEnterEditMode;

        static PlayModeStateChangeHandler()
        {
            EditorApplication.playModeStateChanged += OnPlaymodeStateChanged;
        }
        private static void OnPlaymodeStateChanged(PlayModeStateChange stateChange)
        {
            switch (stateChange)
            {
                // EditMode -> PlayMode 1
                case PlayModeStateChange.ExitingEditMode:
                    OnExitEditMode?.Invoke();
                    break;

                // EditMode -> PlayMode 2
                // 모든 Start()가 실행되고, 실제로 2번째 Update() 이후에 실행
                case PlayModeStateChange.EnteredPlayMode:
                    OnEnterPlayMode?.Invoke();
                    break;

                // PlayMode -> EditMode 1
                case PlayModeStateChange.ExitingPlayMode:
                    OnExitPlayMode?.Invoke();
                    break;

                // PlayMode -> EditMode 2
                case PlayModeStateChange.EnteredEditMode:
                    OnEnterEditMode?.Invoke();
                    break;
            }
        }
    }
}

#endif
This post is licensed under CC BY 4.0 by the author.