Created
July 12, 2022 11:22
-
-
Save INeatFreak/2f90e5a848788870957fb817f464e712 to your computer and use it in GitHub Desktop.
Get callbacks in editor play mode changes to initialize ScriptableObjects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[CreateAssetMenu(fileName = "ExampleObject", menuName = "Examples/ExampleObject", order = 0)] | |
public class ExampleObject : ScriptableObject | |
{ | |
#if UNITY_EDITOR | |
void OnEnable() { | |
EditorApplication.playModeStateChanged += OnPlayStateChange; | |
} | |
void OnDisable() { | |
EditorApplication.playModeStateChanged -= OnPlayStateChange; | |
} | |
void OnPlayStateChange(PlayModeStateChange state) | |
{ | |
switch (state) | |
{ | |
case PlayModeStateChange.EnteredPlayMode: | |
OnGameStart(); | |
break; | |
case PlayModeStateChange.ExitingPlayMode: | |
OnGameStop(); | |
break; | |
} | |
} | |
#else | |
void OnEnable() { | |
OnGameStart(); | |
} | |
void OnDisable() { | |
OnGameStop(); | |
} | |
#endif | |
void OnGameStart() { | |
Debug.Log("Game Started"); | |
} | |
void OnGameStop() { | |
Debug.Log("Game Stopped"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment