45 lines
1.6 KiB
C#
Raw Normal View History

2025-09-08 14:51:28 +08:00
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
public class UIDynamicMover : MonoBehaviour
{
public RectTransform uiElement; // Ҫ<>ƶ<EFBFBD><C6B6><EFBFBD>UIԪ<49><D4AA>
public Transform startPos; // <20><>ʼ<EFBFBD><CABC>Transform
public Transform endPos; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Transform
public float duration = 0.5f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
public Button arrBtn;
private bool isAtEndPosition = false; // <20><>ǰ<EFBFBD>Ƿ<EFBFBD><C7B7>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
private void Start()
{
arrBtn.onClick.AddListener(SwitchPosition);
}
public void SwitchPosition()
{
// <20><><EFBFBD>ݵ<EFBFBD>ǰλ<C7B0>ÿ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
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);
}
}