_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Scene_main/mainBTN.cs

86 lines
2.5 KiB
C#
Raw Normal View History

2024-11-25 22:14:18 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2024-11-26 15:33:45 +08:00
using DG.Tweening;
2024-11-25 22:14:18 +08:00
2024-11-26 15:33:45 +08:00
//MD FUCK Everthing
2024-11-25 22:14:18 +08:00
//ʷɽ<CAB7><C9BD><EFBFBD><EFBFBD>ѧ
public class mainBTN : MonoBehaviour
2024-11-25 23:46:35 +08:00
{
public GameObject mainPanel;
2024-11-26 00:39:25 +08:00
private RectTransform mainPanelRect;
2024-11-25 23:46:35 +08:00
2024-11-25 22:14:18 +08:00
public GameObject BTN_0;
public GameObject BTN_1;
public GameObject BTN_2;
public GameObject BTN_3;
public GameObject BTN_0_0;
public GameObject BTN_1_1;
public GameObject BTN_2_2;
public GameObject BTN_3_3;
// Start is called before the first frame update
void Start()
{
2024-11-26 00:39:25 +08:00
// ȷ<><C8B7>mainPanel<65><6C>Ϊ<EFBFBD><CEAA>
if (mainPanel == null)
{
Debug.LogError("mainPanel is not assigned!");
return;
}
2024-11-25 22:14:18 +08:00
2024-11-26 00:39:25 +08:00
mainPanelRect = mainPanel.GetComponent<RectTransform>();
2024-11-25 22:14:18 +08:00
2024-11-26 00:39:25 +08:00
// <20><><EFBFBD>Ӱ<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
BTN_0.GetComponent<Button>().onClick.AddListener(() => MovePanel(0));
BTN_1.GetComponent<Button>().onClick.AddListener(() => MovePanel(-2100));
BTN_2.GetComponent<Button>().onClick.AddListener(() => MovePanel(-3760));
BTN_3.GetComponent<Button>().onClick.AddListener(() => MovePanel(-5350));
}
2024-11-25 22:14:18 +08:00
2024-11-26 00:39:25 +08:00
// ͳһ<CDB3><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
public void MovePanel(float targetX)
{
// <20><><EFBFBD>°<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
BTN_0.SetActive(targetX != 0);
BTN_1.SetActive(targetX != -2100);
BTN_2.SetActive(targetX != -3760);
BTN_3.SetActive(targetX != -5350);
2024-11-25 22:14:18 +08:00
2024-11-26 00:39:25 +08:00
BTN_0_0.SetActive(targetX == 0);
BTN_1_1.SetActive(targetX == -2100);
BTN_2_2.SetActive(targetX == -3760);
BTN_3_3.SetActive(targetX == -5350);
2024-11-25 22:14:18 +08:00
2024-11-26 15:33:45 +08:00
// ʹ<><CAB9> DOTween ʵ<><CAB5>ƽ<EFBFBD><C6BD><EFBFBD>ƶ<EFBFBD>
mainPanelRect.DOKill(); // ɱ<><C9B1>֮ǰ<D6AE>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mainPanelRect.DOAnchorPosX(targetX, 0.3f).SetEase(Ease.Linear);
2024-11-25 22:14:18 +08:00
}
2024-11-26 15:33:45 +08:00
//Э<><D0AD>ʵ<EFBFBD><CAB5>ƽ<EFBFBD><C6BD><EFBFBD>ƶ<EFBFBD>
//web<65><62>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><D8B1>󣬾<EFBFBD><F3A3ACBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-26 00:39:25 +08:00
private IEnumerator SmoothMovePanel(float targetX, float duration)
{
Vector2 startPosition = mainPanelRect.anchoredPosition;
Vector2 targetPosition = new Vector2(targetX, startPosition.y);
float elapsedTime = 0f;
2024-11-25 22:14:18 +08:00
2024-11-26 00:39:25 +08:00
while (elapsedTime < duration)
{
// <20><><EFBFBD>㵱ǰ<E3B5B1><C7B0><EFBFBD><EFBFBD>
float t = elapsedTime / duration;
// <20>𽥸ı<F0BDA5B8> position
mainPanelRect.anchoredPosition = Vector2.Lerp(startPosition, targetPosition, t);
2024-11-25 22:14:18 +08:00
2024-11-26 00:39:25 +08:00
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
elapsedTime += Time.deltaTime;
yield return null;
}
2024-11-25 22:14:18 +08:00
2024-11-26 00:39:25 +08:00
// <20><><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>λ<EFBFBD><CEBB>ΪĿ<CEAA><C4BF>λ<EFBFBD><CEBB>
mainPanelRect.anchoredPosition = targetPosition;
2024-11-25 22:14:18 +08:00
}
2024-11-26 00:39:25 +08:00
2024-11-25 22:14:18 +08:00
}