Unity如何在Editor下执行协程(coroutine)

2024-02-11 17:32

本文主要是介绍Unity如何在Editor下执行协程(coroutine),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在处理Unity5新的AssetBundle的时候,我有一个需求,需要在Editor下(比如一个menuitem的处理函数中,游戏没有运行,也没有MonoBehaviour)加载AssetBundle。而加载AssetBundle的时候又需要使用yield return www;这样的协程用法。

所以就有了一个需求,在Editor下执行协程。我从网上找到一个EditorCoroutine,其代码如下:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;public static class EditorCoroutineRunner
{private class EditorCoroutine : IEnumerator{private Stack<IEnumerator> executionStack;public EditorCoroutine(IEnumerator iterator){this.executionStack = new Stack<IEnumerator>();this.executionStack.Push(iterator);}public bool MoveNext(){IEnumerator i = this.executionStack.Peek();if (i.MoveNext()){object result = i.Current;if (result != null && result is IEnumerator){this.executionStack.Push((IEnumerator)result);}return true;}else{if (this.executionStack.Count > 1){this.executionStack.Pop();return true;}}return false;}public void Reset(){throw new System.NotSupportedException("This Operation Is Not Supported.");}public object Current{get { return this.executionStack.Peek().Current; }}public bool Find(IEnumerator iterator){return this.executionStack.Contains(iterator);}}private static List<EditorCoroutine> editorCoroutineList;private static List<IEnumerator> buffer;public static IEnumerator StartEditorCoroutine(IEnumerator iterator){if (editorCoroutineList == null){// testeditorCoroutineList = new List<EditorCoroutine>();}if (buffer == null){buffer = new List<IEnumerator>();}if (editorCoroutineList.Count == 0){EditorApplication.update += Update;}// add iterator to buffer firstbuffer.Add(iterator);return iterator;}private static bool Find(IEnumerator iterator){// If this iterator is already added// Then ignore it this timeforeach (EditorCoroutine editorCoroutine in editorCoroutineList){if (editorCoroutine.Find(iterator)){return true;}}return false;}private static void Update(){// EditorCoroutine execution may append new iterators to buffer// Therefore we should run EditorCoroutine firsteditorCoroutineList.RemoveAll(coroutine => { return coroutine.MoveNext() == false; });// If we have iterators in bufferif (buffer.Count > 0){foreach (IEnumerator iterator in buffer){// If this iterators not existsif (!Find(iterator)){// Added this as new EditorCoroutineeditorCoroutineList.Add(new EditorCoroutine(iterator));}}// Clear bufferbuffer.Clear();}// If we have no running EditorCoroutine// Stop calling update anymoreif (editorCoroutineList.Count == 0){EditorApplication.update -= Update;}}
}

这里需要注意几个地方:
1、EditorApplication.update,这个是一个delegate,可以绑定一个函数,从而在编辑器下执行Update。

2、EditorCoroutineRunner.StartEditorCoroutine(Routine1()); 这样可以在编辑器下开启一个协程。

3、另外一个思路是不使用协程,绑定一个Update函数,然后判断www.isDone来获取AssetBundle。这个我并没有实际验证。


文章转载自Unity如何在Editor下执行协程(coroutine),感谢原作者提供好文章

这篇关于Unity如何在Editor下执行协程(coroutine)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

python协程实现高并发的技术详解

《python协程实现高并发的技术详解》协程是实现高并发的一种非常高效的方式,特别适合处理大量I/O操作的场景,本文我们将简单介绍python协程实现高并发的相关方法,需要的小伙伴可以了解下... 目录核心概念与简单示例高并发实践:网络请求协程如何实现高并发:核心技术协作式多任务与事件循环非阻塞I/O与连接

MyBatis Plus中执行原生SQL语句方法常见方案

《MyBatisPlus中执行原生SQL语句方法常见方案》MyBatisPlus提供了多种执行原生SQL语句的方法,包括使用SqlRunner工具类、@Select注解和XML映射文件,每种方法都有... 目录 如何使用这些方法1. 使用 SqlRunner 工具类2. 使用 @Select 注解3. 使用

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

java中ssh2执行多条命令的四种方法

《java中ssh2执行多条命令的四种方法》本文主要介绍了java中ssh2执行多条命令的四种方法,包括分号分隔、管道分隔、EOF块、脚本调用,可确保环境配置生效,提升操作效率,具有一定的参考价值,感... 目录1 使用分号隔开2 使用管道符号隔开3 使用写EOF的方式4 使用脚本的方式大家平时有没有遇到自

mybatis直接执行完整sql及踩坑解决

《mybatis直接执行完整sql及踩坑解决》MyBatis可通过select标签执行动态SQL,DQL用ListLinkedHashMap接收结果,DML用int处理,注意防御SQL注入,优先使用#... 目录myBATiFBNZQs直接执行完整sql及踩坑select语句采用count、insert、u

一个Java的main方法在JVM中的执行流程示例详解

《一个Java的main方法在JVM中的执行流程示例详解》main方法是Java程序的入口点,程序从这里开始执行,:本文主要介绍一个Java的main方法在JVM中执行流程的相关资料,文中通过代码... 目录第一阶段:加载 (Loading)第二阶段:链接 (Linking)第三阶段:初始化 (Initia

Kotlin 协程之Channel的概念和基本使用详解

《Kotlin协程之Channel的概念和基本使用详解》文章介绍协程在复杂场景中使用Channel进行数据传递与控制,涵盖创建参数、缓冲策略、操作方式及异常处理,适用于持续数据流、多协程协作等,需注... 目录前言launch / async 适合的场景Channel 的概念和基本使用概念Channel 的

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

Java实现远程执行Shell指令

《Java实现远程执行Shell指令》文章介绍使用JSch在SpringBoot项目中实现远程Shell操作,涵盖环境配置、依赖引入及工具类编写,详解分号和双与号执行多指令的区别... 目录软硬件环境说明编写执行Shell指令的工具类总结jsch(Java Secure Channel)是SSH2的一个纯J

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手