Unity計時器功能實現(xiàn)示例
Unity計時器
Demo展示
介紹
游戲中有非常多的計時功能,比如:各種cd,以及需要延時調(diào)用的方法;
一般實現(xiàn)有一下幾種方式:
1.手動計時
float persistTime = 10f float startTime = Time.time; if(Time.time - startTime > persistTime) { Debug.Log("計時結(jié)束"); } float curTime = 0; curTime += Time.deltaTime; if(curTime > persistTime) { Debug.Log("計時結(jié)束"); }
2.協(xié)程
private float persistTime = 10f; IEnumerator DelayFunc() { yield return persistTime; Debug.Log("計時結(jié)束"); } private void Start() { StartCoroutine(DelayFunc()); }
3.Invoke回調(diào)
private void Start() { Invoke("DelayFunc", persistTime); }
計時器功能
計時是為了到特定的時間,執(zhí)行某個功能或方法;
計時器(Timer):設(shè)計了計時暫停,計時重置,計時開始方法,計時中方法,計時結(jié)束方法,固定間隔調(diào)用方法,計時器可設(shè)置復(fù)用或單次;
計時管理器(TimerMa):負責(zé)倒計時,以及執(zhí)行計時器方法;
代碼:
using System; using System.Collections.Generic; using UnityEngine; using Object = System.Object; public class Timer { public delegate void IntervalAct(Object args); //總時間和當(dāng)前持續(xù)時間 private float curtime = 0; private float totalTime = 0; //激活 public bool isActive; //計時結(jié)束是否銷毀 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; //持續(xù)事件 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(); } } //持續(xù)事件 if (onStay != null) onStay(); curtime += Time.deltaTime; //間隔事件 if (onInterval != null) { curInterval += Time.deltaTime; if (curInterval > intervalTime) { onInterval(args); curInterval = 0; } } //計時結(jié)束 if (curtime > totalTime) { curtime = 0; isActive = false; if (onEnd != null) { onEnd(); } } } //設(shè)置間隔事件 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(); //計時結(jié)束,且需要銷毀 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 = "結(jié)束計時"; } private void OnInterval(Object value) { count++; mText2.text = $"間隔事件調(diào)用{count}"; } }
到此這篇關(guān)于Unity計時器功能實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Unity計時器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#條件編譯、內(nèi)聯(lián)函數(shù)、CLS介紹
這篇文章介紹了C#的條件編譯、內(nèi)聯(lián)函數(shù)、CLS,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03C#實現(xiàn)遠程關(guān)閉和重啟計算機的示例代碼
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)遠程關(guān)閉和重啟計算機的功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12