Skip to content

Instantly share code, notes, and snippets.

@ashblue
Last active January 13, 2016 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ashblue/720e56fac2371e6db3d6 to your computer and use it in GitHub Desktop.
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.
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