Unity实现自己的协程系统

2024-09-07 08:20
文章标签 实现 系统 unity 协程

本文主要是介绍Unity实现自己的协程系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述:自定义Unity协程调度器(不依赖Mono)
 

        实现了一个协程调度器,允许在程序中以非阻塞的方式调度协程。协程可以在满足特定条件后暂停和恢复,如等待特定的帧数、时间、或等待其他协程执行完毕。它的设计思想与Unity的协程机制类似,但它不依赖Unity的YieldInstruction,因此适用于非Unity环境。


协程可以在以下情况下暂停:
"yield null ;" 等一帧;
"yield 一个int值",等待给定的帧数;
"yield 一个float值;",等待给定的秒数;
"yield scheduler.StartCoroutine(Coroutine())",直到另一个协程完成。


支持多个调度器实例.一个调度器中的协程可以等待另一个完全不同的调度器实例中的协程。

不使用Unity的YieldInstruction类,因为无法访问其内部数据进行调度。调度语义与Unity的调度器略有不同。
         例如,在Unity中,如果启动协程,它会立即运行到第一个yield,而在这个调度器中,它直到下一次调用UpdateAllCoroutines才会运行。
这个特性允许在任何时候启动协程,但确保启动的协程只在特定时间运行。

在同一个更新中运行的协程之间不要依赖更新顺序。例如,StartCoroutine(A), StartCoroutine(B), StartCoroutine(C)
        如果A、B、C都为:while(true) { print(A|B|C); yield; },不要期望 "ABC" 或 "CBA" 或任何特定的顺序。


 代码结构:
CoroutineScheduler 类:

 这个类是协程调度器的核心,它可以管理多个协程并根据指定的调度规则执行协程。
StartCoroutine: 启动一个新的协程。协程不会立即执行,而是在下一次调用UpdateAllCoroutines时开始运行。它可以接受IEnumerator或IEnumerable作为输入。

 Rewind: 允许重置并重新运行一个已完成的协程。

 Stop/StopAllCoroutines: 停止单个或所有协程。StopAllCoroutines停止调度器中的所有协程.

 Pause/Resume: 可以暂停和恢复协程的执行。恢复时协程会从暂停时的位置继续。

 UpdateAllCoroutines: 执行调度器中的所有协程,直到它们的下一个yield。需要调用者提供当前帧数和时间来控制协程的进度。这个方法是调度器的核心,它根据不同的条件(如时间、帧数等)来判断协程是否需要继续执行。

 CoroutineNode 类:

 每个协程都被封装在一个CoroutineNode对象中,保存了协程的执行状态、等待的条件(如等待的帧数或时间)以及是否暂停、完成等状态。

 waitForFrame/waitForTime: 协程的等待条件,当协程需要等待一定的帧数或时间时会使用这些字段。

 Reset: 允许重置协程,使其可以从头开始运行。

 Pause/Resume: 这些方法用来控制协程的暂停和恢复。

 IYieldWrapper 接口:

 这个接口允许实现一些自定义的等待条件,比如等待某个Unity对象执行完特定任务后才继续协程。

 工作原理:
 每次UpdateAllCoroutines被调用时,调度器会遍历所有未完成的协程,并根据协程当前的等待条件(帧数、时间或其他协程)决定是否继续执行。如果协程满足其等待条件,就继续执行它直到遇到下一个yield。

 在协程执行的过程中,可以通过yield return指定暂停条件。当满足条件时,调度器会将协程从等待状态中恢复。


这个调度器的设计受Unity协程机制的启发,但它并不依赖于Unity引擎,因此可以在任何基于帧更新或时间驱动的系统中使用。

 这个调度器的灵活性允许你在任意的时间调度和控制协程,并且可以在多个调度器实例之间进行协作。

using System.Collections;
public class CoroutineScheduler
{public CoroutineNode first = null; // 链表的第一个协程节点int currentFrame; // 当前帧数float currentTime; // 当前时间CoroutineNode runTimeNuext; // 执行时的下一个节点/*** 启动一个协程,协程不会立即运行,而是在下一次调用UpdateAllCoroutines时运行。* 协程的执行可以在任何时候使用yield语句暂停。yield返回值指定协程何时恢复。*/public CoroutineNode StartCoroutine(IEnumerator fiber){// 如果函数没有yield,fiber将为null,什么也不做if (fiber == null){return null;}// 创建协程节点并运行,直到到达第一个yieldCoroutineNode coroutine = new CoroutineNode(fiber);AddCoroutine(coroutine);return coroutine;}public CoroutineNode StartCoroutine(IEnumerable srcFiber){if (srcFiber == null){return null;}CoroutineNode coroutine = new CoroutineNode(srcFiber.GetEnumerator());coroutine.srcFiber = srcFiber;AddCoroutine(coroutine);return coroutine;}public void Rewind(CoroutineNode cn){if (cn.srcFiber == null)throw new System.Exception("不是IEnumerable函数");if (cn.finished){AddCoroutine(cn);}cn.Reset(); // 重置协程节点}public void Stop(CoroutineNode cn){RemoveCoroutine(cn);cn.finished = true; // 标记协程已完成}public void Pause(CoroutineNode cn){if (cn.finished) return;if (!cn.isPaused){cn.isPrePause = true;cn.isPaused = true;}}public void Resume(CoroutineNode cn){if (cn.finished) return;if (cn.isPaused){cn.isPaused = false;cn.isPrePause = true;AddCoroutine(cn); // 恢复协程}}/*** 停止所有在该调度器上运行的协程。建议避免使用此方法,* 应找到一种自然的方式让协程自行完成,而不是在它们完成之前被强制停止。* 如果你需要更细粒度的控制来停止协程,你可以使用多个调度器。*/public void StopAllCoroutines(){CoroutineNode cn = first;CoroutineNode next;while (cn != null){next = cn.listNext;cn.listPrevious = null;cn.listNext = null;cn = next;}first = null; // 清空调度器中的所有协程}/*** 如果此调度器有任何协程运行,则返回true。你可以使用它来检查所有协程是否已完成或被停止。*/public bool HasCoroutines(){return first != null;}/*** 运行所有活跃的协程直到它们的下一个yield。调用者必须提供当前的帧数和时间。* 这允许调度器在不同于Unity主游戏循环的帧数和时间制度下运行。*/public void UpdateAllCoroutines(int frame, float time){currentFrame = frame;currentTime = time;CoroutineNode coroutine = this.first;while (coroutine != null){// 在协程完成并从列表中移除之前,存储listNextrunTimeNuext = coroutine.listNext;if (coroutine.isPaused) // 如果协程暂停,直接进入下一个节点{if (coroutine.isPrePause){coroutine.isPrePause = false;if (coroutine.waitForFrame > 0) coroutine.waitForFrame -= currentFrame;else if (coroutine.waitForTime > 0) coroutine.waitForTime -= currentTime;else if (coroutine.waitForCoroutine != null) Pause(coroutine.waitForCoroutine);else if (coroutine.waitForUnityObject != null) coroutine.waitForUnityObject.Pause();}RemoveCoroutine(coroutine);coroutine = runTimeNuext;continue;}else if (coroutine.isPrePause){coroutine.isPrePause = false;if (coroutine.waitForFrame > 0) coroutine.waitForFrame += currentFrame;else if (coroutine.waitForTime > 0) coroutine.waitForTime += currentTime;else if (coroutine.waitForCoroutine != null) Resume(coroutine.waitForCoroutine);else if (coroutine.waitForUnityObject != null) coroutine.waitForUnityObject.Resume();}if (coroutine.waitForFrame > 0 && frame >= coroutine.waitForFrame){coroutine.waitForFrame = -1;InitUpdateCoroutine(coroutine);}else if (coroutine.waitForTime > 0.0f && time >= coroutine.waitForTime){coroutine.waitForTime = -1.0f;InitUpdateCoroutine(coroutine);}else if (coroutine.waitForCoroutine != null && coroutine.waitForCoroutine.finished){coroutine.waitForCoroutine = null;InitUpdateCoroutine(coroutine);}else if (coroutine.waitForUnityObject != null && coroutine.waitForUnityObject.finished){coroutine.waitForUnityObject = null;InitUpdateCoroutine(coroutine);}else if (coroutine.waitForFrame == -1 && coroutine.waitForTime == -1.0f&& coroutine.waitForCoroutine == null &&coroutine.waitForUnityObject == null){// 初始更新InitUpdateCoroutine(coroutine);}coroutine = runTimeNuext;}}/*** 执行协程直到下一个yield。如果协程已完成,标记它为完成并将其从调度器列表中移除。*/void InitUpdateCoroutine(CoroutineNode coroutine){IEnumerator fiber = coroutine.fiber;if (coroutine.fiber.MoveNext()){// System.Object yieldCommand = fiber.Current == null ? (System.Object)1 : fiber.Current;System.Object yieldCommand = fiber.Current;if (fiber.Current == null){coroutine.waitForFrame = 1 + currentFrame;}else if (yieldCommand is int){coroutine.waitForFrame = (int)yieldCommand + currentFrame;}else if (yieldCommand is float){coroutine.waitForTime = (float)yieldCommand + currentTime;}else if (yieldCommand is CoroutineNode){coroutine.waitForCoroutine = (CoroutineNode)yieldCommand;}else if (yieldCommand is IYieldWrapper){coroutine.waitForUnityObject = yieldCommand as IYieldWrapper;}else{throw new System.ArgumentException("CoroutineScheduler: 意外的协程yield类型: " + yieldCommand.GetType());}}else{// 协程完成coroutine.finished = true;RemoveCoroutine(coroutine);}}void AddCoroutine(CoroutineNode coroutine){if (first == null){first = coroutine;first.listPrevious = null;first.listNext = null;}else{var c = first;while (c.listNext != null)c = c.listNext;c.listNext = coroutine;coroutine.listNext = null;coroutine.listPrevious = c;}coroutine.finished = false; // 协程未完成}void RemoveCoroutine(CoroutineNode coroutine){if (this.first == coroutine){// 移除第一个协程节点this.first = coroutine.listNext;if (first != null) first.listPrevious = null;}else{// 不是列表头if (coroutine.listNext != null){// 移除中间的节点coroutine.listPrevious.listNext = coroutine.listNext;coroutine.listNext.listPrevious = coroutine.listPrevious;}else if (coroutine.listPrevious != null){// listNext为空,移除最后一个节点coroutine.listPrevious.listNext = null;}}if (coroutine == runTimeNuext)runTimeNuext = coroutine.listNext;coroutine.listPrevious = null;coroutine.listNext = null;}
}public interface IYieldWrapper
{bool finished { get; }void Pause();void Resume();
}public class CoroutineNode : IYieldWrapper
{public CoroutineNode listPrevious = null;public CoroutineNode listNext = null;public IEnumerable srcFiber;public IEnumerator fiber;public bool finished = true;public int waitForFrame = -1;public float waitForTime = -1.0f;public CoroutineNode waitForCoroutine;public IYieldWrapper waitForUnityObject; //lonewolfwilliamspublic bool isPaused = false;public bool isPrePause = false;public CoroutineNode(IEnumerator _fiber, bool _finished = false){this.fiber = _fiber;this.finished = _finished;}public CoroutineNode(IEnumerable _srcFiber, bool _finished = false){srcFiber = _srcFiber;this.fiber = _srcFiber.GetEnumerator();this.finished = _finished;}public void Reset(){if (srcFiber != null)fiber = srcFiber.GetEnumerator();isPaused = false;isPrePause = false;waitForFrame = -1;waitForTime = -1.0f;waitForCoroutine = null;waitForUnityObject = null;}bool IYieldWrapper.finished{get { return finished; }}public void Pause(){if (finished) return;if (!isPaused){isPrePause = true;isPaused = true;}}public void Resume(){if (finished) return;if (isPaused){isPaused = false;isPrePause = true;}}
}

这篇关于Unity实现自己的协程系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144560

相关文章

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

mysql数据库重置表主键id的实现

《mysql数据库重置表主键id的实现》在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,本文主要介绍了mysql数据库重置表主键id的实现,具有一定的参考价值,感兴趣的可以了... 目录关键语法演示案例在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,当我们

SpringBoot配置Ollama实现本地部署DeepSeek

《SpringBoot配置Ollama实现本地部署DeepSeek》本文主要介绍了在本地环境中使用Ollama配置DeepSeek模型,并在IntelliJIDEA中创建一个Sprin... 目录前言详细步骤一、本地配置DeepSeek二、SpringBoot项目调用本地DeepSeek前言随着人工智能技

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.