bool RefreshGameObjectWithPrefab(GameObject go, GameObject prefab) { ArrayList addList = new ArrayList(go.transform.childCount); ArrayList deleteList = new ArrayList(go.transform.childCount); bool hasSubChanges = false; foreach (Transform child in go.transform) { GameObject childGo = child.gameObject; if (childGo.name == prefab.name) { PrefabCopy copy = new PrefabCopy(); copy.go = PrefabUtility.InstantiatePrefab(prefab) as GameObject; copy.pos = child.localPosition; copy.rot = child.localRotation; copy.scale = child.localScale; addList.Add(copy); deleteList.Add(childGo); } else { if (RefreshGameObjectWithPrefab(childGo, prefab)) { hasSubChanges = true; } } } foreach (GameObject trash in deleteList) { DestroyImmediate(trash); } foreach (PrefabCopy copy in addList) { copy.go.transform.parent = go.transform; copy.go.transform.localPosition = copy.pos; copy.go.transform.localRotation = copy.rot; copy.go.transform.localScale = copy.scale; go.BroadcastMessage("OnPrefabUpdate", copy.go, SendMessageOptions.DontRequireReceiver); } //fail-safe for duplicates deleteList.Clear(); bool foundAlready = false; foreach (Transform child in go.transform) { GameObject childGo = child.gameObject; if (child.name == prefab.name) { if (foundAlready) { deleteList.Add(childGo); } else { foundAlready = true; } } } foreach (GameObject trash in deleteList) { DestroyImmediate(trash); } // return(addList.Count > 0 || hasSubChanges); }
bool RefreshGameObjectWithPrefab(GameObject go, GameObject prefab) { ArrayList addList = new ArrayList(go.transform.childCount); ArrayList deleteList = new ArrayList(go.transform.childCount); bool hasSubChanges = false; foreach(Transform child in go.transform) { GameObject childGo = child.gameObject; if(childGo.name == prefab.name) { PrefabCopy copy = new PrefabCopy(); copy.go = PrefabUtility.InstantiatePrefab(prefab) as GameObject; copy.pos = child.localPosition; copy.rot = child.localRotation; copy.scale = child.localScale; addList.Add(copy); deleteList.Add(childGo); } else { if(RefreshGameObjectWithPrefab(childGo, prefab)) { hasSubChanges = true; } } } foreach(GameObject trash in deleteList) { DestroyImmediate(trash); } foreach(PrefabCopy copy in addList) { copy.go.transform.parent = go.transform; copy.go.transform.localPosition = copy.pos; copy.go.transform.localRotation = copy.rot; copy.go.transform.localScale = copy.scale; go.BroadcastMessage("OnPrefabUpdate", copy.go, SendMessageOptions.DontRequireReceiver); } //fail-safe for duplicates deleteList.Clear(); bool foundAlready = false; foreach(Transform child in go.transform) { GameObject childGo = child.gameObject; if(child.name == prefab.name) { if(foundAlready) { deleteList.Add(childGo); } else { foundAlready = true; } } } foreach(GameObject trash in deleteList) { DestroyImmediate(trash); } // return addList.Count > 0 || hasSubChanges; }