Last active
November 6, 2021 05:38
-
-
Save malj/facff007ba83dfde4807f489f59e4dc7 to your computer and use it in GitHub Desktop.
Unity runtime scriptable object
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; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class RuntimeScriptableObject : ScriptableObject | |
{ | |
#if UNITY_EDITOR | |
string serializedEditorState; | |
#endif | |
protected virtual void OnEnable() | |
{ | |
#if UNITY_EDITOR | |
EditorApplication.playModeStateChanged += OnPlayModeStateChanged; | |
#endif | |
} | |
protected virtual void OnDisable() | |
{ | |
#if UNITY_EDITOR | |
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged; | |
#endif | |
} | |
#if UNITY_EDITOR | |
void OnPlayModeStateChanged(PlayModeStateChange mode) | |
{ | |
switch (mode) | |
{ | |
case PlayModeStateChange.ExitingEditMode: | |
serializedEditorState = JsonUtility.ToJson(this); | |
break; | |
case PlayModeStateChange.ExitingPlayMode: | |
JsonUtility.FromJsonOverwrite(serializedEditorState, this); | |
break; | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment