using System.Collections.Generic; using UnityEngine; namespace YKMoon { public static partial class YKTransformUtility { public static Transform FindChildWithName(this Transform parent, string name) { var que = QueuePool.Get(); que.Enqueue(parent); while(que.Count > 0) { var top = que.Dequeue(); for(int i = 0; i < top.childCount; i++) { var child = top.GetChild(i); if(string.Equals(child.name, name)) { QueuePool.Release(que); return child; } que.Enqueue(child); } } QueuePool.Release(que); return null; } public static List FindChildrenWithName(this Transform parent, string name) { List children = ListPool.Get(); var que = QueuePool.Get(); que.Enqueue(parent); while(que.Count > 0) { var top = que.Dequeue(); for(int i = 0; i < top.childCount; i++) { var child = top.GetChild(i); if(string.Equals(child.name, name)) { children.Add(child); } que.Enqueue(child); } } QueuePool.Release(que); return children; } public static void SetLayerRecursively(this GameObject obj, int layer) { var que = QueuePool.Get(); que.Enqueue(obj.transform); while(que.Count > 0) { var top = que.Dequeue(); top.gameObject.layer = layer; for(int i = 0; i < top.childCount; i++) { var child = top.GetChild(i); que.Enqueue(child); } } QueuePool.Release(que); } public static T FindComponentWithName(this Transform parent, string name) where T : Component { Transform child = FindChildWithName(parent, name); if(child == null) { return null; } else { return child.GetComponent(); } } public static void AddChild(GameObject parent, GameObject child) { AddChild(parent.transform, child.transform); } public static void AddChild(GameObject parent, GameObject child, Vector3 localPos, Vector3 localScale) { AddChild(parent.transform, child.transform, localPos, localScale); } public static void AddChild(this Transform parent, Transform child) { AddChild(parent, child, Vector3.zero, Vector3.one); } public static void AddChild(this Transform parent, Transform child, Vector3 localPos, Vector3 localScale) { child.SetParent(parent); child.localPosition = localPos; child.localScale = localScale; } public static void ClearChild(this Transform parent) { var childs = QueuePool.Get(); for(int i = 0; i < parent.childCount; i++) { childs.Enqueue(parent.GetChild(i)); } while(childs.Count > 0) { GameObject.DestroyImmediate(childs.Dequeue()); } QueuePool.Release(childs); } public static T GetOrAddComponent(this GameObject go) where T : Component { T result = go.GetComponent(); if(result == null) { result = go.AddComponent(); } return result; } public static string GetChildPath(GameObject root, GameObject child) { string path = ""; var top = child.transform; var end = root.transform; while(top != end) { if(string.IsNullOrEmpty(path)) { path = top.name; } else { path = top.name + "/" + path; } top = top.parent; if(top == null) { //没找到// return ""; } } return path; } private static List m_ComponentCache = new List(); /// /// Grabs a component without allocating memory uselessly /// /// /// /// public static Component GetComponentNoAlloc(this GameObject @this, System.Type componentType) { @this.GetComponents(componentType, m_ComponentCache); Component component = m_ComponentCache.Count > 0 ? m_ComponentCache[0] : null; m_ComponentCache.Clear(); return component; } /// /// Grabs a component without allocating memory uselessly /// /// /// /// public static T GetComponentNoAlloc(this GameObject @this) where T : Component { @this.GetComponents(typeof(T), m_ComponentCache); Component component = m_ComponentCache.Count > 0 ? m_ComponentCache[0] : null; m_ComponentCache.Clear(); return component as T; } } }