The question is answered, right answer was accepted
ScriptableObject created from custom editor lose data on Unity restart
Well title says it all, i am creating a custom Node Editor i am adding my custom node windows and trying to save their info to a ScriptableObject created as asset. After hour of searching i get Unity doesn't like to serialize custom classes so i stripted it out of any and still nothing happens NodeDataContainer class looks like this after the striping... [Serializable] public class NodeDataContainer : ScriptableObject {
//public List<BaseNode> windows = new List<BaseNode>();
//[SerializeField]
//public List<NodeDialogueDataContainer> dialoguesWindows = new List<NodeDialogueDataContainer>();
//[SerializeField]
//public List<NodeResponseDataContainer> responseWindows = new List<NodeResponseDataContainer>();
[SerializeField]
public string dataBaseName;
[SerializeField]
public DialogDataBase dataBaseReference;}
And here is how i am trying to Save to it from the NodeEditor class
void SaveDrawings()
{
SerializedObject so; //Second try with serializedObject instead
if (dataBase== null)
return;
NodeDataContainer nc ;
ScriptableObjectUtility.CreateAsset<NodeDataContainer>(dataBase.name+".save",out nc); //creates Asset and returns a reference of it
so = new SerializedObject(nc); //creating the SerializedObject?
nc.dataBaseName = dataBase.name;
so.FindProperty("dataBaseName").stringValue = dataBase.name;
nc.dataBaseReference = dataBase;
so.FindProperty("dataBaseReference").objectReferenceValue = dataBase;
/*for(int i = 0; i < windows.Count; i++)
{
//nc.windows.Add(windows[i]);
if (windows[i].GetType() == typeof(DialogueNode))
{
DialogueNode dn = (DialogueNode)windows[i];
nc.dialoguesWindows.Add(new NodeDialogueDataContainer(dn.id, dn.dialogueID, dn.windowRect, dn.dialogue));
}
else if (windows[i].GetType() == typeof(ResponseNode)) //obsolete
{
ResponseNode rn = (ResponseNode)windows[i];
nc.responseWindows.Add(new NodeResponseDataContainer(rn.id, rn.responseID, rn.windowRect, rn.dialogue, rn.dialogueResultID));
}
}*/
so.Update();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("Saved!");
//UpdateFlags();
}
Well lets ignore the lists for start (commended out) so I did try to use SerializedObject with FindProperty to make changes, nothing happend, if not use the SerializedObject everything seem to register properly until i restart Unity then the NodeDataContainer Asset is reseted. From SerializedObject.Update() to AssetDatabase.SaveAssets() nothing seem to work and i don't get what am i missing. Also i should note that i tried EditorUtility.SetDirty(nc) which would corrupt the Asset and make it inaccessible, also tried to add inside OnEnable of DataContainer hideFlag = HideFlags.hideanddontsave which would prevent Asset from being created. If you need more info please let me know. Any Insight on whats going wrong or what i am missing would be appreciated.
Note1: What i don't really get is why would Unity save the data of the Asset if i manipulate them from inspector while it won't through script.
Note2: Any insight on how i can save lists with the custom classes too would be great!
Note3:I know i could easily save the data directly on disc avoiding the whole thing of the Asset creation but i really want to figure this out...
Edit: here is how i create the assets if it matters
public static void CreateAsset<T>(string name, out T asset) where T : ScriptableObject
{
string path = "Assets/Editor/NodeEditorSaveFiles";
asset = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(asset, path + "/" + name + ".asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
Answer by PizzaPie · May 21, 2017 at 04:38 PM
EditorUtility.SetDirty(object.asset) did the trick, did use it but didn't seem to work not sure what changed.
Afterwards from wherever you modify your object. If you are using SerializedObjects though it is better to use the Apply$$anonymous$$odifiedProperties()
Follow this Question
Related Questions
[Solved] Custom editor resets after Play? 3 Answers
Why Does My Saved Data Not Load? 0 Answers
Custom editor, not serializing 0 Answers
How to save a two-dimensional array, as part of the variable inspector? 0 Answers
Set a List Property with EditorScripting 0 Answers