45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UIDynamicMover : MonoBehaviour
|
|
{
|
|
public RectTransform uiElement; // 要移动的UI元素
|
|
public Transform startPos; // 起始点Transform
|
|
public Transform endPos; // 结束点Transform
|
|
public float duration = 0.5f; // 动画持续时间
|
|
public Button arrBtn;
|
|
private bool isAtEndPosition = false; // 当前是否在结束点位置
|
|
private void Start()
|
|
{
|
|
arrBtn.onClick.AddListener(SwitchPosition);
|
|
}
|
|
public void SwitchPosition()
|
|
{
|
|
// 根据当前位置开始动画
|
|
if (isAtEndPosition)
|
|
{
|
|
arrBtn.transform.GetChild(0).gameObject.SetActive(false);
|
|
arrBtn.transform.GetChild(1).gameObject.SetActive(true);
|
|
uiElement.DOMove(startPos.position, duration).OnComplete(() => isAtEndPosition = false);
|
|
}
|
|
else
|
|
{
|
|
arrBtn.transform.GetChild(0).gameObject.SetActive(true);
|
|
arrBtn.transform.GetChild(1).gameObject.SetActive(false);
|
|
uiElement.DOMove(endPos.position, duration).OnComplete(() => isAtEndPosition = true);
|
|
}
|
|
}
|
|
public void AtEnd()
|
|
{
|
|
arrBtn.transform.GetChild(0).gameObject.SetActive(true);
|
|
arrBtn.transform.GetChild(1).gameObject.SetActive(false);
|
|
uiElement.DOMove(endPos.position, duration).OnComplete(() => isAtEndPosition = true);
|
|
}
|
|
public void AtStart()
|
|
{
|
|
arrBtn.transform.GetChild(0).gameObject.SetActive(false);
|
|
arrBtn.transform.GetChild(1).gameObject.SetActive(true);
|
|
uiElement.DOMove(startPos.position, duration).OnComplete(() => isAtEndPosition = false);
|
|
}
|
|
} |