1、Timer timer = new Timer(); 创建时间管理器 参数(float time, TimeUnit timeUnit,bool ignoreTimeScale = false, bool autoStart = true) time:时间值、timeUnit 时间单位(帧率、秒、厘秒、毫秒)、ignoreTimeScale 忽略时间缩放(可以使游戏暂停更容易实现)、autoStart 自动开始计时

2、timer.loop = true; // 循环          timer.Finished +=method;  // 回调函数     timer.Pause(); // 暂停

using System.Collections.Generic;
using UnityEngine;

namespace Epitome
{
    public delegate void FinishedHandler();

public class Timer
    {
        TimerManager.TimerState timer;

public bool Running { get { return timer.Running; } }

public bool Paused { get { return timer.Paused; } }

public bool Loop
        {
            set { timer.HasRepeat = value; }
            get { return timer.HasRepeat; }
        }

public event FinishedHandler Finished;

public Timer(float time, TimeUnit timeUnit,bool ignoreTimeScale = false, bool autoStart = true)
        {
            timer = TimerManager.Instance.CreateTimer(time, timeUnit, ignoreTimeScale);
            timer.Finished += TimerFinished;
            if (autoStart) timer.Start();
        }

public void Start() { timer.Start(); }

public void Stop() { timer.Stop(); }

public void Pause() { timer.Pause(); }

public void UnPause() { timer.UnPause(); }

public void TimerFinished()
        {
            FinishedHandler handler = Finished;
            if (handler != null)
                handler();
        }
    }

public enum TimeUnit
    {
        FrameRate, // 帧率
        Second, // 秒
        CentiSecond, // 厘秒:是一秒的百分之一(0.01秒)
        MilliSecond, // 毫秒:是一秒的千分之一(0.001秒)
    }

public class TimerManager : MonoSingleton<TimerManager>
    {
        public class TimerState
        {
            bool running;
            bool paused;
            bool stopped;

public bool Running { get { return running; } }

public bool Paused { get { return paused; } }

public event FinishedHandler Finished;

private TimeUnit timeUnit;

private float delayTime; // 延迟时间
            private float attackTime; // 启动时间
            private  float currentTime; // 当前时间

public bool HasRepeat; // 一直重复

public bool ignoreTimeScale { get; private set; } // 忽略时间缩放

public TimerState(float time, TimeUnit unit, bool ignore)
            {
                timeUnit = unit;
                ignoreTimeScale = ignore;

delayTime = time;

ResetState();
            }

private void ResetState()
            {
                switch (timeUnit)
                {
                    case TimeUnit.FrameRate:
                        currentTime = 0.0f;
                        break;
                    case TimeUnit.Second:
                    case TimeUnit.CentiSecond:
                    case TimeUnit.MilliSecond:
                        if (!ignoreTimeScale) currentTime = 0.0f;
                        else currentTime = Time.realtimeSinceStartup;
                        break;
                }

attackTime = delayTime + currentTime;
            }

public void UpdateTime(float time)
            {
                time = ignoreTimeScale ? time - currentTime : time;

if (running)
                {
                    if (paused) return;

switch (timeUnit)
                    {
                        case TimeUnit.FrameRate:
                            currentTime += 1;
                            break;
                        case TimeUnit.Second:
                            currentTime += time;
                            break;
                        case TimeUnit.CentiSecond:
                            currentTime += time * 100;
                            break;
                        case TimeUnit.MilliSecond:
                            currentTime += time * 1000;
                            break;
                    }

if (currentTime >= attackTime)
                    {
                        if (HasRepeat)
                        {
                            ResetState();
                        }
                        else
                        {
                            Stop();
                        }

FinishedHandler handle = Finished;
                        if (handle != null)
                        {
                            handle();
                        }
                    }
                }
            }

public void Start()
            {
                running = true;
            }

public void Stop()
            {
                stopped = true;
                running = false;
            }

public void Pause()
            {
                paused = true;
            }

public void UnPause()
            {
                paused = false;
            }
        }

private List<TimerState> timerList = new List<TimerState>();

private void Update()
        {
            for (int i = 0; i < timerList.Count ; i++)
            {
                timerList[i].UpdateTime(timerList[i].ignoreTimeScale ? Time.realtimeSinceStartup : Time.deltaTime);
            }
        }

public TimerState CreateTimer(float time, TimeUnit timeUnit,bool ignoreTimeScale)
        {
            TimerState newTimer = new TimerState(time, timeUnit, ignoreTimeScale);
            timerList.Add(newTimer);
            return newTimer;
        }

public void ClearTimer() { }
        public void ClearAllTimer() { }
    }
}

使用案例

public class text : MonoBehaviour {

// Use this for initialization
    void Start () {
        Time.timeScale = 3;

Timer timer = new Timer(1, TimeUnit.Second); //第三个参数是否忽略时间缩放带来的影响
        timer.Loop = true; // 设置可循环
        timer.Finished += rw; 
    }

private void rw()
    {
        Debug.Log("你好");
    }
}
---------------------

Unity3D——Epirome框架_TimerManager计时任务管理器的更多相关文章

  1. unity3D客户端框架

    unity3D客户端框架  博客

  2. Unity3D知识框架

    美术部分:           3d模型,材质,纹理,shader,Animator,Animation,天空盒,灯光效果,烘焙 程序部分:           基本组成:               ...

  3. Unity3d + PureMVC框架搭建

    0.流程:LoginView-SendNotification()---->LoginCommand--Execute()--->调用proxy中的函数操作模型数据--LoginProxy ...

  4. NancyFx框架之检测任务管理器

    先建一个空的项目和之前的NancyFx系列一样的步骤 然后建三个文件夹Models,Module,Views 然后分别安装一下组件 jQuery Microsoft.AspNet.SignalR Mi ...

  5. [Unity3D]Unity资料大全免费分享

     都是网上找的连七八糟的资料了,整理好分享的,有学习资料,视频,源码,插件……等等 东西比较多,不是所有的都是你需要的,可以按  ctrl+F 来搜索你要的东西,如果有广告,不用理会,关掉就可以了,如 ...

  6. [课程设计]Scrum 1.4 多鱼点餐系统开发进度(点餐页面框架布置)

    Scrum 1.4 多鱼点餐系统开发进度 (点餐页面框架布置) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系 ...

  7. Unity3D编程学习分享

    学习地址:http://www.ixueyun.com/lessons/detail-lessonId-692.html 一.课程概述: 以前大部分3D游戏出现在pc和ps.XBox等专业游戏主机上, ...

  8. 《开源框架那些事儿22》:UI框架设计实战

    UI是User Interface的缩写.通常被觉得是MVC中View的部分,作用是提供跟人机交互的可视化操作界面. MVC中Model提供内容给UI进行渲染,用户通过UI框架产生响应,一般而言会由控 ...

  9. [课程设计]Scrum 1.4 多鱼点餐系统开发进度

    Scrum 1.4 多鱼点餐系统开发进度 (点餐页面框架布置) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系 ...

随机推荐

  1. CodeForces19D:Points(线段树+set(动态查找每个点右上方的点))

    Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coo ...

  2. Python测试框架doctest

    doctest是python自带的一个模块.本博客将介绍doctest的两种使用方式:一种是嵌入到python源码中,另外一种是放到一个独立文件. doctest 的概念模型 在python的官方文档 ...

  3. Win7系统打开服务管理界面的几种方法汇总

    转自:https://www.jb51.net/os/windows/318465.html Win7服务管理包含了计算机操作系统和应用程序提供的所有服务,但是这么多服务并非总是用户所需的.比如打印机 ...

  4. springmvc不进入Controller导致404

    转自:https://blog.csdn.net/qq_36769100/article/details/71746449#1.%E5%90%AF%E5%8A%A8%E9%A1%B9%E7%9B%AE ...

  5. mysite-exampleservice和mysite-vsg

    可能找到了这两个nova安装的地方,下午分析 搜素site_name test 这条线 cord-compute-maas-playbook这条线 这个可以作为验证

  6. Lua变量

    Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量. 编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...

  7. UVaLive 7456 Least Crucial Node (并查集+暴力 或者 求割点)

    题意:求标号最小的最大割点.(删除该点后,指定点#sink能到达的点数减少最多). 析:由于不知道要去掉哪个结点,又因为只有100个结点,所以我们考虑用一个暴力,把所有的结点都去一次,然后用并查集去判 ...

  8. (水题)洛谷 - P1553 - 数字反转(升级版) - 字符串格式转换

    https://www.luogu.org/problemnew/show/P1553 忘记给整数加上前导零去除的代码了.其实不去也可以,额外的进位用一个carry另外存起来就好. #include& ...

  9. 第八篇 .NET高级技术之字符串暂存池(缓冲池)

    字符串不可变性,字符串的‘暂存池’两个特性 字符串是引用类型,程序中会存在大量的字符串对象,如果每次都创建一个字符串对象,会比较浪费内存.性能低,因此CLR做了“暂存池”(拘留池,缓冲池,暂存池),在 ...

  10. [BJOI2017]魔法咒语

    Description Chandra 是一个魔法天才. 从一岁时接受火之教会洗礼之后, Chandra 就显示出对火元素无与伦比的亲和力,轻而易举地学会种种晦涩难解的法术.这也多亏 Chandra ...