_xiaofang/xiaofang/Assets/Script/hylScripts/Free01/JoyStickController.cs
2024-12-11 11:10:48 +08:00

79 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class JoyStickController : MonoBehaviour
{
public GameObject rightJoystick_obj;
public Button rightremote;
public GameObject FixedJoystick_obj;
public GameObject lockedFixedJoystick_obj;
public Text timerText; // 用于显示倒计时的UI文本
public float countdownTime = 30f; // 设置倒计时30秒
private float timeRemaining;
private bool isCountingDown = false;
void Start()
{
FixedJoystick_obj.gameObject.SetActive(false);//在游戏开始时把移动控制摇杆关闭显示
timeRemaining = countdownTime;
rightJoystick_obj.gameObject.SetActive(false);
rightremote.onClick.AddListener(OnClickRightRemote);
isCountingDown = true;
}
void Update()
{
if (isCountingDown)
{
// 倒计时
timeRemaining -= Time.deltaTime;
// 时间到达0时停止计时并触发倒计时结束的操作
if (timeRemaining <= 0)
{
timeRemaining = 0;
isCountingDown = false;
OnCountdownEnd(); // 倒计时结束时触发的事件
}
// 更新时间显示
UpdateTimerDisplay();
}
}
public void OnClickRightRemote()
{
rightJoystick_obj.gameObject.SetActive(true);
rightremote.gameObject.SetActive(false);
}
// 更新倒计时UI显示
private void UpdateTimerDisplay()
{
int minutes = Mathf.FloorToInt(timeRemaining / 60);
int seconds = Mathf.FloorToInt(timeRemaining % 60);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
// 倒计时结束时的事件
private void OnCountdownEnd()
{
FixedJoystick_obj.gameObject.SetActive(true);
lockedFixedJoystick_obj.gameObject.SetActive(false);
}
// 重置倒计时
public void ResetCountdown()
{
timeRemaining = countdownTime;
isCountingDown = true;
}
}