using UnityEngine;
using System.Collections;
/// <summary>
/// 时间管理例,实现游戏暂停和继续、缩放
/// </summary>
public class GameTimeManage : MonoBehaviour {
private static GameTimeManage instance;
void Awake () {
instance = this;
}
public static GameTimeManage GetInstance(){
return instance;
}
/// <summary>
/// 暂停游戏
/// </summary>
public void Pause(){
Time.timeScale = 0;
}
/// <summary>
/// 重新开始游戏
/// </summary>
public void Resume(){
Time.timeScale = 1;
}
public delegate void CountDownCallBack(int countDownTime);
public CountDownCallBack timeCountDownCallBack;
private bool ignoreTimeScale = false;
private int countTime = 0;
private float timeinterval = 0;
private bool isStartCountDown = false;
public void StartCountDown(bool ignoreTimeScale,int countTime){
this.ignoreTimeScale = ignoreTimeScale;
this.countTime = countTime;
timeinterval = 1;
isStartCountDown = true;
}
float currentDeltaTime = 0f;
void Update(){
if(isStartCountDown){
currentDeltaTime = ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
timeinterval -= currentDeltaTime;
if(timeinterval <= 0){
countTime--;
if (countTime == 0) {
TimeCountDown(0);
isStartCountDown = false;
} else {
TimeCountDown(countTime);
timeinterval = 1;
}
}
}
}
void TimeCountDown(int time){
if (timeCountDownCallBack != null) {
timeCountDownCallBack (time);
}
}
}
时间管理类,轻松实现倒计时
最新推荐文章于 2021-11-12 07:32:12 发布