Unity 封装一个依赖于MonoBehaviour的计时器(上) 基本功能

        灵感来自下面这本书的协程部分,因此我就自己尝试写了一个

       我的新书Unity3D游戏开发(第3版) | 雨松MOMO程序研究院

   如果你不知道什么是协程:unity保姆级教程之协同程序_unity协同-CSDN博客 

    一句话概括:协程就是单线程的异步操作,其作用于Unity的主线程

1.我写了如下几个功能(只展示无参数):

基础校验

    private bool CheckCount(int count){if (count < 0){Debug.LogError("循环次数不能为负数!");return false;}return true;}private bool CheckTime(float time){if (time < 0){Debug.LogError("等待时间不能为负数!");return false;}return true;}

1.等待受缩放时间影响后的秒数

  public void WaitTime(float waitTime, Action callback){if (CheckTime(waitTime)){StartCoroutine(WaitTimeHandle(waitTime, () => callback?.Invoke()));}}

2.等待不受缩放时间影响后的秒数

    public void WaitRealTime(float waitTime, Action callback){if (CheckTime(waitTime)){StartCoroutine(WaitRealTimeHandle(waitTime, () => callback?.Invoke()));}}

2.按固定时间间隔循环执行

 public void LoopTime(float spacing, int overNumber, Action callback){if (CheckTime(spacing) && CheckCount(overNumber)){StartCoroutine(LoopTimeHandle(spacing, overNumber, () => callback?.Invoke()));}}

3.等待固定帧执行一次

    public void WaitFrame(int frameCount, Action callback){if (CheckCount(frameCount)){StartCoroutine(WaitFrameHandle(frameCount, () => callback?.Invoke()));}}

4.进度反馈

    public void WaitTimeWithProgress(float waitTime, Action<float> progressCallback, Action completeCallback){if (CheckTime(waitTime)){StartCoroutine(ProgressTimer(waitTime, progressCallback, completeCallback));}}private IEnumerator ProgressTimer(float duration, Action<float> progress, Action complete){float startTime = Time.time;while (Time.time - startTime < duration){progress?.Invoke((Time.time - startTime) / duration);yield return null;}complete?.Invoke();}

5.等待当前帧渲染结束执行回调(本帧执行)

    public void WaitForEndOfFrame(Action callback){StartCoroutine(WaitForEndOfFrameHandle(callback));}private IEnumerator WaitForEndOfFrameHandle(Action callback){yield return new WaitForEndOfFrame();callback?.Invoke();}

2.总览

      回调函数我写了无参 一参 两参的版本 可以自行添加参数

using System;
using System.Collections;
using UnityEngine;public class TimeManager : MonoBehaviour
{private static TimeManager instance;public static TimeManager Instance => instance;private void Awake(){if (instance == null){instance = this;}}private bool CheckCount(int count){if (count < 0){Debug.LogError("循环次数不能为负数!");return false;}return true;}private bool CheckTime(float time){if (time < 0){Debug.LogError("等待时间不能为负数!");return false;}return true;}#region 等待固定时间秒/// <summary>/// 等待固定时间以后执行回调/// </summary>/// <param name="waitTime">等待时间(秒)</param>/// <param name="callback">回调函数</param>public void WaitTime(float waitTime, Action callback){if (CheckTime(waitTime)){StartCoroutine(WaitTimeHandle(waitTime, () => callback?.Invoke()));}}public void WaitTime<T>(float waitTime, T param, Action<T> callback){if (CheckTime(waitTime)){StartCoroutine(WaitTimeHandle(waitTime, () => callback?.Invoke(param)));}}public void WaitTime<T, K>(float waitTime, T param1, K param2, Action<T, K> callback){if (CheckTime(waitTime)){StartCoroutine(WaitTimeHandle(waitTime, () => callback?.Invoke(param1, param2)));}}private IEnumerator WaitTimeHandle(float waitTime, Action action){yield return new WaitForSeconds(waitTime);action?.Invoke();}#endregion#region 等待固定时间秒(不受缩放影响)/// <summary>/// 等待固定时间以后执行回调(不受Time.timeScale影响)/// </summary>/// <param name="waitTime">等待时间(秒)</param>/// <param name="callback">回调函数</param>public void WaitRealTime(float waitTime, Action callback){if (CheckTime(waitTime)){StartCoroutine(WaitRealTimeHandle(waitTime, () => callback?.Invoke()));}}public void WaitRealTime<T>(float waitTime, T param, Action<T> callback){if (CheckTime(waitTime)){StartCoroutine(WaitRealTimeHandle(waitTime, () => callback?.Invoke(param)));}}public void WaitRealTime<T, K>(float waitTime, T param1, K param2, Action<T, K> callback){if (CheckTime(waitTime)){StartCoroutine(WaitRealTimeHandle(waitTime, () => callback?.Invoke(param1, param2)));}}private IEnumerator WaitRealTimeHandle(float waitTime, Action action){yield return new WaitForSecondsRealtime(waitTime);action?.Invoke();}#endregion#region 按固定时间间隔循环执行public void LoopTime(float spacing, int overNumber, Action callback){if (CheckTime(spacing) && CheckCount(overNumber)){StartCoroutine(LoopTimeHandle(spacing, overNumber, () => callback?.Invoke()));}}public void LoopTime<T>(float spacing, int overNumber, T param, Action<T> callback){if (CheckTime(spacing) && CheckCount(overNumber)){StartCoroutine(LoopTimeHandle(spacing, overNumber, () => callback?.Invoke(param)));}}public void LoopTime<T, K>(float spacing, int overNumber, T param1, K param2, Action<T, K> callback){if (CheckTime(spacing) && CheckCount(overNumber)){StartCoroutine(LoopTimeHandle(spacing, overNumber, () => callback?.Invoke(param1, param2)));}}private IEnumerator LoopTimeHandle(float spacing, int overNumber, Action action){for (int i = 0; i < overNumber; i++){yield return new WaitForSeconds(spacing);action?.Invoke();}}#endregion#region 等待固定帧执行一次public void WaitFrame(int frameCount, Action callback){if (CheckCount(frameCount)){StartCoroutine(WaitFrameHandle(frameCount, () => callback?.Invoke()));}}public void WaitFrame<T>(int frameCount, T param, Action<T> callback){if (CheckCount(frameCount)){StartCoroutine(WaitFrameHandle(frameCount, () => callback?.Invoke(param)));}}public void WaitFrame<T, K>(int frameCount, T param1, K param2, Action<T, K> callback){if (CheckCount(frameCount)){StartCoroutine(WaitFrameHandle(frameCount, () => callback?.Invoke(param1, param2)));}}private IEnumerator WaitFrameHandle(int frameCount, Action action){for (int i = 0; i < frameCount; i++){yield return null;}action?.Invoke();}#endregion#region 进度反馈public void WaitTimeWithProgress(float waitTime, Action<float> progressCallback, Action completeCallback){if (CheckTime(waitTime)){StartCoroutine(ProgressTimer(waitTime, progressCallback, completeCallback));}}private IEnumerator ProgressTimer(float duration, Action<float> progress, Action complete){float startTime = Time.time;while (Time.time - startTime < duration){progress?.Invoke((Time.time - startTime) / duration);yield return null;}complete?.Invoke();}public void WaitTimeWithProgress<T>(float waitTime, T param, Action<float, T> progressCallback, Action<T> completeCallback){if (CheckTime(waitTime)){StartCoroutine(ProgressTimer(waitTime, param, progressCallback, completeCallback));}}private IEnumerator ProgressTimer<T>(float duration, T param, Action<float, T> progress, Action<T> complete){float startTime = Time.time;while (Time.time - startTime < duration){progress?.Invoke((Time.time - startTime) / duration, param);yield return null;}complete?.Invoke(param);}public void WaitTimeWithProgress<T, K>(float waitTime, T param1, K param2, Action<float, T, K> progressCallback, Action<T, K> completeCallback){if (CheckTime(waitTime)){StartCoroutine(ProgressTimer(waitTime, param1, param2, progressCallback, completeCallback));}}private IEnumerator ProgressTimer<T, K>(float duration, T param1, K param2, Action<float, T, K> progress, Action<T, K> complete){float startTime = Time.time;while (Time.time - startTime < duration){progress?.Invoke((Time.time - startTime) / duration, param1, param2);yield return null;}complete?.Invoke(param1, param2);}#endregion#region 等待当前帧结束执行回调public void WaitForEndOfFrame(Action callback){StartCoroutine(WaitForEndOfFrameHandle(callback));}private IEnumerator WaitForEndOfFrameHandle(Action callback){yield return new WaitForEndOfFrame();callback?.Invoke();}public void WaitForEndOfFrame<T>(T param, Action<T> callback){StartCoroutine(WaitForEndOfFrameHandle(param, callback));}private IEnumerator WaitForEndOfFrameHandle<T>(T param, Action<T> callback){yield return new WaitForEndOfFrame();callback?.Invoke(param);}public void WaitForEndOfFrame<T, K>(T param1, K param2, Action<T, K> callback){StartCoroutine(WaitForEndOfFrameHandle(param1, param2, callback));}private IEnumerator WaitForEndOfFrameHandle<T, K>(T param1, K param2, Action<T, K> callback){yield return new WaitForEndOfFrame();callback?.Invoke(param1, param2);}#endregionpublic void Stop(IEnumerator func){StopCoroutine(func);}public void StopAll(){StopAllCoroutines();}public TimeChainContext StartChain(){return new TimeChainContext(this);}
}

3.Ai给出了如下拓展思路       

        优先级高

        链式调用扩展

        物理时间步长同步

         优先级中

        调试可视化工具

        网络同步时钟

        优先级低

      自动化测试框架集成

      使用Mono管理类去管理该脚本从而摆脱Mono的控制

        我目前的需求没有这么复杂,当作一个简单的计时器使用

        其是依赖于Unity的这一点可以用await和async来替代,但是可能涉及到了多线程,我这块学了但是用的不多还需加强

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/72460.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数学建模 第一节

目录​​​​​​ 前言 一 优化模型的类型 二 线性规划1 线性规划2 三 0-1规划 总结 前言 数学建模主要是将问题转化为模型&#xff0c;然后再以编程的形式输出出来 算法都知道&#xff0c;数学建模也需要用到算法&#xff0c;但是不是主要以编程形式展示&#xff0c;而是…

Vulkan视频解码decode显示display之同步

在ReleaseDisplayedPicture函数中消耗图片资源并且显示display完成&#xff0c;设置两个标志m_hasConsummerSignalFence true 和m_hasConsummerSignalSemaphore true virtual int32_t ReleaseDisplayedPicture(DecodedFrameRelease** pDecodedFramesRelease, uint32_t nu…

网络空间安全(32)Kali MSF基本介绍

前言 Metasploit Framework&#xff08;简称MSF&#xff09;是一款功能强大的开源安全漏洞检测工具&#xff0c;被广泛应用于渗透测试中。它内置了数千个已知的软件漏洞&#xff0c;并持续更新以应对新兴的安全威胁。MSF不仅限于漏洞利用&#xff0c;还包括信息收集、漏洞探测和…

设计模式学习记录

设计模式23种 创建型抽象工厂模式工厂模式生成器模式原型模式单例模式 结构型适配器模式桥接模式组合模式装饰模式外观模式享元模式代理模式 行为型责任链模式命令模式解释器模式迭代器模式中介者模式备忘录模式观察者模式状态模式策略模式模版方法模式访问者模式 创建型 与对…

2.5 python接口编程

在现代软件开发的复杂生态系统中&#xff0c;不同系统、模块之间的交互协作至关重要。接口编程作为一种关键机制&#xff0c;定义了组件之间的通信规范与交互方式。Python 凭借其卓越的灵活性、丰富的库资源以及简洁易读的语法&#xff0c;在接口编程领域占据了重要地位&#x…

mesh开发解析

开源的Mesh网络协议栈及相关项目&#xff1a; 1.B.A.T.M.A.N.(Better Approach to Mobile Ad-hoc Networking)• 简介&#xff1a;B.A.T.M.A.N.是一种用于多跳自组织网络的路由协议&#xff0c;适用于无线Mesh网络。它通过优化数据传输路径&#xff0c;确保网络的高可靠性和动…

PyTorch PINN实战:用深度学习求解微分方程

在人工智能与计算数学的交汇点&#xff0c;物理信息神经网络&#xff08;Physics-Informed Neural Networks&#xff0c;PINN&#xff09;正引领着一场求解微分方程的革命。传统上&#xff0c;微分方程是描述自然现象和工程问题中各种关系的重要工具&#xff0c;但其求解往往依…

【WiFi 7核心技术及未来挑战】

作为刚刚开始从事这一领域的人&#xff0c;浅浅学习了一下WiFi 7&#xff08;IEEE 802.11be&#xff09;。Wi-Fi 7发展迅速&#xff0c;提供前所未有的速度、更低的延迟和更高的可靠性。但从频谱政策到能效挑战&#xff0c;再到成本&#xff0c;仍有许多问题亟待解决。 Wi-Fi 7…

Oracle Linux Server 7.9安装fail2ban

yum search oracle-epel-release yum install oracle-epel-release-el7 search fail2ban yum install fail2ban nano /etc/fail2ban/jail.d/00-firewalld.conf # defalut这里是设定全局设置&#xff0c;如果下面的监控没有设置就以全局设置的值设置。 [DEFAULT] # 用于指定哪…

目标在哪里?——寻找人生的意义与方向

在职场中&#xff0c;许多人都会经历这样的困惑&#xff1a;工作看似顺利&#xff0c;却逐渐失去了成就感和成长感。一位在500强企业工作的学员就遇到了这样的问题。她曾考虑过转型做培训&#xff0c;但苦于找不到明确的切入点&#xff0c;对未来的目标感到迷茫。她不禁问自己&…

C++类与对象——拷贝构造与运算符重载

拷贝构造函数和赋值运算符重载就是C类默认六个函数之二。 拷贝构造函数&#xff1a; 如果⼀个构造函数的第⼀个参数是自身类类型的引用&#xff0c;且任何额外的参数都有默认值&#xff0c;则此构造函数 也叫做拷贝构造函数&#xff0c;也就是说拷贝构造是⼀个特殊的构造函数…

破碎的誓言

破碎的誓言 在秋风的呢喃中&#xff0c;落叶轻叹&#xff0c; 昔日的誓言&#xff0c;如烟消散。 你的眼眸&#xff0c;曾是我星辰的指引&#xff0c; 如今&#xff0c;却成了最深的迷惘。 欺骗的利刃&#xff0c;刺穿了信任的堡垒&#xff0c; 我的心&#xff0c;如裂开…

AD画板学习

AD画板 01 课程简介及学习目标 &#xff08;1&#xff09;能熟练的新建项目文件、原理图文件、PCB文件且修改文件名&#xff0c;并知道文件保存的位置&#xff1b; &#xff08;2&#xff09;会设置原理图编辑器的工作环境&#xff0c;会自底向上绘制层次原理图&#xff1b; …

Linux:进程程序替换

目录 前言 一 进程程序替换的概念 二 进程程序替换的原理 三 为什么需要进行进程程序替换 四 如何进行进程程序替换 1. 进程替换函数 ➊ execl()函数 ➋execv()函数 ➌execlp()函数 ➍execle()函数 ➎execve()* 前言 一般情况下&#xff0c;对应的语言写的程序只…

基于变分推理与 Best‑of‑N 策略的元 Prompt 自动生成与优化框架

摘要 本文提出了一种融合变分推理与 Best‑of‑N 策略的元 Prompt 自动生成与优化框架&#xff0c;通过高度参数化的模板、随机扰动采样及多指标评分机制&#xff0c;实现从初始提示生成到最终输出的动态优化。同时&#xff0c;针对实际应用中对自适应参数调整、深层语义理解、…

Umi-OCR 全家桶

介绍&#xff1a; 下载 访问官网地址 https://github.com/hiroi-sora/umi-ocrhttps://github.com/hiroi-sora/umi-ocr 点击下载&#xff08;.exe 文件 安装即可&#xff09; 桌面使用 安装完毕后去安装路径下点击 Umi-OCR.exe &#xff08;默认不会生成桌面的快捷方式&…

2023南京理工大学计算机复试上机真题

2023南京理工大学计算机复试上机真题 2023南京理工大学计算机复试机试真题 历年南京理工大学计算机复试上机真题 在线评测&#xff1a;传送门&#xff1a;pgcode.cn 括号匹配二 题目描述 苗苗今天刚刚学会使用括号&#xff0c;不过他分不清小括号&#xff0c;中括号&#…

Conda 常规用法指南

Conda 常规用法指南 1. Conda 简介 Conda 是一个开源的包管理和环境管理系统&#xff0c;主要用于 Python 和其他编程语言的开发环境。它能够方便地安装、更新和管理依赖项&#xff0c;使得不同项目可以使用不同的 Python 版本和库。 2. Conda 环境管理 2.1 创建新的环境 conda…

非零初始条件系统的传递函数分析

非零初始条件系统的传递函数分析 在传递函数的定义中&#xff0c;通常假设系统满足零初始条件。然而在实际应用中&#xff0c;很多系统需要处理非零初始状态。为了探讨这一问题&#xff0c;我们以一个一阶微分方程为例进行分析。 一、一阶系统的分析 考虑以下一阶微分方程&a…

centos7安装时采用的默认分区(比如:/dev/sda3的对应挂载点是/),如何对系统扩容?

‌非LVM分区扩容方案‌ 若 /dev/sda3 是‌非LVM分区‌且存储重要数据&#xff0c;可通过 ‌直接扩展分区容量‌ ‌调整文件系统‌ 实现扩容&#xff0c;无需重建LVM或格式化分区‌。以下是具体步骤&#xff1a; ‌1. 扩展物理磁盘&#xff08;虚拟机场景&#xff09;‌ ‌关…