public static void OnObjectWillBeDestroyed(UnityEngine.GameObject objToDestroy) { TopInfo parentTopInfo = TopInfo.GameObjectTop(objToDestroy, 2).GameObjectTopInfo <TopInfo>(); if (parentTopInfo) { parentTopInfo.UnlinkOnDestroyRemove(objToDestroy); } TopInfo topInfo = objToDestroy.GameObjectTopInfo <TopInfo>(); if (topInfo == null) { return; } List <UnityEngine.GameObject> list = topInfo.UnlinkOnDestroy; foreach (UnityEngine.GameObject linkedObj in list) { #if DEBUG Debug.Assert(linkedObj != null && linkedObj.GetComponent <TopInfo>() != null, "OnObjectWillBeDestroyed linked object " + linkedObj + " without topinfo."); #endif LinkTo(linkedObj, null); } }
public static UnityEngine.GameObject GameObjectTop(UnityEngine.GameObject obj, int level) { if (obj == null) { return(null); } Transform tr = obj.transform; int finded = 0; while (tr != null) { TopInfo info = tr.GetComponent <TopInfo>(); if (info != null) { finded++; if (finded == level) { return(tr.gameObject); } } tr = tr.parent; } // Picus.Sys.Debug.Throw("GameObjectTop(" + level + ") asked on " + obj + " but found only on " + finded, true); return(null); }
protected T GameObjectTopInfo <T>() where T : Utils.GO.TopInfo { if (_prefabTopInfo == null && GameObjectTop() != null) { _prefabTopInfo = GameObjectTop().GetComponent <Utils.GO.TopInfo>(); } Debug.Assert(_prefabTop != null, "GameObjectTopInfo<Utils.GameObjectTopInfo>() asked but not found", true); return(_prefabTopInfo as T); }
protected UnityEngine.GameObject GameObjectTop() { if (_prefabTop == null) { _prefabTop = Utils.GO.TopInfo.GameObjectTop(gameObject, 1); if (_prefabTop != null) { _prefabTopInfo = _prefabTop.GetComponent <Utils.GO.TopInfo>(); } Debug.Assert(_prefabTop != null, "GameObjectTop() asked but not found", true); } return(_prefabTop); }
/// <summary> /// Links to toObj. /// </summary> /// <param name="destroyWithParent">If parent is destroyed and set to <c>false</c> it will unlinks and stay on last position.</param> public static void LinkTo(UnityEngine.GameObject obj, UnityEngine.GameObject toObj, bool inheritRotation = true, bool inheritSorting = true, bool destroyWithParent = false) { if (obj == null) { return; } Finder.FindComponentAddIfNotExist <TopInfo>(obj); if (toObj == null) { TopInfo topInfo = TopInfo.GameObjectTop(obj, 2).GameObjectTopInfo <TopInfo>(); obj.transform.parent = Sys.ResourceManager.Instance.RuntimeParent(); if (topInfo) { topInfo.UnlinkOnDestroyRemove(obj); } } else { if (!destroyWithParent) { TopInfo topInfo = toObj.GameObjectTopInfo <TopInfo>(); if (topInfo) { topInfo.UnlinkOnDestroyAdd(obj); } } obj.transform.position = toObj.transform.position; if (inheritRotation) { obj.transform.rotation = toObj.transform.rotation; } obj.transform.parent = toObj.transform; if (inheritSorting) { Finder.FindComponentAddIfNotExist <SortingLayer>(obj).SortAllDeepFromParent(); } } }