I have a custom asset post processor that is working except for one problem: the asset has an extra empty root object which I’d like to remove. How can I do this? I tried just linking myself to it’s parent and then deleting it (see below) but because the root parent is null and the child is now null it just deletes everything and leaves behind an empty game object.
Is the only way to do this to copy all the components from the child object and reconstruct the child then delete the child?
E.g. is this the right way? (It works but want to be sure it’s the best way)
void OnPostprocessMeshHierarchy(GameObject go)
{
Debug.Log("OnPostprocessMeshHierarchy: " + assetPath);
// bail if not our asset
if ((shouldPostProcess == false) || (obj == null))
{
return;
}
// I should have one child, make it the root
if (go.transform.childCount == 1)
{
// copy all the components of the child to the parent
GameObject childGo = go.transform.GetChild(0).gameObject;
foreach (Component component in childGo.GetComponents<Component>())
{
if (component is Transform)
{
continue;
}
Component newComponent = go.AddComponent(component.GetType());
EditorUtility.CopySerialized(component, newComponent);
}
// destroy my child
GameObject.DestroyImmediate(childGo);
}
}