栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity 计时器 Timer 实现

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity 计时器 Timer 实现

这里写自定义目录标题
  • 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 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
{
    private List idleTimers;
    private UnityAction actions;

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


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

    /// 
    /// 开启计时器(无参)
    /// 参数1:首次触发需要等待的时间
    /// 参数2:触发函数
    /// 参数3:循环配置元组
    /// 参数4:是否受游戏时间缩放影响
    /// 
    public Timer StartTimer(float firstTime, UnityAction action, Tuple 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(float firstTime, UnityAction action, T param, Tuple 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(float firstTime, UnityAction action, T1 param1, T2 param2, Tuple 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(float firstTime, UnityAction action, T1 param1, T2 param2, T3 param3, Tuple 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);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905807.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号