Unity时间管理类,用来做一些方法的延迟调用

using UnityEngine;
using System.Collections;
using System;
public class Timer
{//MonoBehaviour
    public class TimerHelper : MonoBehaviour
    {
        private Action callBackAction;
        private Action loseDelegate;
        private void run()
        {
            callBackAction();
        }
        void OnDisable()
        {
            CancelInvoke("run");
        }
        public void Init(Action callBackAction, Action loseDelegate, float delay)
        {
            this.callBackAction = callBackAction;
            this.loseDelegate = loseDelegate;
            CancelInvoke("run");
            InvokeRepeating("run", (float)delay / 1000, (float)delay / 1000);
        }
        //以后要改成这个
        private IEnumerator Run(Action callBackAction, float delay)
        {
            while (true)
            {
                yield return new WaitForSeconds(delay / 1000);
                if (callBackAction != null)
                {
                    callBackAction();
                }
                else
                {
                    loseDelegate();
                }
            }
        }
    }

    public delegate void TimerEnentHandle(TimerEvent timerEvent);
    public event TimerEnentHandle onTimer;
    public string name = "noName";
    private int delay_original;
    private int repeatNumber_original;
    private int m_delay;//间隔
    private int m_totalCount = 0;//最大重复次数
    private int m_currentCount = 0;//运行次数
    private bool dontDestroyOnLoad;//切场景是否清除

    private GameObject go;
    //delay 间隔时间(毫秒) totalCount 次数
    public Timer(int millisecond, int totalCount = 0, bool dontDestroyOnLoad = false)
    {
        //mono = new MonoBehaviour ();
        this.delay = millisecond;
        this.totalCount = totalCount < 0 ? 0 : totalCount;
        this.dontDestroyOnLoad = dontDestroyOnLoad;
    }

    public void start(bool runOnce = false)
    {
        //InvokeRepeating("SendMsg", 2 , 3); 
        //CancelInvoke("SendMsg");
        //这个方法的意思是指:2 秒后调用 SendMsg() 方法,并且之后每隔 3 秒调用一次 SendMsg () 方法
        if (onTimer == null)
        {
            Debug.Log("无法启动没有回调的timer");
            return;
        }
        if (go == null)
        {
            go = new GameObject("TimerHelper");
            if (dontDestroyOnLoad)
                GameObject.DontDestroyOnLoad(go);
            go.AddComponent();
        }
        go.GetComponent().Init(run, stop, delay);//把stop传进去,当代理丢失的时候会自定停止
        if (runOnce)
        {
            run();
        }
    }
    public void stop()
    {
        if (go != null)
        {
            GameObject.Destroy(go);
            go = null;
        }
    }
    public void reset()
    {
        m_currentCount = 0;//重置次数
        if (go != null)
        {//重置当前延时
            go.GetComponent().Init(run, stop, delay);
        }
    }
    private void run()
    {
        if (m_totalCount == 0 || m_currentCount < m_totalCount)
        {
            m_currentCount++;
            if (m_totalCount != 0 && m_currentCount >= m_totalCount)
            {
                stop();
            }
            if (onTimer != null)
            {
                onTimer(new TimerEvent(this));
            }
        }
        else
        {
            stop();
        }
    }
    //间隔时间(毫秒)
    public int delay
    {
        set
        {
            if (value <= 0)
            {
                Debug.LogError("Timer间隔设置错误");
            }
            m_delay = value;
        }
        get
        {
            return m_delay;
        }
    }
    //最大重复次数
    public int totalCount
    {
        set
        {
            m_totalCount = value > 0 ? value : 0;
        }
        get
        {
            return m_totalCount;
        }
    }
    //当前重复次数
    public int currentCount
    {
        get
        {
            return m_currentCount;
        }
    }
    //当前是否处于运行状态
    public bool isRuning
    {
        get
        {
            return go != null;
        }
    }
}
public class TimerEvent
{
    public Timer timer;
    public TimerEvent(Timer timer)
    {
        this.timer = timer;
    }
}


 
 

你可能感兴趣的:(Unity)