Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active June 20, 2017 06:57
Show Gist options
  • Save tsubaki/6bacf17a930e686722a9cecdc4900344 to your computer and use it in GitHub Desktop.
Save tsubaki/6bacf17a930e686722a9cecdc4900344 to your computer and use it in GitHub Desktop.
ゲーム再生終了時にパラメータをリセットする(ゲーム開始時の値に戻す)ScriptableObject
using UnityEngine;
public class ResettableScriptableObject : ScriptableObject
{
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void CallBeforPlayScene()
{
// recently, unity editor will crash
// recommend SaveProject before playing
// UnityEditor.AssetDatabase.SaveAssets ();
}
#endif
protected virtual void OnEnable()
{
#if UNITY_EDITOR
if( UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode == true ){
UnityEditor.EditorApplication.playmodeStateChanged += () => {
if( UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode == false) {
Resources.UnloadAsset(this);
}
};
}
#endif
}
}
[CreateAssetMenu]
public class sample : ResettableScriptableObject
{
[SerializeField] int count;
public void Add()
{
count++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment