- Home /
Question by
fer-geraci · Mar 02, 2018 at 02:27 AM ·
assetsserializationscriptableobjectpersistence
Created Assets not persisting after closing/opening editor
I created an extension to create behavior trees.
Everything works find while in the editor/gameplay, the main issue that even after creating the asset and persisting the scriptable object to disk, once I re open the editor, it wont be loaded.
I put a quick video to demonstrate the behavior here.
The code, to create such an asset is the following:
private NPCNode CreateTree(Node parent, string path) {
NPCNode n = (NPCNode) MakeNode(parent);
if(parent.Parent == null) {
AssetDatabase.CreateAsset(n, path);
}
foreach(Node c in parent.Children) {
NPCNode node = CreateTree(c, path);
if (!n.AddChild(node))
Debug.Log("Failed adding child " + c + " to parent " + parent);
else {
AssetDatabase.AddObjectToAsset(node, path);
Undo.RegisterCreatedObjectUndo(node, node.name);
}
}
Undo.RegisterCreatedObjectUndo(n, n.name);
return n;
}
private NPCNode MakeNode(Node n) {
NPCNode node = null;
object[] pms = new object[] { null };
if (n.isLeaf) {
List<object> parameters = new List<object>();
foreach(ParameterInfo p in n.NodeType.GetConstructors()[0].GetParameters()) {
parameters.Add(n.Parameters[p.Name]);
}
pms = parameters.ToArray();
}
node = NPCNode.CreateInstance(NodeTypes[n.NodeTypeName]);
node.Initialize(pms);
return node;
}
Finally, after creating the tree, the following code executes:
g_LastBakedTree = CreateTree(root,path);
AssetDatabase.Refresh();
EditorUtility.SetDirty(g_LastBakedTree);
AssetDatabase.SaveAssets();
Any help will be greatly appreciated it.
Comment
Best Answer
Answer by fer-geraci · Mar 02, 2018 at 07:09 PM
I found what was happening. Unity doesn't like classes in same file:
BTEditor.cs:
[Serializable]
class NodeList : ScriptableObject {
...
}
[Serializable]
class Node : ScriptableObject {
...
}
So I created separate files for each, and BAM! everything started working fine.
Every object is persisted now on close/open load/unload etc...