【Unity】制作一个C#实现的计时器

13 篇文章 0 订阅

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace FTProject
{
    class TimerItem
    {
        public int id;
        public float interval;
        public int loopTimes;
        private float _interval;

        public UnityEngine.Object callbackObject;
        public object[] args;

        public Action callback;
        public Action<UnityEngine.Object> callbackObjectEvent;
        public Action<UnityEngine.Object, object[]> callbackObjAndArgs;
        public bool isNotLimtied;
        public bool isStop;
        public bool isFree;


        public TimerItem()
        {

        }
        public TimerItem(int id, float interval, int loopTimes, Action callback = null, Action<UnityEngine.Object> actionObject = null, Action<UnityEngine.Object, object[]> action = null, UnityEngine.Object obj = null, object[] args = null)
        {
            this.id = id;
            this.interval = interval;
            this._interval = interval;
            this.loopTimes = loopTimes;
            this.callback = callback;
            this.callbackObjectEvent = actionObject;
            this.callbackObjAndArgs = action;
            this.callbackObject = obj;
            this.args = args;
            isStop = false;
            isNotLimtied = loopTimes == -1;
            isFree = true;
        }
        public void Reset()
        {
            this.interval = this._interval;
            this.loopTimes--;
            if(loopTimes == 0)
            {
                isFree = true;
            }
        }
    }

    public class TimerManager : BaseManager
    {
        public static TimerManager _timerManager;
        public static TimerManager Instance
        {
            get
            {
                if (_timerManager == null)
                {
                    _timerManager = new TimerManager();
                }
                return _timerManager;
            }
        }
        private Dictionary<int, TimerItem> _timerDicti;
        public int actionIndex;
        public TimerManager()
        {
            _timerDicti = new Dictionary<int, TimerItem>();
            actionIndex = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="interval">时间间隔</param>
        /// <param name="loopTimes">执行次数</param>
        /// <param name="callback">回调函数</param>
        /// <param name="actionObject"></param>
        /// <param name="action"></param>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public int AddTimer(float interval, int loopTimes, Action callback, Action<UnityEngine.Object> actionObject = null, Action<UnityEngine.Object, object[]> action = null, UnityEngine.Object obj = null, object[] args = null)
        {
            TimerItem item = GetTimerItem(interval, loopTimes, callback, actionObject, action, obj, args);
            return item.id;
        }

        public void Update(float intervalTime)
        {
            for (int i = 0; i < _timerDicti.Count; i++)
            {
                KeyValuePair<int, TimerItem> item = _timerDicti.ElementAt(i);
                
                if (!item.Value.isStop)
                {
                    if (item.Value.isNotLimtied || item.Value.loopTimes > 0)
                    {
                        item.Value.interval -= intervalTime;
                        if (item.Value.interval <= 0)
                        {
                            if (item.Value.callback != null)
                            {
                                item.Value.callback();
                            }
                            if (item.Value.callbackObjectEvent != null && item.Value.callbackObject != null)
                            {
                                item.Value.callbackObjectEvent(item.Value.callbackObject);
                            }
                            if (item.Value.callbackObjAndArgs != null && item.Value.callbackObject != null && item.Value.args != null)
                            {
                                item.Value.callbackObjAndArgs(item.Value.callbackObject, item.Value.args);
                            }
                            item.Value.Reset();
                        }
                    }
                }
            }
        }

        public void StopTimerById(int id, bool isStop = true)
        {
            if (_timerDicti.ContainsKey(id))
            {
                _timerDicti[id].isStop = isStop;
            }
        }

        public void RemoveTimerById(int id)
        {
            if (_timerDicti.ContainsKey(id))
            {
                _timerDicti[id].isFree = true;
            }
        }

        private TimerItem GetTimerItem(float interval, int loopTimes, Action callback, Action<UnityEngine.Object> actionObject, Action<UnityEngine.Object, object[]> action = null,  UnityEngine.Object obj = null, object[] args = null)
        {
            if (_timerDicti.Count > 0)
            {
                for (int i = 1; i < _timerDicti.Count; i++)
                {
                    TimerItem item = _timerDicti[i];
                    if (item.isFree)
                    {
                        item.isFree = false;
                        item.interval = interval;
                        item.loopTimes = loopTimes;
                        item.callback = callback;
                        item.callbackObjectEvent = actionObject;
                        item.callbackObjAndArgs = action;
                        item.args = args;
                        return item;
                    }
                }
            }
            actionIndex++;
            TimerItem item1 = new TimerItem(actionIndex, interval, loopTimes, callback,actionObject, action, obj, args);
            item1.isFree = false;
            _timerDicti.Add(actionIndex, item1);
            return item1;
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            _timerDicti.Clear();
        }
    }
}

测试代码:


public class Core
{

    public static TimerManager _timerManager;
    public static TimerManager TimerManager
    {
        get
        {
            if(_timerManager == null)
            {
                _timerManager = new TimerManager();
            }
            return _timerManager;
        }
    }
}

public class Test : MonoBehaviour
{
     int index1;
    int index2;

    int id = 0;
    private void Awake()
    {

        object[] args = new object[3];
        args[0] = 1324;
        args[1] = 13232134;
        args[2] = 56565;
        int id = Core.TimerManager.AddTimer(1, -1, TimerTest1);
        Core.TimerManager.AddTimer(3, 2, TimerTest2, this.gameObject);
        Core.TimerManager.AddTimer(10, 2, TimerTest3, this.gameObject, args);
    }
    private void OnDestroy()
    {
        Core.TimerManager.RemoveTimerById(id);
    }

    private void TimerTest1()
    {
        Debug.LogError(++index1);
    }

    private void TimerTest2(UnityEngine.Object obj)
    {
        Debug.LogError(obj.name + (++index2));
    }

    private void TimerTest3(UnityEngine.Object obj, object[] args)
    {
        for (int i = 0; i < args.Length; i++)
        {
            Debug.LogError(args[i].ToString() + obj.name);
        }
    }
   private void FixedUpdate()
    {
        Core.TimerManager.Update(Time.fixedDeltaTime);
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值