using System; using System.Collections.Generic; using Cysharp.Threading.Tasks; using UnityEngine; using UnityEngine.SceneManagement; using Object = UnityEngine.Object; namespace ZC { public static class CommonHelper { public static T FindChildDeep(this Transform self, string name) where T : Object { Transform findDeep = null; FindDeep(self, name, ref findDeep); if (findDeep == null) { Debug.LogError("未找到此组件"); } var component = findDeep.GetComponent(); return component; } static void FindDeep(Transform tran, string name, ref Transform transform) { if (tran.name == name) { transform = tran; return; } for (var i = 0; i < tran.childCount; i++) { FindDeep(tran.GetChild(i), name, ref transform); } } public static List FindChildDeeps(this Transform self) where T : Object { List list = new List(); FindDeeps(self, ref list); if (list.Count <= 0) { Debug.LogError("未找到此组件"); } return list; } static void FindDeeps(Transform tran, ref List list) { var component = tran.GetComponent(); if (component != null) list.Add(component); for (var i = 0; i < tran.childCount; i++) { FindDeeps(tran.GetChild(i), ref list); } } /// /// 判断是否在某个物体范围内(范围型判断,超出返回false) /// 基于MeshRender的实现方式 /// /// /// /// public static bool IsInRangeAutoFix(Transform tran, Transform self) { var meshRenderer = tran.GetComponent(); Bounds rendererBounds = self.GetComponent().bounds; Bounds colliderBounds = meshRenderer.bounds; bool rendererIsInsideBox = colliderBounds.Intersects(rendererBounds); return rendererIsInsideBox; } public static T GetOrAddComponent(this GameObject go) where T : Component { if (go.TryGetComponent(typeof(T), out var component)) { return (T)component; } return go.AddComponent(); } public static async UniTask GetSceneBindingAsync( Action callback = null) { SceneGameObjectBinding binding = null; while (binding == null) { await UniTask.Yield(); Debug.Log("GetSceneBindingAsync"); binding = GameObject.FindObjectOfType(); } callback?.Invoke(binding); return binding; } } }