Skip to content

Instantly share code, notes, and snippets.

@malj
Last active November 6, 2021 05:38
Show Gist options
  • Save malj/facff007ba83dfde4807f489f59e4dc7 to your computer and use it in GitHub Desktop.
Save malj/facff007ba83dfde4807f489f59e4dc7 to your computer and use it in GitHub Desktop.
Unity runtime scriptable object
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