| | | 1 | | #if UNITY_EDITOR |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using UnityEditor; |
| | | 5 | | using UnityEngine; |
| | | 6 | | |
| | | 7 | | namespace Hertzole.ScriptableValues |
| | | 8 | | { |
| | | 9 | | internal static class ScriptableValuesInitialization |
| | | 10 | | { |
| | 0 | 11 | | private static readonly List<IScriptableValueCallbacks> objectsToEnable = new List<IScriptableValueCallbacks>(); |
| | 0 | 12 | | private static readonly List<IScriptableValueCallbacks> objectsToDisable = new List<IScriptableValueCallbacks>() |
| | | 13 | | |
| | | 14 | | [InitializeOnLoadMethod] |
| | | 15 | | private static void InitInEditor() |
| | 0 | 16 | | { |
| | 0 | 17 | | EditorApplication.playModeStateChanged += OnPlayModeStateChanged; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | private static void OnPlayModeStateChanged(PlayModeStateChange state) |
| | 144 | 21 | | { |
| | | 22 | | // When exiting play mode, we want to call OnScriptableObjectPreDisable and OnScriptableObjectDisable on all |
| | 144 | 23 | | if (state == PlayModeStateChange.EnteredEditMode) |
| | 36 | 24 | | { |
| | 240 | 25 | | for (int i = 0; i < objectsToDisable.Count; i++) |
| | 84 | 26 | | { |
| | 84 | 27 | | objectsToDisable[i].OnScriptableObjectPreDisable(); |
| | 84 | 28 | | } |
| | | 29 | | |
| | 240 | 30 | | for (int i = 0; i < objectsToDisable.Count; i++) |
| | 84 | 31 | | { |
| | 84 | 32 | | objectsToDisable[i].OnScriptableObjectDisable(); |
| | 84 | 33 | | } |
| | | 34 | | |
| | 36 | 35 | | objectsToDisable.Clear(); |
| | 36 | 36 | | } |
| | 144 | 37 | | } |
| | | 38 | | |
| | | 39 | | [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] |
| | | 40 | | private static void Init() |
| | 36 | 41 | | { |
| | | 42 | | // When the game starts, call OnScriptableObjectPreEnable and OnScriptableObjectEnable on all registered obj |
| | 252 | 43 | | for (int i = 0; i < objectsToEnable.Count; i++) |
| | 90 | 44 | | { |
| | 90 | 45 | | objectsToEnable[i].OnScriptableObjectPreEnable(); |
| | 90 | 46 | | } |
| | | 47 | | |
| | 252 | 48 | | for (int i = 0; i < objectsToEnable.Count; i++) |
| | 90 | 49 | | { |
| | 90 | 50 | | objectsToEnable[i].OnScriptableObjectEnable(); |
| | 90 | 51 | | } |
| | | 52 | | |
| | 36 | 53 | | objectsToEnable.Clear(); |
| | 36 | 54 | | } |
| | | 55 | | |
| | | 56 | | public static void RegisterForEnable(IScriptableValueCallbacks obj) |
| | 90 | 57 | | { |
| | | 58 | | // This is a bootstrapper, we can't register for enable if the application is already playing. The object sh |
| | 90 | 59 | | if (Application.isPlaying) |
| | 0 | 60 | | { |
| | 0 | 61 | | throw new InvalidOperationException("Cannot register for enable when the application is already playing. |
| | | 62 | | } |
| | | 63 | | |
| | 90 | 64 | | if (!objectsToEnable.Contains(obj)) |
| | 90 | 65 | | { |
| | 90 | 66 | | objectsToEnable.Add(obj); |
| | 90 | 67 | | } |
| | 90 | 68 | | } |
| | | 69 | | |
| | | 70 | | public static void RegisterForDisable(IScriptableValueCallbacks obj) |
| | 84 | 71 | | { |
| | 84 | 72 | | if (!Application.isPlaying) |
| | 0 | 73 | | { |
| | 0 | 74 | | throw new InvalidOperationException("Cannot register for disable when the application is not playing."); |
| | | 75 | | } |
| | | 76 | | |
| | 84 | 77 | | if (!objectsToDisable.Contains(obj)) |
| | 84 | 78 | | { |
| | 84 | 79 | | objectsToDisable.Add(obj); |
| | 84 | 80 | | } |
| | 84 | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | #endif |