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();
        }
    }
  • 相关阅读:
    CDH5.2安装更换hive元数据存储数据库遇到的问题
    SSH 互信
    【记录】Java NIO实现网络模块遇到的BUG
    Http2协议简介
    synchronized(this) 与 synchronized(class) 理解
    【记录】spring boot 图片上传与显示
    Cookie-Session机制
    linux利用用户组给用户赋予不同的权限
    java .equals()和==的区别
    String直接赋值和使用new的区别
  • 原文地址:https://www.cnblogs.com/zjw007/p/6127389.html
Copyright © 2011-2022 走看看