using System; using System.Collections; using System.Collections.Generic; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using DG.Tweening; using XLua; namespace XGame { [LuaCallCSharp] public static partial class Utility { private static readonly List mTempList = new List(); public static void DestroyUnityObject(UnityEngine.Object unityObject) { if (unityObject != null) { UnityEngine.Object.Destroy(unityObject); } } public static void DestroyUnityObjectImmediate(UnityEngine.Object unityObject) { if (unityObject != null) { UnityEngine.Object.DestroyImmediate(unityObject); } } public static GameObject CreateGameObject(GameObject parent, string name) { GameObject go = new GameObject(name); if (parent != null) { go.transform.parent = parent.transform; } NormalizeTransform(go.transform); return go; } public static GameObject NewGameObeject(string name) { return new GameObject(name); } //gameobject ops //create a child gameobject under the parent ,then normilize its transform public static GameObject CreateChildGameObject(GameObject parent, string name) { GameObject go = new GameObject(name); if (parent != null) { go.transform.parent = parent.transform; } NormalizeTransform(go.transform); return go; } public static bool DestroyChildren(Transform transform) { if (transform == null || transform.childCount == 0) { return false; } List childTransformList = new List(); foreach (Transform child in transform) { childTransformList.Add(child); } for (int i = 0; i < childTransformList.Count; i++) { childTransformList[i].parent = null; DestroyUnityObject(childTransformList[i].gameObject); } childTransformList.Clear(); return true; } //normalize an transform public static void NormalizeTransform(Transform transform) { transform.localPosition = Vector3.zero; transform.localRotation = Quaternion.identity; transform.localScale = Vector3.one; } public static T FindObjectInScene() where T : UnityEngine.Object { T component = UnityEngine.Object.FindObjectOfType() as T; return component; } public static bool IsActive(this GameObject gameObject) { if (gameObject == null) { return false; } if (gameObject.activeSelf) { return gameObject.transform.localScale == Vector3.one; } return false; } public static void SetActiveByScale(this Component component, bool active) { if (null == component) { return; } component.transform.localScale = active ? Vector3.one : Vector3.zero; } public static void SetActiveByScale(this GameObject component, bool active) { if (null == component) { return; } component.transform.localScale = active ? Vector3.one : Vector3.zero; } public static void SetActiveEx(this Transform t, bool active) { if (null == t) { return; } t.gameObject.SetActiveEx(active); } public static void SetActiveEx(this Component component, bool active) { if (null == component) { return; } if (component.gameObject.activeSelf != active) { component.gameObject.SetActive(active); } } public static bool SetActiveEx(this GameObject gameObject, bool active) { if (gameObject == null) { return false; } if (gameObject.activeSelf != active) { gameObject.SetActive(active); } return true; } /// /// 通过缩放大小 隐藏 /// /// /// /// public static bool SetActiveExTwo(this GameObject gameObject, bool active) { if (gameObject == null) { return false; } //强制active if (gameObject.activeSelf == false) { gameObject.SetActive(true); } if (active) { gameObject.transform.localScale = Vector3.one; } else { gameObject.transform.localScale = Vector3.zero; } return true; } public static bool SetActiveExTwo(this Component comp, bool active) { if (comp == null) { return false; } SetActiveExTwo(comp.gameObject, active); return true; } public static bool SetActive3(this Transform t, bool active) { return SetActive3(t.gameObject, active); } /// /// 通过把位置挪出屏幕外 隐藏 /// /// /// /// public static bool SetActive3(this GameObject gameObject, bool active) { if (gameObject == null) { return false; } //gameObject.SetActiveEx(active); //CanvasGroup g = gameObject.GetComponent(); //if (g == null) //{ // g = gameObject.AddComponent(); //} //g.alpha = active ? 1 : 0; //g.interactable = active; //g.blocksRaycasts = active; //Canvas[] canvases = gameObject.GetComponentsInChildren(); //int toLayer = LayerMask.NameToLayer(active ? "UI" : "InVisible"); //for (int i = 0; i < canvases.Length; i++) //{ // canvases[i].gameObject.layer = toLayer; //} //MeshRenderer[] mrs = gameObject.GetComponentsInChildren(); //for (int i = 0; i < mrs.Length; i++) //{ // mrs[i].enabled = active; //} //UiEffect[] effects = gameObject.GetComponentsInChildren(); //for (int i = 0; i < effects.Length; i++) //{ // effects[i].gameObject.layer = toLayer; //} //gameObject.SetActiveEx(active); Vector3 pos = gameObject.transform.localPosition; gameObject.transform.localPosition = new Vector3(pos.x, pos.y, active ? 0 : 99999); return true; } /// /// 是否隐藏了 /// /// /// public static bool IsHide(this GameObject gameObject) { return !gameObject.activeSelf || gameObject.transform.localScale == Vector3.zero || gameObject.transform.localPosition.z >= 99999; } public static bool IsHide(this Transform t) { return !t.gameObject.activeSelf || t.localScale == Vector3.zero || t.localPosition.z >= 99999; } public static GameObject[] GetChildren(this GameObject gameObject) { List children = new List(); foreach (Transform child in gameObject.transform) { children.Add(child.gameObject); } return children.ToArray(); } public static GameObject FindGameObject(this GameObject go, string name) { return !go ? null : go.transform.FindGameObject(name); } public static GameObject FindGameObject(this Transform tf, string name) { Transform targetTransform = tf.FindTransform(name); return targetTransform ? targetTransform.gameObject : null; } public static Transform FindTransform(this GameObject go, string name) { return !go ? null : go.transform.FindTransform(name); } public static Transform FindTransform(this Transform tf, string name) { if (!tf) { return null; } if (string.IsNullOrEmpty(name)) { return null; } mTempList.Clear(); mTempList.Add(tf); int index = 0; while (mTempList.Count > index) { Transform transform = mTempList[index++]; for (int i = 0; i < transform.childCount; ++i) { Transform childTransform = transform.GetChild(i); if (childTransform.name == name) { return childTransform; } mTempList.Add(childTransform); } } return null; } public static T FindComponent(this GameObject go, string name) where T : Component { if (go == null) { return null; } return go.transform.FindComponent(name); } public static T FindComponent(this Transform tf, string name) where T : Component { if (tf == null) { return null; } Transform target = tf.FindTransform(name); if (target == null) { return null; } return target.GetComponent(); } public static Component GetOrAddComponent(this GameObject GO, string typeName) { if (string.IsNullOrEmpty(typeName)) { Debug.LogError("GetOrAddComponent typeName is null"); return null; } Type componentType = Utility.GetTypeByTypeName(typeName); if (componentType == null) { Debug.LogError($"GetOrAddComponent componentType is null, typeName:{typeName}"); return null; } Component result = null; if (!GO) return result; result = GO.GetComponent(componentType); if (!result) { result = GO.AddComponent(componentType); } return result; } public static T GetOrAddComponent(this GameObject GO) where T : UnityEngine.Component { T result = null; if (!GO) return result; result = GO.GetComponent(); if (!result) { result = GO.AddComponent(); } return result; } public static GameObject[] GetAllChildrenGameObjects(this GameObject go) { Transform[] allTrans = go.GetComponentsInChildren(); List allGOs = new List(); for (int i = 0; i < allTrans.Length; ++i) { allGOs.Add(allTrans[i].gameObject); } return allGOs.ToArray(); } public static Transform FindSocketTransform(GameObject go, string socketName) { if (string.IsNullOrEmpty(socketName)) { return null; } Transform socketTrans = null; Transform[] childrenTrans = go.transform.GetComponentsInChildren(true); int count = childrenTrans != null ? childrenTrans.Length : 0; for (int i = 0; i < count; i++) { if (childrenTrans[i] != null && childrenTrans[i].name == socketName) { socketTrans = childrenTrans[i]; break; } } return socketTrans; } public static void SetPosition(this GameObject go, float x, float y, float z) { if (go == null) { Debug.LogError("Utility SetPosition go is null"); return; } go.transform.position = new Vector3(x, y, z); } public static void SetPosition(this Transform trans, float x, float y, float z) { if (trans == null) { Debug.LogError("Utility SetPosition trans is null"); return; } trans.position = new Vector3(x, y, z); } public static void SetLocalPosition(this GameObject go, float x, float y, float z) { if (go == null) { Debug.LogError("Utility SetLocalPosition go is null"); return; } go.transform.localPosition = new Vector3(x, y, z); } public static void SetLocalPosition(this Transform trans, float x, float y, float z) { if (trans == null) { Debug.LogError("Utility SetLocalPosition trans is null"); return; } trans.localPosition = new Vector3(x, y, z); } public static void SetRotation(this GameObject go, float x, float y, float z, float w) { if (go == null) { Debug.LogError("Utility SetRotation go is null"); return; } go.transform.rotation = new Quaternion(x, y, z, w); } public static void SetRotation(this Transform trans, float x, float y, float z, float w) { if (trans == null) { Debug.LogError("Utility SetRotation trans is null"); return; } trans.rotation = new Quaternion(x, y, z, w); } public static void SetLocalRotation(this GameObject go, float x, float y, float z, float w) { if (go == null) { Debug.LogError("Utility SetLocalRotation go is null"); return; } go.transform.localRotation = new Quaternion(x, y, z, w); } public static void SetLocalRotation(this Transform trans, float x, float y, float z, float w) { if (trans == null) { Debug.LogError("Utility SetLocalRotation trans is null"); return; } trans.localRotation = new Quaternion(x, y, z, w); } public static void SetLocalEulerAngles(this GameObject go, float x, float y, float z) { if (go == null) { Debug.LogError("Utility SetLocalEulerAngles go is null"); return; } go.transform.localEulerAngles = new Vector3(x, y, z); } public static void SetLocalEulerAngles(this Transform trans, float x, float y, float z) { if (trans == null) { Debug.LogError("Utility SetLocalEulerAngles trans is null"); return; } trans.localEulerAngles = new Vector3(x, y, z); } public static void SetLocalScale(this GameObject go, float x, float y, float z) { if (go == null) { Debug.LogError("Utility SetLocalScale go is null"); return; } go.transform.localScale = new Vector3(x, y, z); } public static void SetLocalScale(this Transform trans, float x, float y, float z) { if (trans == null) { Debug.LogError("Utility SetLocalScale trans is null"); return; } trans.localScale = new Vector3(x, y, z); } public static void SetLocalScale(this Transform trans, float scale) { if (trans == null) { Debug.LogError("Utility SetLocalScale trans is null"); return; } trans.localScale = new Vector3(scale, scale, scale); } public static DG.Tweening.Core.TweenerCore DOLocalMove(this Transform trans,float x,float y,float z,float duration,bool snapping = false) { return trans.DOLocalMove(new Vector3(x, y, z), duration, snapping); } #region 属性获得 减少引用传递 public static float GetPosition(this Transform t, out float y, out float z) { Vector3 position = t.position; y = position.y; z = position.z; return t.position.x; } public static float GetPosition(this GameObject t, out float y, out float z) { return t.transform.GetPosition(out y,out z); } public static float GetLocalPosition(this Transform t, out float y, out float z) { Vector3 position = t.localPosition; y = position.y; z = position.z; return position.x; } public static float GetLocalPosition(this GameObject t, out float y, out float z) { return t.transform.GetLocalPosition(out y, out z); } public static void GetLocalScale(this Transform t, out float x, out float y, out float z) { x = t.localScale.x; y = t.localScale.y; z = t.localScale.z; } public static void GetLocalScale(this GameObject t, out float x, out float y, out float z) { t.transform.GetLocalScale(out x,out y, out z); } public static void GetRotation(this Transform t, out float x, out float y, out float z, out float w) { x = t.rotation.x; y = t.rotation.y; z = t.rotation.z; w = t.rotation.w; } public static void GetRotation(this GameObject o, out float x, out float y, out float z, out float w) { o.transform.GetRotation(out x, out y, out z, out w); } public static void GetLocalRotation(this Transform t, out float x, out float y, out float z, out float w) { x = t.localRotation.x; y = t.localRotation.y; z = t.localRotation.z; w = t.localRotation.w; } public static void GetLocalRotation(this GameObject o, out float x, out float y, out float z, out float w) { o.transform.GetLocalRotation(out x, out y, out z, out w); } public static void GetEulerAngle(this Transform t, out float x, out float y, out float z) { x = t.eulerAngles.x; y = t.eulerAngles.y; z = t.eulerAngles.z; } public static void GetEulerAngle(this GameObject o, out float x, out float y, out float z) { o.transform.GetEulerAngle(out x, out y, out z); } public static void GetLocalEulerAngle(this Transform t, out float x, out float y, out float z) { x = t.localEulerAngles.x; y = t.localEulerAngles.y; z = t.localEulerAngles.z; } public static void GetLocalEulerAngle(this GameObject o, out float x, out float y, out float z) { o.transform.GetLocalEulerAngle(out x, out y, out z); } public static void GetAnchoredPosition(this RectTransform t, out float x, out float y) { x = t.anchoredPosition.x; y = t.anchoredPosition.y; } public static bool IsActive(this Transform t) { return t.gameObject.IsActive(); } #endregion public static bool Exist(this UnityEngine.Object obj) { return obj; } public static bool IsUnityObject(object unityObject) { if (unityObject != null) { if (unityObject is UnityEngine.Object) { return true; } } return false; } public static Component GetComponent(this Component component, string asm, string typeName) { var gen_ret = component.GetComponent(typeName); if (gen_ret != null) return gen_ret; System.Type componentType = Utility.GetTypeByTypeName(typeName); if (componentType == null) { #if ENV_INTRANET Debug.LogWarning($"GetType('{typeName}') Failed!! componentName:{component.name}"); #endif return null; } gen_ret = component.GetComponent(componentType); if (gen_ret == null) { #if ENV_INTRANET Debug.LogWarning($"{component.name}.GetComponent({typeName}) Failed!!"); #endif } return gen_ret; } public static Component GetComponent(this GameObject gameObject, string asm, string typeName) { var gen_ret = gameObject.GetComponent(typeName); if (gen_ret != null) return gen_ret; System.Type componentType = Utility.GetTypeByTypeName(typeName); if (componentType == null) { #if ENV_INTRANET Debug.LogWarning($"GetType('{typeName}') Failed!! objectName:{gameObject.name}"); #endif return null; } gen_ret = gameObject.GetComponent(componentType); if (gen_ret == null) { #if ENV_INTRANET Debug.LogWarning($"Go.GetComponent({typeName}) Failed!!"); #endif } return gen_ret; } //防止代码剪裁 //private static void PreventCodeClipping(GameObject obj) //{ // Empty4Raycast a = obj.GetComponent(); //} } }