using System; using System.Collections.Generic; using UnityEngine; namespace ZXKFramework { public static class UnityExtension { public static bool IsNull(this T selfObj) where T : class { return null == selfObj; } public static bool IsNotNull(this T selfObj) where T : class { return null != selfObj; } public static Tvalue TryGet(this Dictionary dict, Tkey key) { Tvalue value; dict.TryGetValue(key, out value); return value; } private static readonly List s_CachedTransforms = new List(); public static void SetActiveSafe(this GameObject gameObject, bool value) { if (gameObject.activeInHierarchy != value) { gameObject.SetActive(value); } } /// /// 获取或增加组件。 /// /// 要获取或增加的组件。 /// 目标对象。 /// 获取或增加的组件。 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; } public static T GetOrAddComponent(this Transform transform) where T : Component { T component = transform.GetComponent(); if (component == null) { component = transform.gameObject.AddComponent(); } return component; } public static Component GetOrAddComponent(this Transform transform, Type type) { Component component = transform.GetComponent(type); if (component == null) { component = transform.gameObject.AddComponent(type); } return component; } #region 查找模块 public static GameObject FindFirst(this Transform transform, string objName) { return FindGameobject(transform, objName); } public static GameObject FindFirst(this GameObject gameObject, string objName) { return FindGameobject(gameObject.transform, objName); } public static T FindFirst(this Transform transform, string objName) where T : Component { return FindGameobject(transform, objName); } public static T FindFirst(this GameObject gameObject, string objName) where T : Component { return FindGameobject(gameObject.transform, objName); } //查找组件 public static T FindGameobject(Transform trs, string objName) where T : Component { GameObject tempObj = FindGameobject(trs, objName); if (tempObj != null) { if (tempObj.GetComponent() != null) { return tempObj.GetComponent(); } else { Debug.LogError(string.Format("FindTools.FindGameobject 无法找到 {0} 对应的游戏组件 {1}", objName, typeof(T).Name)); } } return null; } //所有提示都已写全 public static GameObject FindGameobject(Transform trs, string objName) { if (trs == null) { Debug.LogError("FindTools.FindGameobject trs = null"); return null; } if (string.IsNullOrEmpty(objName)) { Debug.LogError("FindTools.FindGameobject objName = null"); return null; } GameObject tempObj = Find(trs, objName); if (tempObj == null) { Debug.LogError(string.Format("FindTools.FindGameobject 无法找到 {0} 对应的游戏物体", objName)); } return tempObj; } private static GameObject Find(Transform trs, string objName) { if (trs.name == objName) { return trs.gameObject; } if (trs.childCount > 0) { foreach (Transform tempTrs in trs) { GameObject obj = Find(tempTrs, objName); if (obj != null) { return obj; } } } return null; } #endregion /// /// 获取 GameObject 是否在场景中。 /// /// 目标对象。 /// GameObject 是否在场景中。 /// 若返回 true,表明此 GameObject 是一个场景中的实例对象;若返回 false,表明此 GameObject 是一个 Prefab。 public static bool InScene(this GameObject gameObject) { return gameObject.scene.name != null; } /// /// 递归设置游戏对象的层次。 /// /// 对象。 /// 目标层次的编号。 public static void SetLayerRecursively(this GameObject gameObject, int layer) { gameObject.GetComponentsInChildren(true, s_CachedTransforms); for (int i = 0; i < s_CachedTransforms.Count; i++) { s_CachedTransforms[i].gameObject.layer = layer; } s_CachedTransforms.Clear(); } /// /// 取 的 (x, y, z) 转换为 的 (x, z)。 /// /// 要转换的 Vector3。 /// 转换后的 Vector2。 public static Vector2 ToVector2(this Vector3 vector3) { return new Vector2(vector3.x, vector3.z); } /// /// 取 的 (x, y) 转换为 的 (x, 0, y)。 /// /// 要转换的 Vector2。 /// 转换后的 Vector3。 public static Vector3 ToVector3(this Vector2 vector2) { return new Vector3(vector2.x, 0f, vector2.y); } /// /// 取 的 (x, y) 和给定参数 y 转换为 的 (x, 参数 y, y)。 /// /// 要转换的 Vector2。 /// Vector3 的 y 值。 /// 转换后的 Vector3。 public static Vector3 ToVector3(this Vector2 vector2, float y) { return new Vector3(vector2.x, y, vector2.y); } #region Transform /// /// 设置绝对位置的 x 坐标。 /// /// 对象。 /// x 坐标值。 public static void SetPositionX(this Transform transform, float newValue) { Vector3 v = transform.position; v.x = newValue; transform.position = v; } /// /// 设置绝对位置的 y 坐标。 /// /// 对象。 /// y 坐标值。 public static void SetPositionY(this Transform transform, float newValue) { Vector3 v = transform.position; v.y = newValue; transform.position = v; } /// /// 设置绝对位置的 z 坐标。 /// /// 对象。 /// z 坐标值。 public static void SetPositionZ(this Transform transform, float newValue) { Vector3 v = transform.position; v.z = newValue; transform.position = v; } /// /// 增加绝对位置的 x 坐标。 /// /// 对象。 /// x 坐标值增量。 public static void AddPositionX(this Transform transform, float deltaValue) { Vector3 v = transform.position; v.x += deltaValue; transform.position = v; } /// /// 增加绝对位置的 y 坐标。 /// /// 对象。 /// y 坐标值增量。 public static void AddPositionY(this Transform transform, float deltaValue) { Vector3 v = transform.position; v.y += deltaValue; transform.position = v; } /// /// 增加绝对位置的 z 坐标。 /// /// 对象。 /// z 坐标值增量。 public static void AddPositionZ(this Transform transform, float deltaValue) { Vector3 v = transform.position; v.z += deltaValue; transform.position = v; } /// /// 设置相对位置的 x 坐标。 /// /// 对象。 /// x 坐标值。 public static void SetLocalPositionX(this Transform transform, float newValue) { Vector3 v = transform.localPosition; v.x = newValue; transform.localPosition = v; } /// /// 设置相对位置的 y 坐标。 /// /// 对象。 /// y 坐标值。 public static void SetLocalPositionY(this Transform transform, float newValue) { Vector3 v = transform.localPosition; v.y = newValue; transform.localPosition = v; } /// /// 设置相对位置的 z 坐标。 /// /// 对象。 /// z 坐标值。 public static void SetLocalPositionZ(this Transform transform, float newValue) { Vector3 v = transform.localPosition; v.z = newValue; transform.localPosition = v; } /// /// 增加相对位置的 x 坐标。 /// /// 对象。 /// x 坐标值。 public static void AddLocalPositionX(this Transform transform, float deltaValue) { Vector3 v = transform.localPosition; v.x += deltaValue; transform.localPosition = v; } /// /// 增加相对位置的 y 坐标。 /// /// 对象。 /// y 坐标值。 public static void AddLocalPositionY(this Transform transform, float deltaValue) { Vector3 v = transform.localPosition; v.y += deltaValue; transform.localPosition = v; } /// /// 增加相对位置的 z 坐标。 /// /// 对象。 /// z 坐标值。 public static void AddLocalPositionZ(this Transform transform, float deltaValue) { Vector3 v = transform.localPosition; v.z += deltaValue; transform.localPosition = v; } /// /// 设置相对尺寸的 x 分量。 /// /// 对象。 /// x 分量值。 public static void SetLocalScaleX(this Transform transform, float newValue) { Vector3 v = transform.localScale; v.x = newValue; transform.localScale = v; } /// /// 设置相对尺寸的 y 分量。 /// /// 对象。 /// y 分量值。 public static void SetLocalScaleY(this Transform transform, float newValue) { Vector3 v = transform.localScale; v.y = newValue; transform.localScale = v; } /// /// 设置相对尺寸的 z 分量。 /// /// 对象。 /// z 分量值。 public static void SetLocalScaleZ(this Transform transform, float newValue) { Vector3 v = transform.localScale; v.z = newValue; transform.localScale = v; } /// /// 增加相对尺寸的 x 分量。 /// /// 对象。 /// x 分量增量。 public static void AddLocalScaleX(this Transform transform, float deltaValue) { Vector3 v = transform.localScale; v.x += deltaValue; transform.localScale = v; } /// /// 增加相对尺寸的 y 分量。 /// /// 对象。 /// y 分量增量。 public static void AddLocalScaleY(this Transform transform, float deltaValue) { Vector3 v = transform.localScale; v.y += deltaValue; transform.localScale = v; } /// /// 增加相对尺寸的 z 分量。 /// /// 对象。 /// z 分量增量。 public static void AddLocalScaleZ(this Transform transform, float deltaValue) { Vector3 v = transform.localScale; v.z += deltaValue; transform.localScale = v; } /// /// 二维空间下使 指向指向目标点的算法,使用世界坐标。 /// /// 对象。 /// 要朝向的二维坐标点。 /// 假定其 forward 向量为 public static void LookAt2D(this Transform transform, Vector2 lookAtPoint2D) { Vector3 vector = lookAtPoint2D.ToVector3() - transform.position; vector.y = 0f; if (vector.magnitude > 0f) { transform.rotation = Quaternion.LookRotation(vector.normalized, Vector3.up); } } #endregion Transform } }