197 lines
6.4 KiB
C#
197 lines
6.4 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.Threading.Tasks;
|
||
using UnityEngine.SceneManagement;
|
||
using Unity.VisualScripting;
|
||
public class Starthost : MonoBehaviour
|
||
{
|
||
private GameObject panel;
|
||
private Button posiBtn;//人员到位情况按钮
|
||
private Button start;//开始演练按钮
|
||
public bool isReady;//在线玩家是否准备
|
||
public bool isSatisfy;//人员是否满足
|
||
public bool isStart;//演习是否开始
|
||
private GameObject time;//计时
|
||
private Text timeText;//计时时间
|
||
private bool isTimerRunning=false;//是否开始计时
|
||
private float timer = 0f;
|
||
public float moveDuration = 2f; // 移动时间
|
||
public float scaleDuration = 2f; // 缩放时间
|
||
private RectTransform rectTransform;
|
||
|
||
private Canvas canvas;
|
||
private Panel2 GetPanel2;
|
||
public Alertwindow alertwindow;
|
||
private bool isButtonClicked = false; // 监听的变量
|
||
private GameObject peopleposition;//人员的位置信息
|
||
//非主持人的画面
|
||
public bool ishost=false;
|
||
private Button ready;
|
||
private Image image;
|
||
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);
|
||
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);
|
||
}
|
||
}
|
||
void OnEnable()
|
||
{
|
||
Alertwindow.BoolValueChanged += OnBoolChanged;
|
||
}
|
||
void OnDisable()
|
||
{
|
||
Alertwindow.BoolValueChanged -= OnBoolChanged;
|
||
}
|
||
void OnBoolChanged(bool newBoolValue)
|
||
{
|
||
isButtonClicked=newBoolValue;
|
||
if (isButtonClicked)
|
||
{
|
||
Debug.Log("回到开始界面");
|
||
}
|
||
}
|
||
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);
|
||
}
|
||
|
||
}
|
||
void OnReadyBtn()
|
||
{
|
||
image.gameObject.SetActive(true);
|
||
ready.gameObject.SetActive(false);
|
||
}
|
||
// 停止计时并重置时间显示的方法
|
||
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 );
|
||
//// 给每个按钮添加点击监听事件
|
||
//foreach (Button button in buttons)
|
||
//{
|
||
// button.onClick.AddListener(ShowPopupPanel);
|
||
//}
|
||
Game.uiManager.ShowUI<Image>("Panel");
|
||
}
|
||
// 显示弹出界面的方法
|
||
void ShowPopupPanel()
|
||
{
|
||
Game.uiManager.ShowUI<Image>("Panel");
|
||
}
|
||
//开始进行演习
|
||
void OnClickStartBtn()
|
||
{
|
||
isStart = true;
|
||
if (isSatisfy)
|
||
{
|
||
AnimateUIElementAsync();
|
||
start.interactable = false; // 防止点击事件
|
||
//总指挥接警动画
|
||
SceneManager.LoadScene(5);
|
||
//开始计时
|
||
isTimerRunning = true;
|
||
time.SetActive(true);
|
||
}
|
||
else
|
||
{
|
||
Game.uiManager.ShowUI<Image>("Schedule_host03");
|
||
}
|
||
}
|
||
// 异步动画
|
||
async void AnimateUIElementAsync()
|
||
{
|
||
// 等待两秒
|
||
await Task.Delay(2000);
|
||
// 获取画布的尺寸
|
||
Canvas canvas = GetComponentInParent<Canvas>();
|
||
float canvasHeight = canvas.GetComponent<RectTransform>().rect.height;
|
||
// 初始位置是画布中心
|
||
Vector2 startPosition = rectTransform.anchoredPosition;
|
||
Vector2 targetPosition = new Vector2(startPosition.x, canvasHeight / 2-50); // 顶部位置
|
||
// 缩放动画
|
||
Vector3 startScale = rectTransform.localScale;
|
||
Vector3 targetScale = startScale * 0.5f; // 缩小两倍
|
||
// 同时进行缩放和移动
|
||
float elapsedTime = 0f;
|
||
while (elapsedTime < Mathf.Max(moveDuration, scaleDuration)) // 确保同时进行,选择较长的时间作为循环的上限
|
||
{
|
||
float tScale = Mathf.Clamp01(elapsedTime / scaleDuration); // 计算缩放进度
|
||
float tMove = Mathf.Clamp01(elapsedTime / moveDuration); // 计算移动进度
|
||
// 缩放
|
||
rectTransform.localScale = Vector3.Lerp(startScale, targetScale, tScale);
|
||
// 移动
|
||
rectTransform.anchoredPosition = Vector2.Lerp(startPosition, targetPosition, tMove);
|
||
elapsedTime += Time.deltaTime;
|
||
await Task.Yield(); // 等待到下一帧,类似于协程的yield return null
|
||
}
|
||
// 确保最终达到目标值
|
||
rectTransform.localScale = targetScale;
|
||
rectTransform.anchoredPosition = targetPosition;
|
||
panel.SetActive(false);
|
||
Game.uiManager.ShowUI<Image>("Panel1_2");
|
||
}
|
||
}
|