unity 計時管理

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

public class TimerManager
{
    private static Dictionary<string, TimerItem> dictList = new Dictionary<string, TimerItem>();

    /// <summary>
    /// 註冊計時
    /// </summary>
    /// <param name="timerKey">Timer key.</param>
    /// <param name="totalNum">Total number.</param>
    /// <param name="delayTime">Delay time.</param>
    /// <param name="callback">Callback.</param>
    /// <param name="endCallback">End callback.</param>
    public static void Register(string timerKey, int totalNum, float delayTime, Action<int> callback, Action endCallback)
    {
        TimerItem timerItem = null;
        if(!dictList.ContainsKey(timerKey))
        {
            GameObject objectItem = new GameObject ();
            objectItem.name = timerKey;

            timerItem = objectItem.AddComponent<TimerItem> ();
            dictList.Add(timerKey, timerItem);
        }else
        {
            timerItem = dictList[timerKey];
        }

        if(timerItem != null)
        {
            timerItem.Run(totalNum, delayTime, callback, endCallback);
        }
    }

    /// <summary>
    /// 取消註冊計時
    /// </summary>
    /// <param name="timerKey">Timer key.</param>
    public static void UnRegister(string timerKey)
    {
        if(!dictList.ContainsKey(timerKey)) return;

        TimerItem timerItem = dictList [timerKey];
        dictList.Remove(timerKey);
        if(timerItem != null)
        {
            timerItem.Stop ();
            GameObject.Destroy(timerItem.gameObject);
        }
    }
}

class TimerItem : MonoBehaviour
{
    private int totalNum;
    private float delayTime;
    private Action<int> callback;
    private Action endCallback;

    private int currentIndex;

    public void Run(int totalNum, float delayTime, Action<int> callback, Action endCallback)
    {
        this.Stop ();

        this.currentIndex = 0;

        this.totalNum = totalNum;
        this.delayTime = delayTime;
        this.callback = callback;
        this.endCallback = endCallback;

        this.StartCoroutine ("EnumeratorAction");
    }

    public void Stop()
    {
        this.StopCoroutine ("EnumeratorAction");
    }

    private IEnumerator EnumeratorAction()
    {
        yield return new WaitForSeconds (this.delayTime);

        this.currentIndex ++;
        if(this.callback != null) this.callback(this.currentIndex);

        if(this.totalNum != -1)
        {
            if(this.currentIndex >= this.totalNum)
            {
                if(this.endCallback != null) this.endCallback();
                yield break;
            }
        }
        this.StartCoroutine("EnumeratorAction");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章