zoukankan      html  css  js  c++  java
  • 定时器

    using System.Collections.Generic;
    using UnityEngine;
    
    public class Timer
    {
        //自动释放
        private readonly bool autoRelease;
        //回调
        private readonly TimerMngr.Callback func;
        //间隔时间
        private float interval;
        //重复次数
        private int repeat;
        private bool started;
        //时间计数
        private float time;
    
        public Timer(TimerMngr.Callback _func)
        {
            time = 0f;
            repeat = 0;
            func = _func;
            autoRelease = true;
        }
    
        public Timer(TimerMngr.Callback _func, int repeatTimes, bool autoR)
        {
            time = 0f;
            func = _func;
            autoRelease = autoR;
            repeat = repeatTimes;
        }
    
        public Timer(TimerMngr.Callback _func, bool autoR)
        {
            time = 0f;
            func = _func;
            autoRelease = autoR;
            repeat = 0;
        }
    
        public void Start(float _time)
        {
            time = 0f;
            interval = _time;
            started = true;
        }
    
        public void Stop()
        {
            time = 0f;
            started = false;
        }
    
        public bool TimesUp()
        {
            return time >= interval;
        }
        public bool FixedUpdate()
        {
            if (started == false)
                return false;
    
            time += Time.deltaTime;
            if (!TimesUp())
                return false;
            //时间到了
            if (func != null)
            {
                func();
                if (repeat <= 1)
                {
                    Stop();
                    return autoRelease;
                }
                repeat--;
                time = 0;
                return false;
            }
            Stop();
            return true;
        }
    }
    
    public class TimerMngr : Singleton<TimerMngr>
    {
        public delegate void Callback();
    
        private readonly LinkedList<Timer> timerList = new LinkedList<Timer>();
    
        public Timer Create(Callback func)
        {
            var timer = new Timer(func);
            timerList.AddLast(timer);
            return timer;
        }
    
        public Timer Create(Callback func, bool autoRelease)
        {
            var timer = new Timer(func, autoRelease);
            timerList.AddLast(timer);
            return timer;
        }
    
        public Timer Create(Callback func, int repeatTimes, bool autoRelease)
        {
            var timer = new Timer(func, repeatTimes, autoRelease);
            timerList.AddLast(timer);
            return timer;
        }
    
        public void Destroy(Timer timer)
        {
            timerList.Remove(timer);
        }
    
        public void FixedUpdate()
        {
            var first = timerList.First;
            while (first != null)
            {
                if (first.Value.FixedUpdate())
                {
                    var node = first;
                    first = first.Next;
                    timerList.Remove(node);
                }
                else
                {
                    first = first.Next;
                }
            }
        }
    }
    public class test : MonoBehaviour
    {
    
        // Use this for initialization
        void Start()
        {
            TimerMngr.Instance.Create(() =>
            {
                print("两秒后回调回来");
            }).Start(2);
        }
    
        public void FixedUpdate()
        {
            TimerMngr.Instance.FixedUpdate();
        }
    }
  • 相关阅读:
    ubuntu上搭建review board代码评审站点
    android5.1 for tq335x
    2015.04.11
    DECLARE_GLOBAL_DATA_PTR宏定义问题
    6410移植android4.4.2笔记(持续更新)
    ubuntu安装packet提示重复冲突问题
    android5.0 aosp编译记录(由于机器硬件原因,改为4.4.2编译通过)
    2015.01.16
    2015.01.14
    SDSM框架
  • 原文地址:https://www.cnblogs.com/zjw007/p/6127389.html
Copyright © 2011-2022 走看看