When editing a prefab, I want to remove some game objects from the prefab just before it’s saved.
My scenario is that I’m using a Playable graph to preview sitting animations for seats. When previewing, a default avatar is spawned and I want to remove the avatar after I have done my edits so that it doesn’t get included in the final prefab.
Here’s is an example of what I’ve tried but maybe there’s a better solution:
#if UNITY_EDITOR
public class RemoveChildrenOfSeatOnSave : AssetModificationProcessor
{
static string OnWillSaveAssets(string paths)
{
foreach (string path in paths)
{
Debug.Log(path);
var furnitureItem = AssetDatabase.LoadAssetAtPath(path);
if (furnitureItem == null)
{
Debug.Log(“Furniture Item is null”);
continue;
}
furnitureItem.seat.isTestingSitting = false;
if (furnitureItem.seat.sittingTransform.childCount > 0)
{
furnitureItem.seat.sittingTransform.ClearChildrenImmediate();
}
EditorUtility.SetDirty(furnitureItem.gameObject);
}
return paths;
}
}
#endif