AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/UI/Selector.cs

74 lines
2.0 KiB
C#
Raw Normal View History

2024-12-04 23:15:05 +08:00
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class Selector : MonoBehaviour
{
2024-12-04 23:15:05 +08:00
[SerializeField]
private Animator animator;
private RectTransform m_rect => transform as RectTransform;
private RectTransform m_target;
private TweenerCore<Vector3, Vector3, VectorOptions> m_trackTween;
public RectTransform Target
{
get => m_target;
set
{
if (m_target == value) return;
m_target = value;
var itemUIRect = m_target.transform as RectTransform;
m_rect.pivot = itemUIRect.pivot;
m_rect.sizeDelta = itemUIRect.rect.size;
m_rect.SetAsLastSibling();
2024-12-04 23:15:05 +08:00
animator.SetTrigger("reactive");
if (m_trackTween != null)
{
m_trackTween.Kill();
m_trackTween = null;
}
m_trackTween = DOTween.To(() => m_rect.position, (value) => m_rect.position = value, itemUIRect.position, 0.125f);
m_trackTween.onComplete = () => m_trackTween = null;
}
}
2024-12-04 23:15:05 +08:00
private bool m_active;
public bool Active
{
get => m_active;
set
{
if (m_active == value) return;
m_active = value;
animator.SetBool("active", value);
}
}
public void RefreshPosition()
{
if (Target != null)
{
m_rect.position = Target.position;
}
}
private void LateUpdate()
{
2024-12-04 23:15:05 +08:00
if (m_trackTween != null && m_trackTween.endValue != Target.position)
{
2024-12-04 23:15:05 +08:00
m_trackTween.ChangeEndValue(Target.position, true);
}
if (Target == null) return;
}
}
}