From 57e11f9e51def5ae6289e4a52bc59fc9241e272a Mon Sep 17 00:00:00 2001 From: LiRuoChen <571244399@qq.com> Date: Tue, 21 Jul 2026 19:14:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8XericUIBehaviour=E4=B8=AD=E5=B5=8C?= =?UTF-8?q?=E5=85=A5=E6=96=B0api=E6=8D=A2=E7=AE=97=E3=80=82=E8=BF=98?= =?UTF-8?q?=E6=9C=89=E6=88=91=E5=8D=95=E8=AF=8D=E6=8B=BC=E9=94=99=E4=BA=86?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E4=B8=80=E4=B8=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ericUIBehaciour.cs => XericUIBehaviour.cs} | 91 ++++++++++++++++++- ...ciour.cs.meta => XericUIBehaviour.cs.meta} | 0 .../FlipPage/XericFlipPageIndexVesselBase.cs | 2 +- .../Rendering/Component/XericUIActionTable.cs | 2 +- 4 files changed, 91 insertions(+), 4 deletions(-) rename Runtime/Core/Base/{XericUIBehaciour.cs => XericUIBehaviour.cs} (58%) rename Runtime/Core/Base/{XericUIBehaciour.cs.meta => XericUIBehaviour.cs.meta} (100%) diff --git a/Runtime/Core/Base/XericUIBehaciour.cs b/Runtime/Core/Base/XericUIBehaviour.cs similarity index 58% rename from Runtime/Core/Base/XericUIBehaciour.cs rename to Runtime/Core/Base/XericUIBehaviour.cs index f68fc0e..f352871 100644 --- a/Runtime/Core/Base/XericUIBehaciour.cs +++ b/Runtime/Core/Base/XericUIBehaviour.cs @@ -1,4 +1,4 @@ -using System; +using System; using Deconstruction.Runtime; #if ODIN_INSPECTOR using Sirenix.OdinInspector; @@ -9,7 +9,7 @@ using UnityEngine.UI; namespace XericUI.Core.Base { - public class XericUIBehaciour : UIBehaviour + public class XericUIBehaviour : UIBehaviour { #if ODIN_INSPECTOR [InfoBox("脚本必填属性存在空引用!请检查开放属性后再试", InfoMessageType.Error, VisibleIf = "PublicFieldNullReference")] @@ -147,6 +147,93 @@ namespace XericUI.Core.Base #endregion + #region 锚点设置 + + /// + /// 设置锚点并保持视觉位置和尺寸不变(补偿偏移量抵消锚点变化带来的位移) + /// + protected void SetAnchor(Vector2 anchorMin, Vector2 anchorMax) + { + var rt = rectTransform; + if (rt == null) return; + + var parentRt = rt.parent != null ? rt.parent as RectTransform : null; + if (parentRt == null) + { + rt.anchorMin = anchorMin; + rt.anchorMax = anchorMax; + return; + } + + Vector2 parentSize = parentRt.rect.size; + + // 计算当前在父空间中的实际边界 + float left = parentSize.x * rt.anchorMin.x + rt.offsetMin.x; + float right = parentSize.x * rt.anchorMax.x + rt.offsetMax.x; + float bottom = parentSize.y * rt.anchorMin.y + rt.offsetMin.y; + float top = parentSize.y * rt.anchorMax.y + rt.offsetMax.y; + + // 设置新锚点 + rt.anchorMin = anchorMin; + rt.anchorMax = anchorMax; + + // 补偿偏移量以保持视觉位置和尺寸不变 + rt.offsetMin = new Vector2( + left - parentSize.x * anchorMin.x, + bottom - parentSize.y * anchorMin.y + ); + rt.offsetMax = new Vector2( + right - parentSize.x * anchorMax.x, + top - parentSize.y * anchorMax.y + ); + } + + /// + /// 设置锚点,通过参数控制是否保持位置 + /// + /// true=保持位置和尺寸不变(同两参数版本);false=位置被动改变,仅保持尺寸 + protected void SetAnchor(Vector2 anchorMin, Vector2 anchorMax, bool preservePosition) + { + if (preservePosition) + { + SetAnchor(anchorMin, anchorMax); + } + else + { + var rt = rectTransform; + if (rt == null) return; + rt.anchorMin = anchorMin; + rt.anchorMax = anchorMax; + } + } + + /// + /// 设置锚点和anchoredPosition,保持尺寸不变 + /// + protected void SetAnchor(Vector2 anchorMin, Vector2 anchorMax, Vector2 anchoredPosition) + { + var rt = rectTransform; + if (rt == null) return; + rt.anchorMin = anchorMin; + rt.anchorMax = anchorMax; + rt.anchoredPosition = anchoredPosition; + } + + /// + /// 设置锚点、anchoredPosition和sizeDelta + /// + protected void SetAnchor(Vector2 anchorMin, Vector2 anchorMax, Vector2 anchoredPosition, Vector2 sizeDelta) + { + var rt = rectTransform; + if (rt == null) return; + rt.anchorMin = anchorMin; + rt.anchorMax = anchorMax; + rt.anchoredPosition = anchoredPosition; + rt.sizeDelta = sizeDelta; + } + + #endregion + #region 显影 public virtual void Show() diff --git a/Runtime/Core/Base/XericUIBehaciour.cs.meta b/Runtime/Core/Base/XericUIBehaviour.cs.meta similarity index 100% rename from Runtime/Core/Base/XericUIBehaciour.cs.meta rename to Runtime/Core/Base/XericUIBehaviour.cs.meta diff --git a/Runtime/FlipPage/XericFlipPageIndexVesselBase.cs b/Runtime/FlipPage/XericFlipPageIndexVesselBase.cs index b521956..a3ac416 100644 --- a/Runtime/FlipPage/XericFlipPageIndexVesselBase.cs +++ b/Runtime/FlipPage/XericFlipPageIndexVesselBase.cs @@ -36,7 +36,7 @@ namespace XericUI.FlipPage /// ÔÚҳÃæÊý¾ÝÖÐpageNumber¿ÉÒԻñȡ×ԼºËùÔڵÄҳÊý£¬itemInfos»ñȡҳÃæÖеÄËùÓÐÊý¾ÝÏîĿ¡£ /// [AddComponentMenu("Xeric UI Vessel/FlipPage/Basic FilpPage Database")] - public class XericFlipPageIndexVesselBase : XericUIBehaciour + public class XericFlipPageIndexVesselBase : XericUIBehaviour { #region É趨²ÎÊý diff --git a/Runtime/XTable/Rendering/Component/XericUIActionTable.cs b/Runtime/XTable/Rendering/Component/XericUIActionTable.cs index 5f49cc1..ebb8ed7 100644 --- a/Runtime/XTable/Rendering/Component/XericUIActionTable.cs +++ b/Runtime/XTable/Rendering/Component/XericUIActionTable.cs @@ -23,7 +23,7 @@ namespace XericUI.XTable.Rendering.Component /// [ExecuteAlways] [AddComponentMenu("Xeric UI Vessel/Table/Xeric UI Action Table", 60)] - public partial class XericUIActionTable : XericUIBehaciour, IPointerClickHandler + public partial class XericUIActionTable : XericUIBehaviour, IPointerClickHandler { #region 序列化字段