Unity 计时器 Timer 实现

Unity 计时器 Timer 实现,第1张

这里写自定义目录标题
  • Timer类构建
  • TimerManeger构建

实现了基本的计时、暂停、停止功能
支持最多三参数函数触发
未实现帧计时功能

Timer类构建
using System;
using UnityEngine;

public class Timer
{
    public bool onOff;//计时器开关
    public float passTime;
    public int passCount;
    public int paramLevel;
    public float firstTime;
    public Tuple<int, float> loopSet;//循环配置元组
    public bool isScale;
   
    public dynamic action;
    public dynamic param_1;
    public dynamic param_2;
    public dynamic param_3;
   


    public void Update()
    {
        if (onOff)
        {
            if (isScale)
            {
                passTime += Time.deltaTime;
            }
            else
            {
                passTime += Time.unscaledDeltaTime;
            }

            if (loopSet == null)
            {
                if (passTime >= firstTime)
                {
                    Trigger();
                    TimerManeger.Instance.Stop(this);
                }
            }
            else if (loopSet.Item1 > 0)
            {
                if (passCount > 0)
                {
                    if (passTime >= loopSet.Item2)
                    {
                        Trigger();
                        passCount++;
                        passTime = 0f;
                    }
                }
                else
                {
                    if (passTime >= firstTime)
                    {
                        Trigger();
                        passCount++;
                        passTime = 0f;
                    }
                }

                if (passCount >= loopSet.Item1)
                {
                    TimerManeger.Instance.Stop(this);
                }

            }
            else if (loopSet.Item1 == 0)
            {
                if (passCount > 0)
                {
                    if (passTime >= loopSet.Item2)
                    {
                        Trigger();
                        passTime = 0;
                    }
                }
                else
                {
                    if (passTime >= firstTime)
                    {
                        Trigger();
                        passTime = 0;
                    }
                }
            }
        }
    }

    private void Trigger() 
    {
        switch (paramLevel)
        {
            case 0:
                action();
                break;
            case 1:
                action(param_1);
                break;
            case 2:
                action(param_1, param_2);
                break;
            case 3:
                action(param_1, param_2, param_3);
                break;
            default:
                break;
        }
    }
}
TimerManeger构建
using System;
using System.Collections.Generic;
using UnityEngine.Events;

public class TimerManeger : SingletonMono<TimerManeger>
{
    private List<Timer> idleTimers;
    private UnityAction actions;

    private void Start()
    {
        idleTimers = new List<Timer>();
        
    }


    private void Update()
    {
        if (actions != null)
        {
            actions();//触发
        }
    }

    /// 
    /// 开启计时器(无参)
    /// 参数1:首次触发需要等待的时间
    /// 参数2:触发函数
    /// 参数3:循环配置元组
    /// 参数4:是否受游戏时间缩放影响
    /// 
    public Timer StartTimer(float firstTime, UnityAction action, Tuple<int, float> loopSet = null, bool isScale = true)
    {
        Timer timer;
        if (idleTimers.Count > 0)
        {
            timer = idleTimers[0];
            idleTimers.RemoveAt(0);
        }
        else
        {
            timer = new Timer();
        }
        timer.firstTime = firstTime;
        timer.action = action;
        timer.loopSet = loopSet;
        timer.isScale = isScale;
        timer.paramLevel = 0;
        timer.onOff = true;
        actions += timer.Update;
        return timer;
    }


    /// 
    /// 开启计时器(一个参数)
    /// 
    public Timer StartTimer<T>(float firstTime, UnityAction<T> action, T param, Tuple<int, float> loopSet = null, bool isScale = true)
    {
        Timer timer;
        if (idleTimers.Count > 0)
        {
            timer = idleTimers[0];
            idleTimers.RemoveAt(0);
        }
        else
        {
            timer = new Timer();
        }

        timer.firstTime = firstTime;
        timer.action = action;
        timer.param_1 = param;
        timer.loopSet = loopSet;
        timer.isScale = isScale;
        timer.paramLevel = 1;
        timer.onOff = true;
        actions += timer.Update;
        return timer;
    }


    /// 
    /// 开启计时器(两个参数)
    /// 
    public Timer StartTimer<T1, T2>(float firstTime, UnityAction<T1, T2> action, T1 param1, T2 param2, Tuple<int, float> loopSet = null,  bool isScale = true)
    {
        Timer timer;
        if (idleTimers.Count > 0)
        {
            timer = idleTimers[0];
            idleTimers.RemoveAt(0);
        }
        else
        {
            timer = new Timer();
        }

        timer.firstTime = firstTime;
        timer.action = action;
        timer.param_1 = param1;
        timer.param_2 = param2;
        timer.loopSet = loopSet;
        timer.isScale = isScale;
        timer.paramLevel = 2;
        timer.onOff = true;
        actions += timer.Update;
        return timer;
    }


    /// 
    /// 开启计时器(三个参数)
    /// 
    public Timer StartTimer<T1, T2, T3>(float firstTime, UnityAction<T1, T2, T3> action, T1 param1, T2 param2, T3 param3, Tuple<int, float> loopSet = null,  bool isScale = true)
    {
        Timer timer;
        if (idleTimers.Count > 0)
        {
            timer = idleTimers[0];
            idleTimers.RemoveAt(0);
        }
        else
        {
            timer = new Timer();
        }

        timer.firstTime = firstTime;
        timer.action = action;
        timer.param_1 = param1;
        timer.param_2 = param2;
        timer.param_3 = param3;
        timer.loopSet = loopSet;
        timer.isScale = isScale;
        timer.paramLevel = 3;
        timer.onOff = true;
        actions += timer.Update;
        return timer;
    }


    /// 
    /// 暂停计时器
    /// 
    public void Pause(Timer timer) 
    {
        timer.onOff = false;
    }


    /// 
    /// 停止计时器
    /// 
    public void Stop(Timer timer)
    {
        timer.onOff = false;
        timer.isScale = true;
        timer.passTime = 0f;
        timer.passCount = 0;
        timer.paramLevel = -1;
        timer.firstTime = 0f;
        timer.loopSet = null;
        timer.action = null;
        timer.param_1 = null;
        timer.param_2 = null;
        timer.param_3 = null;
        actions -= timer.Update;
        idleTimers.Add(timer);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/758508.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-01
下一篇 2022-05-01

发表评论

登录后才能评论

评论列表(0条)

保存