Unity中定时执行事件的定时器

       Unity开发一年多了,菜鸟一个,最近半年项目做得紧,一直想写写博客记录下项目中遇见的困难和最后解决办法,可是由于各种原因没有启动,今天终于开始启动了!!!

       一直从事VR开发,项目中经常需要用到定时执行某一事件的功能,因此写了一个简易定时器,勉强够用。下面直接上代码:

       下面这个脚本主要作用是创建一个空对象(从创建出来到游戏结束时),用来挂载各种需要常载脚本。

 

using UnityEngine;

public class DontDestroyGO : MonoBehaviour where T: MonoBehaviour
{
    protected static GameObject dontDestroyGO;

    //创建挂载定时器脚本的游戏对象
    internal static void CreateDontDestroyGO()
    {
        if (dontDestroyGO == null)
        {
            dontDestroyGO = new GameObject("DontDestroyGO");
            DontDestroyOnLoad(dontDestroyGO);
            
#if (UNITY_EDITOR && !DEBUG)
        
dontDestroyGO.hideFlags = HideFlags.HideInHierarchy;  
#endif} if (!dontDestroyGO.GetComponent()) ontDestroyGO.AddComponent(); }}

 

 

接下来上定时器的脚本

 

 

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

namespace HMLFramwork
{
    public class TimerTask
    {
        ///


        /// 延时时间
        ///

        public float delayed_time;
        ///
        /// 计时器,计时达到延时时间就执行事件
        ///

        public float timer;
        ///
        /// 是否重复执行
        ///

        public bool isRepeat;
        ///
        /// 事件当前状态:是暂停,还是继续
        ///

        public bool isPause;
        ///
        /// 事件
        ///

        public Action func;

        public TimerTask(float delayed_time, bool isRepeat, Action func)
        {
            this.delayed_time = delayed_time;
            timer = 0;
            this.isRepeat = isRepeat;
            this.isPause = false;
            this.func = func;
        }
    }
    ///


    /// 定时任务控制
    ///

    public class TimerEvent : DontDestroyGO
    {
        static List timerTasks = new List();

        void OnEnable()
        {
            Debug.Log("提醒:开启定时器...");
        }

        TimerTask taskTemp;
        void FixedUpdate()
        {
            if (pause) return;
            if (timerTasks.Count < 1) return;

            for (int i = 0; i < timerTasks.Count; i++)
            {
                taskTemp = timerTasks[i];
                if (!taskTemp.isPause) taskTemp.timer += Time.fixedDeltaTime;
                if (taskTemp.timer >= taskTemp.delayed_time)
                {
                    taskTemp.func.Invoke();
                    if (!taskTemp.isRepeat)
                    {
                        timerTasks.RemoveAt(i);
                        --i;
                        if (timerTasks.Count == 0) break;
                    }
                    else
                    {
                        taskTemp.timer = 0;
                    }
                }
            }

        }

        ///


        /// 添加固定更新定时事件
        ///

        /// 定时时长
        /// 回调事件
        /// 是否重复(不传入,则默认为不重复执行)
        public static void Add(float delayedTime, Action func, bool isrepeat = false)
        {
            CreateDontDestroyGO();
            if (func != null)
            {
                bool isContain = false;
                for (int i = 0; i < timerTasks.Count; i++)
                {
                    if (timerTasks[i].func.Equals(func))
                    {
                        isContain = true;
                        break;
                    }
                }
                if (!isContain) timerTasks.Add(new TimerTask(delayedTime, isrepeat, func));
            }
        }

        ///


        /// 暂停延时事件的计时
        ///

        ///
        public static void Pause(Action func)
        {
            if (func != null)
            {
                for (int i = 0; i < timerTasks.Count; i++)
                {
                    var taskTemp = timerTasks[i];
                    if (taskTemp.func.Equals(func))
                    {
                        taskTemp.isPause = true;
                    }
                }
            }

        }

        ///


        /// 结束事件的计时暂停状态
        ///

        ///
        public static void UnPause(Action func)
        {
            if (func != null)
            {
                for (int i = 0; i < timerTasks.Count; i++)
                {
                    var taskTemp = timerTasks[i];
                    if (taskTemp.func.Equals(func))
                    {
                        taskTemp.isPause = false;
                    }
                }
            }
        }

        ///


        /// 移除指定事件
        ///

        ///
        public static void Remove(Action func)
        {
            if (func != null)
            {
                for (int i = 0; i < timerTasks.Count; i++)
                {
                    var taskTemp = timerTasks[i];
                    if (taskTemp.func.Equals(func))
                    {
                        timerTasks.Remove(taskTemp);
                    }
                }
            }
        }
        ///
        /// 清空定时任务
        ///

        public static void Clear()
        {
            timerTasks.Clear();
        }
    }
}

 

代码没什么难度很容易理解,在此希望各位大神留下宝贵的意见,我好改进,谢谢!

 

 

 

你可能感兴趣的:(Unity)