/// <summary> /// Make a barebones copy of an object including only empty gameobjects and colliders. /// </summary> private static void CloneChildrenAndColliders(GameObject srcGO, GameObject copyGO, Ghost rootGhost) { copyGO.transform.localPosition = srcGO.transform.localPosition; copyGO.transform.localScale = srcGO.transform.localScale; copyGO.transform.localRotation = srcGO.transform.localRotation; #if !UNITY_2019_1_OR_NEWER copyGO.layer = GHOST_WORLD_LAYER; #else copyGO.layer = srcGO.layer; #endif /// Clone the colliders on this node CloneColliders(rootGhost, srcGO, copyGO, ref colliderCount); /// Create GhostComponent links if any srcGO.GetComponents(reusableNeedsGameObjectClone); GhostComponent ghostComponent = null; foreach (var iNeedsClone in reusableNeedsGameObjectClone) { IHauntedComponent ihc = iNeedsClone as IHauntedComponent; if (ihc != null) { if (ghostComponent == null) { ghostComponent = copyGO.AddComponent <GhostComponent>(); } ghostComponent.AddHaunted(ihc, rootGhost); } /// Copy any components that are flagged by interface to be copied to ghost ICopyToGhost icopy = iNeedsClone as ICopyToGhost; if (icopy != null) { (icopy as Component).ComponentCopy(copyGO); } } ///// Copy any components that are flagged by interface to be copied to ghost //srcGO.GetComponents(reusableCopyToGhostFind); //foreach (var icopy in reusableCopyToGhostFind) //{ // (icopy as Component).ComponentCopy(copyGO); //} /// Find all children and repeat this cloning process for each child for (int i = 0; i < srcGO.transform.childCount; i++) { Transform orig = srcGO.transform.GetChild(i); /// Test to see if there is any reason to clone children (colliders or components flagged as needing their child cloned) if (orig.GetComponentInChildren <Collider>() == null && orig.GetComponentInChildren <INeedsGhostGameObject>() == null) { continue; } Transform copy = new GameObject(orig.name).transform; copy.parent = copyGO.transform; CloneChildrenAndColliders(srcGO.transform.GetChild(i).gameObject, copy.gameObject, rootGhost); } }
public void AddHaunted(IHauntedComponent iHauntedComponent, Ghost ghost) { iHauntedComponents.Add(iHauntedComponent); iHauntedComponent.GhostComponent = this; this.ghost = ghost; }