Unity3D_Util_Unity定时管理

博客提到在Unity开发中常使用定时器,如Unity API里的Update()、LateUpadte()、协成等,使用这些需继承MonoBehaviour,作者做法是将所有定时集中到一个脚本里。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

Unity开发中常常会用定时器,例如Unity API中的Update(),LateUpadte(),协成等,要用的这些必须继承MonoBehaviour,我的做法是把所有的定时都集中到一个脚本里

 

 public class MonoBehaviourAgent : MonoBehaviour
    {
        public MonoBehaviourAgent()
        {
            events = new Queue<UnityAction>();
            UpdateAgentEvent += () =>
            {
                lock (events)
                {
                    while (events.Count > 0)
                    {
                        var action = events.Dequeue();
                        action.DefaultAction();
                    }
                }

            };
            disposables = new List<IDisposable>();
        }

        #region Unity Api Update
        public event UnityAction UpdateAgentEvent;
        public event UnityAction LateUpdateAgentEvent;
        public event UnityAction FixedUpdateAgentEvent;
        private readonly Queue<UnityAction> events;
        private List<IDisposable> disposables;
        private void Update()
        {
            UpdateAgentEvent.DefaultAction();
        }
        private void LateUpdate()
        {
            LateUpdateAgentEvent.DefaultAction();
        }
        private void FixedUpdate()
        {
            FixedUpdateAgentEvent.DefaultAction();
        }

        private void OnDestroy()
        {
            foreach (var disposable in disposables)
            {
                disposable.Dispose();
            }
        }
        #endregion

        #region 协成定时器
        public void WariFixedTime(float fixedTime, UnityAction action)
        {
            StartCoroutine(wariFixedTime(fixedTime, action));
        }
        IEnumerator wariFixedTime(float fixedTime, UnityAction action)
        {
            var waiter = new WaitForSeconds(fixedTime);
            while (true)
            {
                yield return waiter;
                action.DefaultAction();
            }
        }
        public void WariFixedOneTime(float fixedTime, UnityAction action)
        {
            StartCoroutine(wariFixedOneTime(fixedTime, action));
        }
        IEnumerator wariFixedOneTime(float fixedTime, UnityAction action)
        {
            var waiter = new WaitForSeconds(fixedTime);
            yield return waiter;
            action.DefaultAction();
        }
        #endregion

        #region Threading.Timer
        /// <summary>
        /// 开启定时器(执行一次)
        /// </summary>
        /// <param name="milisecond">定时毫秒</param>
        /// <param name="action">回调函数</param>
        public void OneTimeTimerStart(int milisecond, UnityAction action)
        {
            OneTimeTimer oneTimeTimer = new OneTimeTimer(milisecond, () =>
            {
                action.DefaultAction();
            });
        }
        /// <summary>
        /// 开启定时器
        /// </summary>
        /// <param name="milisecond">定时毫秒</param>
        /// <param name="action">回调函数</param>
        public void PeriodicTimerStart(int milisecond, UnityAction action)
        {
            PeriodicTimer periodicTimer = new PeriodicTimer(milisecond, () =>
            {
                action.DefaultAction();
            });
        }
        /// <summary>
        /// 开启定时器(执行一次)
        /// </summary>
        /// <param name="milisecond">定时毫秒</param>
        /// <param name="action">回到主线程调用</param>
        public void OneTimeTimerStartOnMainThread(int milisecond, UnityAction action)
        {
            OneTimeTimer oneTimeTimer = new OneTimeTimer(milisecond, () =>
            {
                lock (events)
                {
                    events.Enqueue(action);
                }
            });
        }
        /// <summary>
        /// 开启定时器
        /// </summary>
        /// <param name="milisecond">定时毫秒</param>
        /// <param name="action">回到主线程调用</param>
        public void PeriodicTimerStartOnMainThread(int milisecond, UnityAction action)
        {
            PeriodicTimer periodicTimer = new PeriodicTimer(milisecond, () =>
              {
                  lock (events)
                  {
                      events.Enqueue(action);
                  }
              });
        }
        /// <summary>
        /// 执行一次定时类
        /// </summary>
        sealed class OneTimeTimer : IDisposable
        {
            static readonly HashSet<Timer> timers = new HashSet<Timer>();
            private UnityAction action;
            private Timer timer;
            public OneTimeTimer(int dueTime, UnityAction action)
            {
                this.action = action;
                timer = new Timer(Tick, null, dueTime, -1);
                lock (timers)
                {
                    timers.Add(timer);
                }
            }

            private void Tick(object state)
            {
                try
                {
                    action.DefaultAction();
                }
                finally
                {
                    Unroot();
                }
            }

            private void Unroot()
            {
                lock (timers)
                {
                    if (timer != null)
                        timers.Remove(timer);
                }
                if (timer != null)
                    timer.Dispose();
            }

            public void Dispose()
            {
            }
        }
        /// <summary>
        /// 执行循环定时类
        /// </summary>
        sealed class PeriodicTimer : IDisposable
        {
            static readonly HashSet<Timer> timers = new HashSet<Timer>();
            private UnityAction action;
            private Timer timer;

            public PeriodicTimer(int period, UnityAction action)
            {
                this.action = action;
                timer = new Timer(Tick, null, period, period);
                lock (timers)
                {
                    timers.Add(timer);
                }
            }

            private void Tick(object state)
            {
                lock (this)
                {
                    action.DefaultAction();
                }
            }

            public void Dispose()
            {
                lock (timers)
                {
                    if (timer != null)
                        timers.Remove(timer);
                }
                if (timer != null)
                    timer.Dispose();
            }
        }
        #endregion

    }

 

转载于:https://www.cnblogs.com/PandaHome/p/10934242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值