定时器的实现

文章介绍了在Unity中,由于协程的限制和定时器操作的重要性,如何通过自定义`TimeManager`和`TimeBehaviour`类实现两种类型的定时器,包括无参数和带参数的定时器,并提供了开启和关闭定时器的方法。
摘要由CSDN通过智能技术生成

定时器的目的:

        总所周知unity的协程有类似于定时器的功能,但是为啥还需要专门写个定时器的功能呢。原因很简单,因为协程需要继承MonoBehaviour的类才能使用,但是不是所有的类都继承MonoBehaviour。另一方面定时器本身属于非常危险的操作,同时也大量使用。因此统一对定时器的统一管理也是非常必要的。

代码设计:

        本质还是通过协程去实现定时的功能,所以需要在场景中新建TimeGameObject,通过此对象开启协程。

      

关键代码:

        TimeManager.cs

        在使用协程之前需要在创建GameObject实例,并且挂载脚本。在定时器模块中获取此对象。在构造函数中实现即可。需要注意的是定时器的开启和关闭都是静态函数,所以构造函数也应该为静态的。

public class TimeManager 
{
    private static TimeBehaviour _TimeMono;
    static TimeManager()
    {
        GameObject timeManagerGo = new GameObject("TimeManager");
        GameObject.DontDestroyOnLoad(timeManagerGo);
        _TimeMono = timeManagerGo.AddComponent<TimeBehaviour>();
    }


    public class TimeBehaviour : MonoBehaviour
    { }

}


        按照个人经验,把定时器分为两种。并且都支持有入参和无入参的情况。

        第一种为,定时器不会随时间自动结束,会按照设定的时间间隔不同调用回调函数。当回调函数的返回值为true时,定时器继续。当回调函数的返回值为false时,定时器结束。

    private static IEnumerator TimeCoroutine<T>(float time, Func<T, bool> callback, T data)
    {
        
        bool isContinue = true;
        while(isContinue == true)
        {
            yield return new WaitForSeconds(time);
            isContinue = callback(data);
        }
        yield return 0;
    }

    private static IEnumerator TimeCoroutine(float time, Func<bool> callback)
    {
        bool isContinue = true;
        while (isContinue == true)
        {
            yield return new WaitForSeconds(time);
            isContinue = callback();
        }
        yield return 0;
    }

        

        第二种为,定时器会在设定的总时间达到时自定结束,在定时器结束前间隔调用回调函数。

    private static IEnumerator IntervalTimeCoroutine<T>(float sumTime, float intervalTime, UnityAction<T> callback, T data)
    {
        float recordTime = 0;
        while (recordTime < sumTime)
        {
            yield return new WaitForSeconds(intervalTime);
            recordTime += intervalTime;
            callback(data);
            if (recordTime >= sumTime)
            {
                break;
            }
        }
        yield return 0;
    }


    private static IEnumerator IntervalTimeCoroutine(float sumTime, float intervalTime, UnityAction callback)
    {
        float recordTime = 0;
        while (recordTime < sumTime)
        {
            yield return new WaitForSeconds(intervalTime);
            callback();
            recordTime += intervalTime;
            if (recordTime >= sumTime)
            {
                break;
            }
        }
        yield return 0;

    }

        定时器的开启和关闭

    public static Coroutine RegisterTime<T>(float time, Func<T, bool> callback, T data)
    {
        return _TimeMono.StartCoroutine(TimeCoroutine<T>(time, callback, data));
    }

    public static Coroutine RegisterTime(float time, Func<bool> callback)
    {
        return _TimeMono.StartCoroutine(TimeCoroutine(time, callback));
    }


    public static Coroutine RegisterIntervalTime<T>(float sumTime, float intervalTime, UnityAction<T> callback, T data)
    {
        return _TimeMono.StartCoroutine(IntervalTimeCoroutine<T>(sumTime, intervalTime, callback, data));
    }

    public static Coroutine RegisterIntervalTime(float sumTime, float intervalTime, UnityAction callback)
    {
        return _TimeMono.StartCoroutine(IntervalTimeCoroutine(sumTime, intervalTime, callback));
    }

    public static void CloseTime(ref Coroutine coroutine)
    {
        if (coroutine != null)
        {
            _TimeMono.StopCoroutine(coroutine);
            coroutine = null;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值