Hi there!
I write a thing which needs to filter overrides, and save only those that are under a specific GO (prefab instance) in the scene hierarchy (named ‘root’ in the following code)
if (PrefabUtility.HasPrefabInstanceAnyOverrides(root.gameObject, false)) {
var list1 = PrefabUtility.GetAddedGameObjects(root.gameObject);
var list2 = PrefabUtility.GetAddedComponents(root.gameObject);
var list3 = PrefabUtility.GetRemovedComponents(root.gameObject);
var list4 = PrefabUtility.GetObjectOverrides(root.gameObject, false);
List<PrefabOverride> overridesList = new List<PrefabOverride>();
list1.ForEach(item => {
if (item.instanceGameObject.transform.IsChildOf(root))
overridesList.Add(item);});
list2.ForEach(item => {
if (item.instanceComponent.transform.IsChildOf(root))
overridesList.Add(item);});
list3.ForEach(item => {
if (item.containingInstanceGameObject.transform.IsChildOf(root))
overridesList.Add(item);});
list4.ForEach(item => {
System.Type type = item.instanceObject.GetType();
if (type == typeof(Transform) && ((Transform)item.instanceObject).IsChildOf(root))
overridesList.Add(item);
});
overridesList.ForEach(item => item.Apply(rootPrefabPath));
}
The list can be large, and then it causes a series of saves in a row, which can be time consuming.
- Can we have something like
PrefabUtility.ApplyOverrides(string prefabAssetPath, PrefabOverride[] overrides);
that will save them in one pass?
- Can ObjectOverride have a reference to corresponding GameObject? It seems to me wrong to do as I did for list4. Or maybe we can have a
List<PrefabOverride> PrefabUtility.GetOverrides(GameObject prefabInstance);
returning combined list?