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

相关文章

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 中手

python 线程池顺序执行的方法实现

《python线程池顺序执行的方法实现》在Python中,线程池默认是并发执行任务的,但若需要实现任务的顺序执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录方案一:强制单线程(伪顺序执行)方案二:按提交顺序获取结果方案三:任务间依赖控制方案四:队列顺序消

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

Spring Bean初始化及@PostConstruc执行顺序示例详解

《SpringBean初始化及@PostConstruc执行顺序示例详解》本文给大家介绍SpringBean初始化及@PostConstruc执行顺序,本文通过实例代码给大家介绍的非常详细,对大家的... 目录1. Bean初始化执行顺序2. 成员变量初始化顺序2.1 普通Java类(非Spring环境)(

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理