unity3d框架研究之计时器

代码内容不是本人原创的,请原谅我斗胆作为原创发表:


计时器部分分为两个类,一个算是基类(虽然没有继承关系),另一个类做为对外的接口,实现计时器的注册,更新,删除,获取等等的操作,

值得注意的是,这个计时器把计数的时间放大了1000倍,这样可以使计时更加的精确


下面是代码:


/**
* 文件名称:TimerObject.cs
* 简    述:自定义的选择框
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


namespace Need.Mx
{
    public delegate void TimerTriggerCallback(TimerObject timerObj);

    /// <summary>
    /// 定时器对象;
    /// </summary>
    public class TimerObject
    {
        数据变量//
        /// <summary>
        /// 定时器GUID;
        /// </summary>
        private int Guid;
        /// <summary>
        /// 起点;
        /// </summary>
        private int startTick;
        /// <summary>
        /// 起点;
        /// </summary>
        private int endTick;
        
        /// <summary>
        /// 间隔多长时间触发;
        /// </summary>
        private int triggerTick;

        private bool  isOver = false;

        /// <summary>
        /// 触发回调;
        /// </summary>
        private TimerTriggerCallback Callback;
        //


        内部逻辑变量/
        /// <summary>
        /// 当前时间;
        /// </summary>
        private float curTick;

        /// <summary>
        /// 定时累加时间;
        /// </summary>
        private int delta = 0;
        private bool running = false;
        //
        
        #region 属性访问器;
        /// <summary>
        /// 定时器GUID;
        /// </summary>
        public int TimerGuid
        {
            get
            {
                return Guid;
            }
        }
        /// <summary>
        /// 起点;
        /// </summary>
        public int StartTick
        {
            get
            {
                return startTick/1000;
            }
        }
        /// <summary>
        /// 起点;
        /// </summary>
        public int EndTick
        {
            get
            {
                return endTick/1000;
            }
        }
        /// <summary>
        /// 当前时间;
        /// </summary>
        public int CurTick
        {
            get
            {
                return (int)Mathf.Round((curTick + 499.9f) / 1000.0f);
            }
        }
        /// <summary>
        /// 间隔多长时间触发;
        /// </summary>
        public int TriggerTick
        {
            get
            {
                return triggerTick/1000;
            }
        }
        /// <summary>
        /// 是否已经结束;
        /// </summary>
        public bool IsOver
        {
            get
            {
                return isOver;
            }
        }
        #endregion

        public TimerObject(int guid, int st, int et, int tt, TimerTriggerCallback callback)
        {
            Guid = guid;
            startTick = st;
            endTick = et;
            curTick = st;
            triggerTick = tt;
            Callback = callback;
            delta = 0;
            isOver = false;
            running = true;
        }

        /// <summary>
        /// 开启定时器(因为定时器生成后就会运行,这个借口主要是对应Pause);
        /// </summary>
        public void Play()
        {
            running = true;
        }

        /// <summary>
        /// 暂停定时器;
        /// </summary>
        public void Pause()
        {
            running = false;
        }

        /// <summary>
        /// 停止定时器;
        /// </summary>
        public void Stop()
        {
            curTick = endTick;
        }

        /// <summary>
        /// 删除定时器
        /// </summary>
        public void Over()
        {
            isOver = true;
        }

        /// <summary>
        /// 重置定时器,注意,掉用此函数后,要将此定时器对象重新注册到管理类里面,否则可能不会触发定时器事件;
        /// </summary>
        public void Reset()
        {
            curTick = startTick;
            delta = 0;
            isOver = false;
            running = true;

            Callback(this);
        }
        
        /// <summary>
        /// 更新定时器;
        /// </summary>
        public bool UpdateTick(int tickInMillionSeconds)
        {
            if (running)
            {
                if (startTick > endTick)
                {
                    curTick -= tickInMillionSeconds;
                    if (curTick <= endTick)
                    {
                        isOver = true;
                    }
                }
                else
                {
                    curTick += tickInMillionSeconds;
                    if (curTick >= endTick)
                    {
                        isOver = true;
                    }
                }

                delta += tickInMillionSeconds;
                //Log.Print("curTick"+curTick+"  delta:" + delta + "  isOver:" + isOver);
                if (delta >= triggerTick)
                {
                    delta -= triggerTick;
                    Callback(this);
                }
                else if(isOver)
                {
                    Callback(this);
                }

                return isOver;
            }
            else
            {
                return false;
            }
        }
    }
}



然后是另外的一个对外提供的接口类:



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


namespace Need.Mx
{
    public class TimerManager
    {
        #region Singleton
        protected static TimerManager instance;

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

        protected TimerManager()
        {
            TimerList = new List<TimerObject>();
            removeList = new List<TimerObject>();
        }
        #endregion

        private int GuidIndex = 0;
        protected List<TimerObject> TimerList;

        protected List<TimerObject> removeList;

        #region 外部接口;
        /// <summary>
        /// 注册一个定时器;
        /// </summary>
        public int RegisterTimerEx(int start, int end, int trigger, TimerTriggerCallback callback, bool startTrigger = true)
        {
            int guid = GetGuid();
            TimerObject timerObj = new TimerObject(guid, start * 1000, end * 1000, trigger * 1000, callback);
            TimerList.Add(timerObj);
            if (startTrigger)
            {
                //第一次触发一下;
                callback(timerObj);
            }            
            return guid;
        }

        public int RegisterTimer(float start, float end, float trigger, TimerTriggerCallback callback, bool startTrigger = true)
        {
            return RegisterTimerEx((int)start, (int)end, (int)trigger, callback, startTrigger);
        }

        /// <summary>
        /// 注册一个定时器;
        /// </summary>
        public void RegisterTimer(TimerObject timerObj)
        {
            if (!TimerList.Contains(timerObj))
            {
                TimerList.Add(timerObj);
            }
        }

        /// <summary>
        /// 删除一个定时器;
        /// </summary>
        public TimerObject RemoverTimer(int guid)
        {
            TimerObject tobeRemObj = null;
            for (int i = 0; i < TimerList.Count; ++i)
            {
                TimerObject to = TimerList[i];
                if (to.TimerGuid == guid)
                {
                    tobeRemObj = to;
                    break;
                }
            }

            if (tobeRemObj != null)
            {
                TimerList.Remove(tobeRemObj);                
            }

            return tobeRemObj;
        }

        /// <summary>
        /// 删除一个定时器;
        /// </summary>
        public void RemoverTimer(TimerObject timerObj)
        {
            if (TimerList.Contains(timerObj))
            {
                TimerList.Remove(timerObj);
            }
        }

        /// <summary>
        /// 获取一个定时器;
        /// </summary>
        public TimerObject GetTimerObject(int guid)
        {
            TimerObject tobeRemObj = null;
            for (int i = 0; i < TimerList.Count; ++i)
            {
                TimerObject to = TimerList[i];
                if (to.TimerGuid == guid)
                {
                    tobeRemObj = to;
                    break;
                }
            }

            return tobeRemObj;
        }

        /// <summary>
        /// 定时器更新;
        /// </summary>
        public void UpdateAllTimers(float tick)
        {
            int iTick = (int)(tick * 1000);
            for (int i = 0; i < TimerList.Count; ++i)
            {
                TimerObject to = TimerList[i];
                if (to.UpdateTick(iTick))
                {
                    removeList.Add(to);
                }
            }

            for (int i = 0; i < removeList.Count; ++i)
            {
                TimerObject toRem = removeList[i];
                TimerList.Remove(toRem);
            }

            removeList.Clear();
        }
        #endregion

        #region 内部逻辑;

        private int GetGuid()
        {
            return GuidIndex++;
        }
        #endregion
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值