IndieGame/client/Assets/Ether/Scripts/Module/Extension/Common/CommonExtension.cs

216 lines
7.4 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00
/********************************************************************
: 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;
2024-10-31 10:58:14 +08:00
#if UNITY_EDITOR
using UnityEditor;
#endif
2024-10-11 10:12:15 +08:00
using UnityEngine;
2024-10-31 10:58:14 +08:00
using UnityEngine.UI;
2024-10-11 10:12:15 +08:00
namespace Ether
{
public static class CommonExtension
{
#region UI自适应
#region
/// <summary>
/// Text根据内容适配大小
/// </summary>
/// <param name="text"></param>
public static void SetAutoSize(this TextMeshProUGUI text, float addWidth = 0, float addHeight = 0)
{
RectTransform rect = text.GetComponent<RectTransform>();
// 动态设置width
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, text.preferredWidth + addWidth);
// 动态设置height
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight + addHeight);
}
/// <summary>
/// Text根据内容适配高度
/// </summary>
/// <param name="text"></param>
public static void SetAutoHeight(this TextMeshProUGUI text, float addSize = 0)
{
RectTransform rect = text.GetComponent<RectTransform>();
// 获取Text的Size
Vector2 rectSize = rect.rect.size;
// width不变
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rectSize.x);
// 动态设置height
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight + addSize);
}
/// <summary>
/// Text根据内容适配宽度
/// </summary>
/// <param name="text"></param>
public static void SetAutoWidth(this TextMeshProUGUI text, float addSize = 0)
{
RectTransform rect = text.GetComponent<RectTransform>();
// 获取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<RectTransform>();
//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<RectTransform>();
//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<RectTransform>();
//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<RectTransform>();
return rect.rect.size;
}
#endregion
#endregion
2024-10-31 10:58:14 +08:00
#if UNITY_EDITOR
public static void CreateComponent<T>(GameObject selectObject, Action<GameObject> 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<Canvas>();
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<Canvas>().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
2024-10-11 10:12:15 +08:00
/// <summary>
/// 设置物体显示和隐藏
/// </summary>
2024-10-31 10:58:14 +08:00
public static void SetVisible(this Component component, bool visible, Action callback = null)
{
SetVisible(component.gameObject, visible, callback);
}
2024-10-11 10:12:15 +08:00
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<AnimController>();
2024-10-31 10:58:14 +08:00
if (anim)
2024-10-11 10:12:15 +08:00
{
anim.PlayDisableAnim(() =>
{
callback?.Invoke();
go.SetActive(visible);
});
}
else
{
callback?.Invoke();
go.SetActive(visible);
}
}
}
}
}