Unity計時器
Demo展示
介紹
遊戲中有非常多的計時功能,比如:各種cd,以及需要延時呼叫的方法;
一般實現有一下幾種方式:
1.手動計時
float persistTime = 10f
float startTime = Time.time;
if(Time.time - startTime > persistTime)
{
Debug.Log("計時結束");
}
float curTime = 0;
curTime += Time.deltaTime;
if(curTime > persistTime)
{
Debug.Log("計時結束");
}
2.協程
private float persistTime = 10f;
IEnumerator DelayFunc()
{
yield return persistTime;
Debug.Log("計時結束");
}
private void Start()
{
StartCoroutine(DelayFunc());
}
3.Invoke回撥
private void Start()
{
Invoke("DelayFunc", persistTime);
}
計時器功能
計時是為了到特定的時間,執行某個功能或方法;
計時器(Timer):設計了計時暫停,計時重置,計時開始方法,計時中方法,計時結束方法,固定間隔呼叫方法,計時器可設定複用或單次;
計時管理器(TimerMa):負責倒數計時,以及執行計時器方法;
程式碼:
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = System.Object;
public class Timer
{
public delegate void IntervalAct(Object args);
//總時間和當前持續時間
private float curtime = 0;
private float totalTime = 0;
//啟用
public bool isActive;
//計時結束是否銷燬
public bool isDestroy;
//是否暫停
public bool isPause;
//間隔事件和間隔事件——Dot
private float intervalTime = 0;
private float curInterval = 0;
private IntervalAct onInterval;
private Object args;
//進入事件
public Action onEnter;
private bool isOnEnter = false;
//持續事件
public Action onStay;
//退出事件
public Action onEnd;
public Timer(float totalTime, bool isDestroy = true, bool isPause = false)
{
curtime = 0;
this.totalTime = totalTime;
isActive = true;
this.isDestroy = isDestroy;
this.isPause = isPause;
TimerMa.I.AddTimer(this);
}
public void Run()
{
//暫停計時
if (isPause || !isActive)
return;
if (onEnter != null)
{
if (!isOnEnter)
{
isOnEnter = true;
onEnter();
}
}
//持續事件
if (onStay != null)
onStay();
curtime += Time.deltaTime;
//間隔事件
if (onInterval != null)
{
curInterval += Time.deltaTime;
if (curInterval > intervalTime)
{
onInterval(args);
curInterval = 0;
}
}
//計時結束
if (curtime > totalTime)
{
curtime = 0;
isActive = false;
if (onEnd != null)
{
onEnd();
}
}
}
//設定間隔事件
public void SetInterval(float interval, IntervalAct intervalFunc, Object args = null)
{
this.intervalTime = interval;
onInterval = intervalFunc;
curInterval = 0;
this.args = args;
}
//重置計時器
public void Reset()
{
curtime = 0;
isActive = true;
isPause = false;
curInterval = 0;
isOnEnter = false;
}
//獲取剩餘時間
public float GetRemainTime()
{
return totalTime - curtime;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimerMa : MonoBehaviour
{
#region 單例
private static TimerMa instance;
TimerMa() {}
public static TimerMa I
{
get
{
if (instance == null)
instance = new TimerMa();
return instance;
}
}
#endregion
private List<Timer> timerList;
private void Awake()
{
instance = this;
timerList = new List<Timer>();
}
public void AddTimer(Timer t)
{
timerList.Add(t);
}
void Update()
{
for (int i = 0; i < timerList.Count;)
{
timerList[i].Run();
//計時結束,且需要銷燬
if(!timerList[i].isActive && timerList[i].isDestroy)
timerList.RemoveAt(i);
else
++i;
}
}
}
測試計時器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Object = System.Object;
public class Test : MonoBehaviour
{
public Text mText1;
public Text mText2;
private Timer timer;
private int count = 0;
void Start()
{
timer = new Timer(5f,false);
timer.SetInterval(1f, OnInterval);
timer.onEnter = OnStart;
timer.onEnd = OnExit;
}
void Update()
{
Debug.Log(count);
mText1.text = timer.GetRemainTime().ToString("f2");
if (Input.GetKeyDown(KeyCode.A))
{
if (!timer.isPause)
{
timer.isPause = true;
mText2.text = "暫停計時";
}
}
if (Input.GetKeyDown(KeyCode.S))
{
if (timer.isPause)
{
timer.isPause = false;
mText2.text = "取消暫停計時";
}
}
if (Input.GetKeyDown(KeyCode.D))
{
timer.Reset();
mText2.text = "重置計時";
}
}
private void OnStart()
{
mText2.text = "開始計時";
}
private void OnExit()
{
mText2.text = "結束計時";
}
private void OnInterval(Object value)
{
count++;
mText2.text = $"間隔事件呼叫{count}";
}
}