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*);*
if (windows*.GetType() == typeof(DialogueNode))*
{
DialogueNode dn = (DialogueNode)windows*;*
nc.dialoguesWindows.Add(new NodeDialogueDataContainer(dn.id, dn.dialogueID, dn.windowRect, dn.dialogue));
}
else if (windows*.GetType() == typeof(ResponseNode)) //obsolete*
{
ResponseNode rn = (ResponseNode)windows*;*
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(string name, out T asset) where T : ScriptableObject
{
string path = “Assets/Editor/NodeEditorSaveFiles”;
asset = ScriptableObject.CreateInstance();
AssetDatabase.CreateAsset(asset, path + “/” + name + “.asset”);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}