using System; using System.Collections.Generic; using Cysharp.Threading.Tasks; using UnityEngine; using UnityEngineInternal; namespace TEngine { /// /// Unity 的扩展方法辅助类。 /// public static class UnityExtension { /// /// 获取或增加组件。 /// /// 要获取或增加的组件。 /// 目标对象。 /// 获取或增加的组件。 public static T GetOrAddComponent(this GameObject gameObject) where T : Component { T component = gameObject.GetComponent(); if (component == null) { component = gameObject.AddComponent(); } return component; } /// /// 获取或增加组件。 /// /// 目标对象。 /// 要获取或增加的组件类型。 /// 获取或增加的组件。 public static Component GetOrAddComponent(this GameObject gameObject, Type type) { Component component = gameObject.GetComponent(type); if (component == null) { component = gameObject.AddComponent(type); } return component; } /// /// 移除组件。 /// /// 目标对象。 /// 要获取或增加的组件类型。 /// [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] public static void RemoveMonoBehaviour(this GameObject gameObject, Type type) { if (type == null) throw new ArgumentNullException(nameof(type)); Component component = gameObject.GetComponent(type); if (component != null) { UnityEngine.Object.Destroy(component); } } /// /// 移除组件。 /// /// 目标对象。 /// 要获取或增加的组件类型。 public static void RemoveMonoBehaviour(this GameObject gameObject) where T : Component { T component = gameObject.GetComponent(); if (component != null) { UnityEngine.Object.Destroy(component); } } /// /// 获取 GameObject 是否在场景中。 /// /// 目标对象。 /// GameObject 是否在场景中。 /// 若返回 true,表明此 GameObject 是一个场景中的实例对象;若返回 false,表明此 GameObject 是一个 Prefab。 public static bool InScene(this GameObject gameObject) { return gameObject.scene.name != null; } private static readonly List CachedTransforms = new List(); /// /// 递归设置游戏对象的层次。 /// /// 对象。 /// 目标层次的编号。 public static void SetLayerRecursively(this GameObject gameObject, int layer) { gameObject.GetComponentsInChildren(true, CachedTransforms); for (int i = 0; i < CachedTransforms.Count; i++) { CachedTransforms[i].gameObject.layer = layer; } CachedTransforms.Clear(); } /// /// ActivatesDeactivates the GameObject, depending on the given true or false/ value. /// /// GameObject. /// Activate or deactivate the object, where true activates the GameObject and false deactivates the GameObject. /// Cache Activate or deactivate the object, where true activates the GameObject and false deactivates the GameObject. public static void SetActive(this GameObject go, bool value, ref bool cacheValue) { if (go != null && value != cacheValue) { cacheValue = value; go.SetActive(value); } } /// /// 查找子节点。 /// /// 位置组件。 /// 子节点路径。 /// 位置组件。 public static Transform FindChild(this Transform transform, string path) { var findTrans = transform.Find(path); return findTrans != null ? findTrans : null; } /// /// 根据名字找到子节点,主要用于dummy接口。 /// /// 位置组件。 /// 子节点名字。 /// 位置组件。 public static Transform FindChildByName(this Transform transform, string name) { if (transform == null) { return null; } for (int i = 0; i < transform.childCount; i++) { var childTrans = transform.GetChild(i); if (childTrans.name == name) { return childTrans; } var find = FindChildByName(childTrans, name); if (find != null) { return find; } } return null; } [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] public static Component FindChildComponent(this Transform transform, string path, Type type) { var findTrans = transform.Find(path); if (findTrans != null) { return findTrans.gameObject.GetComponent(type); } return null; } public static T FindChildComponent(this Transform transform, string path) where T : Component { var findTrans = transform.Find(path); if (findTrans != null) { return findTrans.gameObject.GetComponent(); } return null; } } }