1. 程式人生 > >Unity時間管理類,用來做一些方法的延遲呼叫

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("<color=#ff0000>無法啟動沒有回撥的timer</color>");
            return;
        }
        if (go == null)
        {
            go = new GameObject("TimerHelper");
            if (dontDestroyOnLoad)
                GameObject.DontDestroyOnLoad(go);
            go.AddComponent<TimerHelper>();
        }
        go.GetComponent<TimerHelper>().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<TimerHelper>().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;
    }
}