範例-動態新增/移除事件

using UnityEngine;

public class Timer : MonoBehaviour
{
    ITimer _timer;

    void Start()
    {
        _timer = TimerEx.Countdown(60) // 60秒倒數計時器
            .On1Second(OnOneSecond) // 每秒回呼
            .OnInterval(On30Second) // 每30秒回呼
            .OnCompleted(OnComplete) // 計時器數完後回呼
            .Start();
    }

    void OnOneSecond(float time)
    {
        Debug.Log("剩餘秒數:" + time);

        if (time == 45)
        {
            // 剩餘45秒鐘時, 不再接這個倒數事件 (但計時器仍會持續倒數)
            _timer.RemoveEvent(OnSecond);
        }
    }

    void On30Second(float time)
    {
        Debug.Log("剩餘秒數:" + time);

        // 剩餘30秒鐘時, 再接回OnOneSecond()的回呼
        _timer.RemoveEvent(On30Second);
        _timer.On1Second(OnOneSecond);
    }

    void OnComplete(float t)
    {
        Debug.Log("計時器已結束");
    }

}

results matching ""

    No results matching ""