I’m trying to create nested ScriptableObjects. So far I can add a child ScriptableObject to a parent object using AssetDatabase.AddObjectToAsset which is good. However, attempting to add a sub child object to the child adds the sub child to the parent instead. This is the code I’m using:
public void CreateNestedStructure()
{
var parent = CreateInstance<ScriptableObject>();
parent.name = "Parent";
AssetDatabase.CreateAsset(parent, "Assets/Parent.asset");
var child = CreateInstance<ScriptableObject>();
child.name = "Child";
AssetDatabase.AddObjectToAsset(child, parent);
var subChild = CreateInstance<ScriptableObject>();
subChild.name = "Sub Child";
AssetDatabase.AddObjectToAsset(subChild, child);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
This is the result
I wish to have the Sub Child reside inside Child, to make the file structure more organized. I assume the problem is that Child is an object of the Parent asset and not an asset itself. Is it possible to achieve this structure somehow?
Assets do not support nesting. An asset contains a number of objects, one of which is the “main” asset that is shown as the parent in the editor. But there is no parent-child relationship that would let you create a hierarchy, the objects are always just a flat list.
You could make a custom editor for the main object, that then shows a hierarchy of the objects contained in the asset, but I don’t think it’s possible in the assets window itself.
Ahh ok, that’s what I thought! Thanks for the clarification though. I am actually working on a custom editor for a node-based system, where the parent is the graph, the child is a node and the sub-child is a node attribute. I just can’t figure out a good way to organize the objects. I guess the solution is to place the objects in folders to achieve the hierarchical structure. The nested object approach would just be cleaner imo, had it been possible