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 序列化字段