public GameObject SwapPrefabs(GameObject obj, List <GameObject> toReparent = null) { PrefabUID objectID = obj.GetComponent <PrefabUID>(); // Get this object's ID GameObject masterPrefab = FindMatchingID(objectID); // prefab to update the current Game Object with if (masterPrefab != null) { string oldName = masterPrefab.name; GameObject oldParent = obj.transform.parent.gameObject; // store the old parent and transform data Vector3 oldScale = obj.transform.localScale; Vector3 oldPosition = obj.transform.position; Vector3 oldRotation = obj.transform.eulerAngles; GameObject copyPrefab; // Instantiate a copy of the master prefab, apply the transform data and resolve parenting & children copyPrefab = Instantiate(masterPrefab); copyPrefab.transform.position = oldPosition; copyPrefab.transform.localScale = oldScale; copyPrefab.transform.eulerAngles = oldRotation; copyPrefab.transform.parent = oldParent.transform; copyPrefab.name = oldName; if (toReparent.Count > 0) { foreach (GameObject child in toReparent) { child.transform.parent = copyPrefab.transform; } } toDelete.Add(obj); // add the old object to the list of objects to be deleted return(copyPrefab); } return(null); }
public GameObject FindMatchingID(PrefabUID child) { foreach (PrefabUID ID in prefabCache.prefabsToUpdate) { if (ID.m_IDName == child.m_IDName) { return(ID.gameObject); } } return(null); }
void OnEnable() { UID = (PrefabUID)target; stringID = serializedObject.FindProperty("IDName"); }