_xiaofang/xiaofang/Assets/Res/gsj/scripts/Starthost.cs

197 lines
6.4 KiB
C#
Raw Normal View History

2024-12-20 10:10:19 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;
using UnityEngine.SceneManagement;
2024-12-23 14:46:56 +08:00
using Unity.VisualScripting;
2024-12-20 10:10:19 +08:00
public class Starthost : MonoBehaviour
{
private GameObject panel;
private Button posiBtn;//<2F><>Ա<EFBFBD><D4B1>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
private Button start;//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
public bool isReady;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>׼<EFBFBD><D7BC>
public bool isSatisfy;//<2F><>Ա<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
public bool isStart;//<2F><>ϰ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ʼ
private GameObject time;//<2F><>ʱ
private Text timeText;//<2F><>ʱʱ<CAB1><CAB1>
private bool isTimerRunning=false;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ʼ<EFBFBD><CABC>ʱ
private float timer = 0f;
public float moveDuration = 2f; // <20>ƶ<EFBFBD>ʱ<EFBFBD><CAB1>
public float scaleDuration = 2f; // <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
private RectTransform rectTransform;
private Canvas canvas;
private Panel2 GetPanel2;
public Alertwindow alertwindow;
private bool isButtonClicked = false; // <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
2024-12-23 14:46:56 +08:00
private GameObject peopleposition;//<2F><>Ա<EFBFBD><D4B1>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>Ϣ
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵Ļ<CBB5><C4BB><EFBFBD>
public bool ishost=false;
private Button ready;
private Image image;
2024-12-20 10:10:19 +08:00
void Start()
{
panel = transform.Find("Panel1").gameObject;
posiBtn = transform.Find("Panel1/right/under/btnRenYuanDaoWei").GetComponent<Button>();
start = transform.Find("Panel1/right/under/btnRenYanLianStart").GetComponent<Button>();
posiBtn.onClick.AddListener(OnClickPosiBtn);
start.onClick.AddListener(OnClickStartBtn);
time = transform.Find("time").gameObject;
timeText = transform.Find("time/txtCountdown").GetComponent<Text>();
timeText.text = "00:00";
rectTransform = time.GetComponent<RectTransform>();
canvas = time.GetComponentInParent<Canvas>();
time.gameObject.SetActive(false);
2024-12-23 14:46:56 +08:00
peopleposition = GameObject.Find("peopleposition").gameObject;
ready = transform.Find("Panel1/right/under/Ready").GetComponent<Button>();
image= transform.Find("Panel1/right/under/Image").GetComponent<Image>();
ready.gameObject.SetActive(false);
image.gameObject.SetActive(false);
if (!ishost)
{
ready.gameObject.SetActive(true);
posiBtn.gameObject.SetActive(false);
start.gameObject.SetActive(false);
ready.onClick.AddListener(OnReadyBtn);
}
else
{
ready.gameObject.SetActive(false);
posiBtn.gameObject.SetActive(true);
start.gameObject.SetActive(true);
}
2024-12-20 10:10:19 +08:00
}
void OnEnable()
{
Alertwindow.BoolValueChanged += OnBoolChanged;
}
void OnDisable()
{
Alertwindow.BoolValueChanged -= OnBoolChanged;
}
void OnBoolChanged(bool newBoolValue)
{
isButtonClicked=newBoolValue;
if (isButtonClicked)
{
Debug.Log("<22>ص<EFBFBD><D8B5><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>");
}
}
void Update()
{
if(!isReady)
{
start.interactable = false;
}
else
{
start.interactable = true;
}
if (isTimerRunning)
{
timer += Time.deltaTime;
int minutes = Mathf.FloorToInt(timer / 60f);
int seconds = Mathf.FloorToInt(timer % 60f);
string formattedTime = string.Format("{0:00}:{1:00}", minutes, seconds);
timeText.text = formattedTime;
}
if (isButtonClicked)
{
panel.SetActive(true);
start.interactable = true;
rectTransform.position = new Vector2(960, 540);
StopTimer();
rectTransform.localScale = new Vector3(1,1,1);
}
2024-12-23 14:46:56 +08:00
}
void OnReadyBtn()
{
image.gameObject.SetActive(true);
ready.gameObject.SetActive(false);
2024-12-20 10:10:19 +08:00
}
// ֹͣ<CDA3><D6B9>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʾ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
public void StopTimer()
{
isTimerRunning = false;
timer = 0f;
if (timeText != null)
{
timeText.text = "00:00";
}
isButtonClicked = false;
time.SetActive(false);
//peopleposition.SetActive(false);
}
void OnClickPosiBtn()
{
//peopleposition.SetActive(true );
//// <20><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
//foreach (Button button in buttons)
//{
// button.onClick.AddListener(ShowPopupPanel);
//}
Game.uiManager.ShowUI<Image>("Panel");
}
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
void ShowPopupPanel()
{
Game.uiManager.ShowUI<Image>("Panel");
}
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϰ
void OnClickStartBtn()
{
isStart = true;
if (isSatisfy)
{
AnimateUIElementAsync();
start.interactable = false; // <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
//<2F><>ָ<EFBFBD>ӽӾ<D3BD><D3BE><EFBFBD><EFBFBD><EFBFBD>
//<2F><>ʼ<EFBFBD><CABC>ʱ
isTimerRunning = true;
time.SetActive(true);
}
else
{
Game.uiManager.ShowUI<Image>("Schedule_host03");
}
}
// <20><EFBFBD><ECB2BD><EFBFBD><EFBFBD>
async void AnimateUIElementAsync()
{
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
await Task.Delay(2000);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ijߴ<C4B3>
Canvas canvas = GetComponentInParent<Canvas>();
float canvasHeight = canvas.GetComponent<RectTransform>().rect.height;
// <20><>ʼλ<CABC><CEBB><EFBFBD>ǻ<EFBFBD><C7BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Vector2 startPosition = rectTransform.anchoredPosition;
Vector2 targetPosition = new Vector2(startPosition.x, canvasHeight / 2-50); // <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
// <20><><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD>
Vector3 startScale = rectTransform.localScale;
Vector3 targetScale = startScale * 0.5f; // <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
// ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><C5BA>ƶ<EFBFBD>
float elapsedTime = 0f;
while (elapsedTime < Mathf.Max(moveDuration, scaleDuration)) // ȷ<><C8B7>ͬʱ<CDAC><CAB1><EFBFBD>У<EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>ϳ<EFBFBD><CFB3><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϊѭ<CEAA><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
float tScale = Mathf.Clamp01(elapsedTime / scaleDuration); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD><C5BD><EFBFBD>
float tMove = Mathf.Clamp01(elapsedTime / moveDuration); // <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD>
rectTransform.localScale = Vector3.Lerp(startScale, targetScale, tScale);
// <20>ƶ<EFBFBD>
rectTransform.anchoredPosition = Vector2.Lerp(startPosition, targetPosition, tMove);
elapsedTime += Time.deltaTime;
await Task.Yield(); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD>yield return null
}
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD>մﵽĿ<EFB5BD><C4BF>ֵ
rectTransform.localScale = targetScale;
rectTransform.anchoredPosition = targetPosition;
panel.SetActive(false);
Game.uiManager.ShowUI<Image>("Panel1_2");
}
}