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();
}
}
//内部类
class TaskBehavIoUr : MonoBehavIoUr { }
}
测试
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Script_04_15 : MonoBehavIoUr {
void Start () {
//开启定时器
Coroutine coroutine = WaitTimeManager.WaitTime(5f, delegate {
Debug.LogFormat("等待5秒后回调");
});
//等待过程中取消它
//WaitTimeManager.CancelWait (ref coroutine);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。