• WaitTimeManagerDemo


    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 ();
        }

        //等待
        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{}
    }
     

    Demo:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class WaitTimeManagerDemo : MonoBehaviour {
        void Start () {

            //开启
            Coroutine coroutine = WaitTimeManager.WaitTime(5f,delegate {
                Debug.LogFormat("等待5秒后回调");
            });
            Coroutine coroutine2 = WaitTimeManager.WaitTime(2f, delegate {
                Debug.LogFormat("等待2秒后回调");
            });
            Coroutine coroutine3 = WaitTimeManager.WaitTime(3f, delegate {
                Debug.LogFormat("等待3秒后回调");
            });
            Coroutine coroutine4 = WaitTimeManager.WaitTime(4f, () => {
                Debug.LogFormat("等待4秒后回调");
            });     

            //取消
            WaitTimeManager.CancelWait(ref coroutine);

        }
    }
     

  • 相关阅读:
    DeiT:注意力也能蒸馏
    Spring Boot创业众筹网
    20220809模拟
    设计模式-迭代器模式
    JQuery表单验证(validate):常用例子介绍
    MySQL----存储过程
    Vue 双向绑定、diff和nextTick原理
    干货 | 答编辑/审稿人问之样本量大关
    微服务分布式架构中,如何实现日志链路跟踪?
    java计算机毕业设计web企业档案管理系统源码+mysql数据库+系统+lw文档+部署
  • 原文地址:https://blog.csdn.net/racerun/article/details/128100370