45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
|
|
using System;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace AlicizaX.UI
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Transform 位置状态控制
|
||
|
|
/// 控制 RectTransform 的锚点位置
|
||
|
|
/// </summary>
|
||
|
|
[Serializable]
|
||
|
|
[ControlerStateName("Transform/Position")]
|
||
|
|
[ControlerStateAttachType(true)]
|
||
|
|
public class TransformPositionState : ControllerStateBase
|
||
|
|
{
|
||
|
|
[SerializeField] private Vector2 _position;
|
||
|
|
[HideInInspector] [SerializeField] private Vector2 _defaultPosition;
|
||
|
|
|
||
|
|
public override void Init(UXControllerStateRecorder recorder)
|
||
|
|
{
|
||
|
|
if (recorder != null && recorder.TryGetComponent<RectTransform>(out var rect))
|
||
|
|
{
|
||
|
|
_defaultPosition = rect.anchoredPosition;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
||
|
|
{
|
||
|
|
if (recorder != null && recorder.TryGetComponent<RectTransform>(out var rect))
|
||
|
|
{
|
||
|
|
rect.anchoredPosition = (entryIndex == selectionIndex) ? _position : _defaultPosition;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
||
|
|
{
|
||
|
|
return recorder != null && recorder.GetComponent<RectTransform>() != null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override string GetDescription()
|
||
|
|
{
|
||
|
|
return $"匹配时: 位置={_position}, 默认={_defaultPosition}";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|