Unity封裝延時(shí)調(diào)用定時(shí)器
更新時(shí)間:2020年04月18日 08:45:33 作者:林新發(fā)
這篇文章主要為大家詳細(xì)介紹了Unity封裝延時(shí)調(diào)用定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Unity封裝延時(shí)調(diào)用定時(shí)器的具體代碼,供大家參考,具體內(nèi)容如下
封裝一個(gè)延時(shí)調(diào)用定時(shí)器類
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class WaitTimeManager
{
private static TaskBehaviour m_Task;
static WaitTimeManager()
{
GameObject go = new GameObject("#WaitTimeManager#");
GameObject.DontDestroyOnLoad(go);
m_Task = go.AddComponent<TaskBehaviour> ();
}
//等待
static public Coroutine WaitTime(float time,UnityAction callback)
{
return m_Task.StartCoroutine(Coroutine(time,callback));
}
//取消等待
static public void CancelWait(ref Coroutine coroutine)
{
if (coroutine != null) {
m_Task.StopCoroutine(coroutine);
coroutine = null;
}
}
static IEnumerator Coroutine(float time, UnityAction callback) {
yield return new WaitForSeconds (time);
if (callback != null) {
callback();
}
}
//內(nèi)部類
class TaskBehaviour : MonoBehaviour { }
}
測(cè)試
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Script_04_15 : MonoBehaviour {
void Start () {
//開啟定時(shí)器
Coroutine coroutine = WaitTimeManager.WaitTime(5f, delegate {
Debug.LogFormat("等待5秒后回調(diào)");
});
//等待過程中取消它
//WaitTimeManager.CancelWait (ref coroutine);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity3D實(shí)現(xiàn)相機(jī)跟隨控制
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)相機(jī)跟隨控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組
這篇文章主要介紹了深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組,隱式類型的數(shù)組通常與匿名類型以及對(duì)象初始值設(shè)定項(xiàng)和集合初始值設(shè)定項(xiàng)一起使用,需要的朋友可以參考下2016-01-01

