/******************************************************************** 文件: CommonExtension.cs 作者: 梦语 邮箱: 1982614048@qq.com 创建时间: 2024/04/11 14:55:52 最后修改: 梦语 最后修改时间: 2024/04/22 20:18:33 功能: *********************************************************************/ using System; using System.Collections; using System.Collections.Generic; using TMPro; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using UnityEngine.UI; namespace Ether { public static class CommonExtension { #region UI自适应 #region 文本自适应大小 /// /// Text根据内容适配大小 /// /// public static void SetAutoSize(this TextMeshProUGUI text, float addWidth = 0, float addHeight = 0) { RectTransform rect = text.GetComponent(); // 动态设置width rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, text.preferredWidth + addWidth); // 动态设置height rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight + addHeight); } /// /// Text根据内容适配高度 /// /// public static void SetAutoHeight(this TextMeshProUGUI text, float addSize = 0) { RectTransform rect = text.GetComponent(); // 获取Text的Size Vector2 rectSize = rect.rect.size; // width不变 rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rectSize.x); // 动态设置height rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight + addSize); } /// /// Text根据内容适配宽度 /// /// public static void SetAutoWidth(this TextMeshProUGUI text, float addSize = 0) { RectTransform rect = text.GetComponent(); // 获取Text的Size Vector2 rectSize = rect.rect.size; // 动态设置width rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, text.preferredWidth + addSize); // height不变 rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, rectSize.y); } #endregion #region UI大小设置 public static void SetWidth(this GameObject go, float width) { RectTransform rect = go.GetComponent(); //rect.sizeDelta = new Vector2(width, rect.sizeDelta.y); rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); } public static void SetHeight(this GameObject go, float height) { RectTransform rect = go.GetComponent(); //rect.sizeDelta = new Vector2(rect.sizeDelta.x, height); rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); } public static void SetSize(this GameObject go, Vector2 size) { RectTransform rect = go.GetComponent(); //rect.sizeDelta = size; rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x); rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y); } public static Vector3 GetSize(this GameObject obj) { RectTransform rect = obj.GetComponent(); return rect.rect.size; } #endregion #endregion #if UNITY_EDITOR public static void CreateComponent(GameObject selectObject, Action callBack, params Type[] types) where T : Component { GameObject gameObject; Type[] typesArray = new Type[types.Length + 1]; for (int i = 0; i < types.Length; i++) { typesArray[i] = types[i]; } typesArray[types.Length] = typeof(T); if (selectObject != null) { Debug.Log($"选择物体:{selectObject.name}"); // 获取物体的根 Canvas Canvas rootCanvas = selectObject.GetComponentInParent(); Transform parentTransform; if (rootCanvas != null) { parentTransform = selectObject.transform; } else { var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)); var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule)); parentTransform = canvas.transform; } gameObject = ObjectFactory.CreateGameObject($"New {typeof(T).Name}", typesArray); gameObject.transform.SetParent(parentTransform, false); } else { var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)); var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule)); canvas.GetComponent().renderMode = RenderMode.ScreenSpaceOverlay; Transform parentTransform = canvas.transform; gameObject = ObjectFactory.CreateGameObject($"New {typeof(T).Name}", typesArray); gameObject.transform.SetParent(parentTransform, false); } gameObject.transform.localPosition = Vector3.zero; gameObject.transform.localScale = new Vector3(1, 1, 1); callBack?.Invoke(gameObject); EditorUtility.FocusProjectWindow(); Selection.activeObject = gameObject; EditorGUIUtility.PingObject(Selection.activeObject); } #endif /// /// 设置物体显示和隐藏 /// public static void SetVisible(this Component component, bool visible, Action callback = null) { SetVisible(component.gameObject, visible, callback); } public static void SetVisible(this GameObject go, bool visible, Action callback = null) { if (!go) { return; } if (go.activeSelf == visible) { return; } if (visible) { go.SetActive(true); callback?.Invoke(); } else { AnimController anim = go.GetComponent(); if (anim) { anim.PlayDisableAnim(() => { callback?.Invoke(); go.SetActive(visible); }); } else { callback?.Invoke(); go.SetActive(visible); } } } } }