Unity計(jì)時(shí)器功能實(shí)現(xiàn)示例
Unity計(jì)時(shí)器
Demo展示
介紹
游戲中有非常多的計(jì)時(shí)功能,比如:各種cd,以及需要延時(shí)調(diào)用的方法;
一般實(shí)現(xiàn)有一下幾種方式:
1.手動(dòng)計(jì)時(shí)
float persistTime = 10f float startTime = Time.time; if(Time.time - startTime > persistTime) { Debug.Log("計(jì)時(shí)結(jié)束"); } float curTime = 0; curTime += Time.deltaTime; if(curTime > persistTime) { Debug.Log("計(jì)時(shí)結(jié)束"); }
2.協(xié)程
private float persistTime = 10f; IEnumerator DelayFunc() { yield return persistTime; Debug.Log("計(jì)時(shí)結(jié)束"); } private void Start() { StartCoroutine(DelayFunc()); }
3.Invoke回調(diào)
private void Start() { Invoke("DelayFunc", persistTime); }
計(jì)時(shí)器功能
計(jì)時(shí)是為了到特定的時(shí)間,執(zhí)行某個(gè)功能或方法;
計(jì)時(shí)器(Timer):設(shè)計(jì)了計(jì)時(shí)暫停,計(jì)時(shí)重置,計(jì)時(shí)開(kāi)始方法,計(jì)時(shí)中方法,計(jì)時(shí)結(jié)束方法,固定間隔調(diào)用方法,計(jì)時(shí)器可設(shè)置復(fù)用或單次;
計(jì)時(shí)管理器(TimerMa):負(fù)責(zé)倒計(jì)時(shí),以及執(zhí)行計(jì)時(shí)器方法;
代碼:
using System; using System.Collections.Generic; using UnityEngine; using Object = System.Object; public class Timer { public delegate void IntervalAct(Object args); //總時(shí)間和當(dāng)前持續(xù)時(shí)間 private float curtime = 0; private float totalTime = 0; //激活 public bool isActive; //計(jì)時(shí)結(jié)束是否銷毀 public bool isDestroy; //是否暫停 public bool isPause; //間隔事件和間隔事件——Dot private float intervalTime = 0; private float curInterval = 0; private IntervalAct onInterval; private Object args; //進(jìn)入事件 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() { //暫停計(jì)時(shí) 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; } } //計(jì)時(shí)結(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; } //重置計(jì)時(shí)器 public void Reset() { curtime = 0; isActive = true; isPause = false; curInterval = 0; isOnEnter = false; } //獲取剩余時(shí)間 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(); //計(jì)時(shí)結(jié)束,且需要銷毀 if(!timerList[i].isActive && timerList[i].isDestroy) timerList.RemoveAt(i); else ++i; } } }
測(cè)試計(jì)時(shí)器
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 = "暫停計(jì)時(shí)"; } } if (Input.GetKeyDown(KeyCode.S)) { if (timer.isPause) { timer.isPause = false; mText2.text = "取消暫停計(jì)時(shí)"; } } if (Input.GetKeyDown(KeyCode.D)) { timer.Reset(); mText2.text = "重置計(jì)時(shí)"; } } private void OnStart() { mText2.text = "開(kāi)始計(jì)時(shí)"; } private void OnExit() { mText2.text = "結(jié)束計(jì)時(shí)"; } private void OnInterval(Object value) { count++; mText2.text = $"間隔事件調(diào)用{count}"; } }
到此這篇關(guān)于Unity計(jì)時(shí)器功能實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Unity計(jì)時(shí)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#條件編譯、內(nèi)聯(lián)函數(shù)、CLS介紹
這篇文章介紹了C#的條件編譯、內(nèi)聯(lián)函數(shù)、CLS,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決
這篇文章主要介紹了unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04C#模式畫(huà)刷HatchBrush用法實(shí)例
這篇文章主要介紹了C#模式畫(huà)刷HatchBrush用法,實(shí)例分析了模式畫(huà)刷HatchBrush繪圖的相關(guān)技巧,需要的朋友可以參考下2015-06-06c#編寫(xiě)webservice服務(wù)引用實(shí)例分享
c#編寫(xiě)webservice服務(wù)引用實(shí)例分享,大家參考使用吧2013-12-12C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉和重啟計(jì)算機(jī)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉和重啟計(jì)算機(jī)的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#?WPF后臺(tái)動(dòng)態(tài)添加控件實(shí)戰(zhàn)教程
最近嘗試用wpf在后臺(tái)動(dòng)態(tài)添加控件,所以下面這篇文章主要給大家介紹了關(guān)于C#?WPF后臺(tái)動(dòng)態(tài)添加控件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05WPF實(shí)現(xiàn)文字粒子閃爍動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)文字粒子閃爍動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08