Unity 自定义计时器(Event Timer)

当需要倒计时功能的时候,自定义计一个时器,功能可以自己随意拓展很方便。

1、使用

MyTimer.GetInstance().StartTimer(10f, 1f, (obj, args)=>
{
	Debug.Log("time = " + args.LastTime + ", " + args.LostTime + ", " + args.DealTimes);
});

2、计时器

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

public class MyTimer : SingleMonoBase<MyTimer>
{
    private List<TimerHandler> mTimeHandles;

    protected override void Awake()
    {
        mTimeHandles = new List<TimerHandler>();
    }

    /// <summary>
    /// 启动一个计时器
    /// </summary>
    /// <param name="totalTime">总时长</param>
    /// <param name="eventTime">每xx秒处理一次event</param>
    public TimerHandler StartTimer(float totalTime, float eventTime, EventHandler<TimeEventArgs> handler)
    {
        TimerHandler handle = new TimerHandler(totalTime, eventTime, handler);
        mTimeHandles.Add(handle);
        return handle;
    }

    public void StartTimer(TimerHandler handle)
    {
        if (mTimeHandles.Contains(handle))
        {
            handle.Restart();
        }
        else
        {
            handle.Restart();
            mTimeHandles.Add(handle);
        }
    }

    public void RestartTimer(TimerHandler handle)
    {
        if (mTimeHandles.Contains(handle))
        {
            handle.Restart();
        }
        else
        {
            StartTimer(handle);
        }
    }

    public void DeleteTimer(TimerHandler handle)
    {
        if (mTimeHandles.Contains(handle))
        {
            mTimeHandles.Remove(handle);
        }
    }

    protected override void Update()
    {
        mTimeHandles.ForEach((handle) =>
        {
            handle.Update(Time.deltaTime);
        });
    }

    protected override void OnDestroy()
    {
        mTimeHandles.Clear();
    }
}


public class TimerHandler
{
    /// <summary>
    /// 处理event的总时长
    /// </summary>
    float mTime;
    float mUpdateTime;

    /// <summary>
    /// 每xx秒处理一次event(mTime必须大于mEventTime)
    /// </summary>
    float mEventTime;
    float mUpdateEventTime;
    event EventHandler<TimeEventArgs> mEventHandler;//event EventHandler mEventHandler;

    TimeEventArgs args;

    public TimerHandler(float totalTime, float time, EventHandler<TimeEventArgs> handler)
    {
        mTime = totalTime;
        mUpdateTime = mTime;

        mEventTime = time;
        mUpdateEventTime = mEventTime;
        mEventHandler = handler;

        args = new TimeEventArgs();
        args.LastTime = mTime - mUpdateTime;
        args.LostTime = mUpdateTime;
    }

    public void Restart()
    {
        mUpdateTime = mTime;
        mUpdateEventTime = mEventTime;
        args.Clear();
    }

    public void Update(float delta)
    {
        if (mUpdateTime > 0)
        {
            mUpdateTime -= delta;
            OnEvent(delta);
        }
    }

    private void OnEvent(float delta)
    {
        mUpdateEventTime -= delta;
        if (mUpdateEventTime <= 0)
        {
            mUpdateEventTime = mEventTime;

            args.LastTime = mTime - mUpdateTime;
            args.LostTime = mUpdateTime;
            args.DealTimes ++;
            mEventHandler?.Invoke(this, args);
        }
    }
}

public class TimeEventArgs : EventArgs
{
    /// <summary>
    /// 剩余的时间
    /// </summary>
    public float LastTime;

    /// <summary>
    /// 流失的时间
    /// </summary>
    public float LostTime;

    /// <summary>
    /// 处理次数
    /// </summary>
    public int DealTimes;

    public void Clear()
    {
        LastTime = 0;
        LostTime = 0;
        DealTimes = 0;
    }
}

3、MonoBehaviour单例

using UnityEngine;
using Asserts = System.Diagnostics.Debug;

public class SingleMonoBase<T> : MonoBehaviour
    where T : SingleMonoBase<T>
{
    private static T instance = null;

    protected virtual void Awake()
    {
        Asserts.Assert(null == instance);
        instance = (T)this;
        StaticManagerRoot.Instance.AddChild(this.transform);
    }

    protected virtual void Update() { 
    
    }

    protected virtual void OnDestroy()
    {
        Asserts.Assert(this == instance);
        instance = null;
    }

    public static T GetInstance()
    {
        if (null == instance)
        {
            instance = FindObjectOfType<T>();

            if (null == instance)
            {
                GameObject item = new GameObject(typeof(T).ToString());

                instance = item.AddComponent<T>();
            }
        }

        return instance;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值