欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > 【unity开发】实用辅助函数:RectTransformUtil

【unity开发】实用辅助函数:RectTransformUtil

2024/10/24 5:21:58 来源:https://blog.csdn.net/qq_57404161/article/details/141202452  浏览:    关键词:【unity开发】实用辅助函数:RectTransformUtil

参考资料

https://docs.unity.cn/cn/2022.3/ScriptReference/RectTransform.html

代码

using System.Collections.Generic;
using UnityEngine;public static class RectTransformUtil
{#region 嵌套类public enum AnchorPresets{BottomLeft, BottomCenter, BottomRight,MiddleLeft, MiddleCenter, MiddleRight,TopLeft, TopCenter, TopRight,StretchLeft, StretchCenter, StretchRight,StretchStretch,BottomStretch, MiddleStretch, TopStretch,TopHalf, BottomHalf, LeftHalf, RightHalf,BottomLeftQuarter, BottomRightQuarter, TopLeftQuarter, TopRightQuarter,}private static Dictionary<AnchorPresets, (Vector2, Vector2)> anchorDic = new(){{AnchorPresets.BottomLeft,          (new(0.0f, 0.0f),   new(0.0f, 0.0f))},{AnchorPresets.BottomCenter,        (new(0.5f, 0.0f),   new(0.5f, 0.0f))},{AnchorPresets.BottomRight,         (new(1.0f, 0.0f),   new(1.0f, 0.0f))},{AnchorPresets.MiddleLeft,          (new(0.0f, 0.5f),   new(0.0f, 0.5f))},{AnchorPresets.MiddleCenter,        (new(0.5f, 0.5f),   new(0.5f, 0.5f))},{AnchorPresets.MiddleRight,         (new(1.0f, 0.5f),   new(1.0f, 0.5f))},{AnchorPresets.TopLeft,             (new(0.0f, 1.0f),   new(0.0f, 1.0f))},{AnchorPresets.TopCenter,           (new(0.5f, 1.0f),   new(0.5f, 1.0f))},{AnchorPresets.TopRight,            (new(1.0f, 1.0f),   new(1.0f, 1.0f))},{AnchorPresets.StretchLeft,         (new(0.0f, 0.0f),   new(0.0f, 1.0f))},{AnchorPresets.StretchCenter,       (new(0.5f, 0.0f),   new(0.5f, 1.0f))},{AnchorPresets.StretchRight,        (new(1.0f, 0.0f),   new(1.0f, 1.0f))},{AnchorPresets.StretchStretch,      (new(0.0f, 0.0f),   new(1.0f, 1.0f))},{AnchorPresets.BottomStretch,       (new(0.0f, 0.0f),   new(1.0f, 0.0f))},{AnchorPresets.MiddleStretch,       (new(0.0f, 0.5f),   new(1.0f, 0.5f))},{AnchorPresets.TopStretch,          (new(0.0f, 1.0f),   new(1.0f, 1.0f))},{AnchorPresets.TopHalf,             (new(0.0f, 0.5f),   new(1.0f, 1.0f))},{AnchorPresets.BottomHalf,          (new(0.0f, 0.0f),   new(1.0f, 0.5f))},{AnchorPresets.LeftHalf,            (new(0.0f, 0.0f),   new(0.5f, 1.0f))},{AnchorPresets.RightHalf,           (new(0.5f, 0.0f),   new(1.0f, 1.0f))},{AnchorPresets.BottomLeftQuarter,   (new(0.0f, 0.0f),   new(0.5f, 0.5f))},{AnchorPresets.BottomRightQuarter,  (new(0.5f, 0.0f),   new(1.0f, 0.5f))},{AnchorPresets.TopLeftQuarter,      (new(0.0f, 0.5f),   new(0.5f, 1.0f))},{AnchorPresets.TopRightQuarter,     (new(0.5f, 0.5f),   new(1.0f, 1.0f))},};#endregion 嵌套类#region setpublic static void SetX(GameObject obj, float x) { SetX(obj.GetComponent<RectTransform>(), x); }public static void SetX(RectTransform rect, float x){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }rect.anchoredPosition = new Vector2(x, rect.anchoredPosition.y);}public static void SetY(GameObject obj, float y) { SetY(obj.GetComponent<RectTransform>(), y); }public static void SetY(RectTransform rect, float y){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, y);}public static void SetXY(GameObject obj, float x, float y) { SetXY(obj.GetComponent<RectTransform>(), x, y); }public static void SetXY(RectTransform rect, float x, float y){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }rect.anchoredPosition = new Vector2(x, y);}public static void SetW(GameObject obj, float width) { SetW(obj.GetComponent<RectTransform>(), width); }public static void SetW(RectTransform rect, float width){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }Vector2 anchoredPosition = rect.anchoredPosition;Vector2 sizeDelta = rect.sizeDelta;if (rect.anchorMin.x == rect.anchorMax.x)sizeDelta.x = width; // 闭合时,sizeDelta.x 的值为当前宽度elsesizeDelta.x += width - rect.rect.width; // 散开时,sizeDelta.x = 当前宽度 - 锚点宽度rect.sizeDelta = sizeDelta;rect.anchoredPosition = anchoredPosition;}public static void SetH(GameObject obj, float height) { SetH(obj.GetComponent<RectTransform>(), height); }public static void SetH(RectTransform rect, float height){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }Vector2 anchoredPosition = rect.anchoredPosition;Vector2 sizeDelta = rect.sizeDelta;if (rect.anchorMin.y == rect.anchorMax.y)sizeDelta.y = height;elsesizeDelta.y += height - rect.rect.height;rect.sizeDelta = sizeDelta;rect.anchoredPosition = anchoredPosition;}public static void SetWH(GameObject obj, float width, float height) { SetWH(obj.GetComponent<RectTransform>(), width, height); }public static void SetWH(RectTransform rect, float width, float height){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }Vector2 anchoredPosition = rect.anchoredPosition;Vector2 sizeDelta = rect.sizeDelta;if (rect.anchorMin.x == rect.anchorMax.x)sizeDelta.x = width;elsesizeDelta.x += width - rect.rect.width;if (rect.anchorMin.y == rect.anchorMax.y)sizeDelta.y = height;elsesizeDelta.y += height - rect.rect.height;rect.sizeDelta = sizeDelta;rect.anchoredPosition = anchoredPosition;}public static void SetAnchor(GameObject obj, AnchorPresets preset) { SetAnchor(obj.GetComponent<RectTransform>(), preset); }public static void SetAnchor(RectTransform rect, AnchorPresets preset){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }(rect.anchorMin, rect.anchorMax) = anchorDic[preset];}public static void SetPivot(GameObject obj, AnchorPresets preset) { SetPivot(obj.GetComponent<RectTransform>(), preset); }public static void SetPivot(RectTransform rect, AnchorPresets preset){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }var (a, b) = anchorDic[preset];rect.pivot = (a + b) / 2;}public static void Reset(GameObject obj) { Reset(obj.GetComponent<RectTransform>()); }public static void Reset(RectTransform rect){if (rect == null) { Debug.LogWarning($"RectTransform 组件为 null"); return; }SetAnchor(rect, AnchorPresets.MiddleCenter);SetPivot(rect, AnchorPresets.MiddleCenter);rect.anchoredPosition3D = Vector3.zero;rect.localScale = Vector3.one;SetWH(rect, 100, 100);}#endregion set
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com