powerfun-unity/Assets/Scripts/UI/Prefab/Panel/GameRoomCountDownController.cs

59 lines
1.4 KiB
C#
Raw Normal View History

2022-08-05 15:10:09 +08:00
using Assets.Scenes.Ride.Scripts;
using Assets.Scripts.Apis.Models;
2022-05-10 19:24:07 +08:00
using System;
using UnityEngine;
using UnityEngine.UI;
public class GameRoomCountDownController : PFUIPanel
{
public Text count;
2022-08-05 15:10:09 +08:00
public Text endCount;
2022-05-10 19:24:07 +08:00
private int Seconds { get; set; }
2023-03-10 14:22:41 +08:00
2022-05-10 19:24:07 +08:00
public GameRoomModel GameRoom { get; set; }
private Action Callback { get; set; }
2023-03-10 14:22:41 +08:00
private bool simple { get; set; }
public void Init(int seconds,Action action,bool isEnd = false,bool simple = false)
2022-05-10 19:24:07 +08:00
{
2023-03-10 14:22:41 +08:00
_stopped = false;
2022-05-10 19:24:07 +08:00
Seconds = seconds;
Callback = action;
2023-03-10 14:22:41 +08:00
this.simple = simple;
2023-02-20 17:32:24 +08:00
endCount.gameObject.SetActive(false);
2022-05-10 19:24:07 +08:00
}
2023-03-10 14:22:41 +08:00
private bool _stopped;
2023-02-20 17:32:24 +08:00
public void Stop()
{
2023-03-10 14:22:41 +08:00
this._stopped = true;
2023-02-20 17:32:24 +08:00
}
2022-08-17 18:28:28 +08:00
2023-03-10 14:22:41 +08:00
private float _timer = 0f;
2022-05-10 19:24:07 +08:00
private void Update()
{
2023-03-10 14:22:41 +08:00
_timer -= Time.deltaTime;
while (_timer < 0)
2022-05-10 19:24:07 +08:00
{
if (Seconds == 0)
{
Callback?.Invoke();
}
2023-03-10 14:22:41 +08:00
if (Seconds <= 0 || _stopped)
2022-05-10 19:24:07 +08:00
{
gameObject.SetActive(false);
}
2023-03-10 14:22:41 +08:00
count.gameObject.SetActive(Seconds >0);
count.text = simple ? Seconds.ToString() : Helper.FormatTicks(Seconds);
endCount.text = Helper.FormatTicks(Seconds);
2022-05-10 19:24:07 +08:00
Seconds--;
2023-03-10 14:22:41 +08:00
_timer += 1f;
2022-05-10 19:24:07 +08:00
}
}
}