Hi.
We have a game where we are using nested prefabs in a specific way.
The game is an “endless runner” type of game, and we are creating some zones that the level consists of and within these zones, we have patterns which contain our obstacles for the player etc.
To iterate effectively, we like to store them like this :
Zones(Release Candidate)
zone 1
z1_pattern (1)
Some obstacle-prefab
z1_pattern (2)
bunch of obstacle preabs
and if someone needs to tweak a pattern i.e objects that are children of pattern (2) - we almost always want to apply any changes from pattern (2) and children as overrides to pattern(2) - but since they are in a nested prefab, this workflow becomes very cumbersome because we must go to the Zones(Release Candidate) object, and pick every single change to apply it to pattern(2).
Is there a way, editor script or otherwise that can apply all changes as if pattern(2) was the top level prefab?
Maybe a picture makes it clearer :
I want to do this for every thing on the pattern level…
Ideally an object would have
“Apply all changes in my hierarchy as overloads to me”.
And I could just press that button on the LA_Z1_Pattern(1) gameobject…
public static class PrefabExtensions
{
public static void ApplyAllChangesInMyHierarchyToMeAsPrefab(GameObject prefabGameObject)
{
var status = PrefabUtility.GetPrefabInstanceStatus(prefabGameObject);
if (status != PrefabInstanceStatus.NotAPrefab && status != PrefabInstanceStatus.MissingAsset)
{
var origFab = PrefabUtility.GetCorrespondingObjectFromOriginalSource(prefabGameObject);
if (origFab != null)
{
var assetPath = AssetDatabase.GetAssetPath(origFab);
ApplyAllPrefabChangesInGivenHierarchyToPrefabAtPath(assetPath,prefabGameObject);
}
}
}
private static void ApplyAllPrefabChangesInGivenHierarchyToPrefabAtPath(string assetPath, GameObject hierarchy)
{
var status = PrefabUtility.GetPrefabInstanceStatus(hierarchy);
if (status != PrefabInstanceStatus.NotAPrefab)
{
foreach (var ob in PrefabUtility.GetAddedComponents(hierarchy.gameObject))
{
ob.Apply(assetPath);
}
foreach (var ob in PrefabUtility.GetObjectOverrides(hierarchy.gameObject))
{
ob.Apply(assetPath);
}
foreach (var ob in PrefabUtility.GetAddedGameObjects(hierarchy.gameObject))
{
ob.Apply(assetPath);
}
foreach (var ob in PrefabUtility.GetRemovedComponents(hierarchy.gameObject))
{
ob.Apply(assetPath);
}
}
for (int i = 0; i < hierarchy.transform.childCount; i++)
{
ApplyAllPrefabChangesInGivenHierarchyToPrefabAtPath(assetPath,hierarchy.transform.GetChild(i).gameObject);
}
}
}
This … seems to work. Maybe there is some gotchas when I do more thorough testing…