Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save INeatFreak/2f90e5a848788870957fb817f464e712 to your computer and use it in GitHub Desktop.
Save INeatFreak/2f90e5a848788870957fb817f464e712 to your computer and use it in GitHub Desktop.
Get callbacks in editor play mode changes to initialize ScriptableObjects.
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