Last active
January 13, 2016 14:53
-
-
Save ashblue/720e56fac2371e6db3d6 to your computer and use it in GitHub Desktop.
Save and restore a scriptable object reference after your custom editor windows have been re-compiled.
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; | |
using UnityEditor; | |
public static class Wm { | |
const string DATABASE_ID_KEY = "MyCustomDatabase"; | |
public static MyScriptableObject _db; | |
public static MyScriptableObject Db { | |
get { | |
if (_db == null && EditorPrefs.HasKey(DATABASE_ID_KEY)) { | |
_db = GetDatabaseFromStorage(); | |
} | |
return _db; | |
} | |
set { | |
_db = value; | |
if (_db != null) { | |
EditorPrefs.SetFloat(DATABASE_ID_KEY, _db.GetInstanceID()); | |
} | |
} | |
} | |
static MyScriptableObject GetDatabaseFromStorage () { | |
int instanceId = (int)EditorPrefs.GetFloat(DATABASE_ID_KEY); | |
Object obj = EditorUtility.InstanceIDToObject(instanceId); | |
if (obj is MyScriptableObject) { | |
return (MyScriptableObject)obj; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment