public Ghoster SpawnTemporary(Func <bool> sustainQuery, IEnumerable <Type> preservedTypes = null) { if (Application.isPlaying && sustainQuery != null) { Ghoster ghost = (Ghoster)GameObject.Instantiate(this); ghost.Origin = this; ghost.SustainQuery = sustainQuery; ghost.transform.parent = this.transform.parent; ghost.transform.localPosition = this.transform.localPosition; ghost.transform.localRotation = this.transform.localRotation; ghost.transform.localScale = this.transform.localScale; ghost.transform.parent = null; ghost.gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.NotEditable; ghost.CleanObject(ghost.gameObject, preservedTypes == null ? Ghoster.DefaultPreservedTypes : preservedTypes); this.ghosts.AddLast(ghost); return(ghost); } return(null); }
private void CleanObject(GameObject target, IEnumerable <Type> preservedTypes) { foreach (Transform child in target.transform) { this.CleanObject(child.gameObject, preservedTypes); } IEnumerator <Type> preservedTypesEnumerator = preservedTypes.GetEnumerator(); // Reverse iteration is a partial solution to required components, as they are added recursively // Note that re-ordering the required components will still break the cleaning process Component[] components = target.GetComponents <Component>(); for (int i = components.Length - 1; i >= 0; --i) { Component component = components[i]; if (!(component is Transform) && !(component is Ghoster) && !this.preservedComponents.Contains(component) && !Ghoster.TypeListContains(preservedTypesEnumerator, component.GetType())) { Component.DestroyImmediate(component); } preservedTypesEnumerator.Reset(); } }