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)
{
// test
editorCoroutineList = new List<EditorCoroutine>();
}
if (buffer == null)
{
buffer = new List<IEnumerator>();
}
if (editorCoroutineList.Count == 0)
{
EditorApplication.update += Update;
} // add iterator to buffer first
buffer.Add(iterator); return iterator;
} private static bool Find(IEnumerator iterator)
{
// If this iterator is already added
// Then ignore it this time
foreach (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 first
editorCoroutineList.RemoveAll
(
coroutine => { return coroutine.MoveNext() == false; }
); // If we have iterators in buffer
if (buffer.Count > 0)
{
foreach (IEnumerator iterator in buffer)
{
// If this iterators not exists
if (!Find(iterator))
{
// Added this as new EditorCoroutine
editorCoroutineList.Add(new EditorCoroutine(iterator));
}
} // Clear buffer
buffer.Clear();
} // If we have no running EditorCoroutine
// Stop calling update anymore
if (editorCoroutineList.Count == 0)
{
EditorApplication.update -= Update;
}
}
}
这里须要注意几个地方:
1、EditorApplication.update,这个是一个delegate,能够绑定一个函数,从而在编辑器下运行Update。
2、EditorCoroutineRunner.StartEditorCoroutine(Routine1()); 这样能够在编辑器下开启一个协程。
3、另外一个思路是不使用协程,绑定一个Update函数,然后推断www.isDone来获取AssetBundle。
这个我并没有实际验证。
4、www能够正常的载入出AssetBundle。可是isDone的变量一直为false。额外要注意由于Editor模式下不存在退出游戏清理资源的概念,所以要注意处理已载入的assetbundle的情况,否则可能会报冲突的错误。
5、理论上仅仅支持yield return null这种情况,延时要自己处理。
Unity协程的原理是引擎在特定条件下运行MoveNext运行以下的语句。在上面的代码中无论是延时还是其它的东西,都是每帧运行MoveNext,这样WaitForSeconds这种协程是无效的。
www的情况比較特殊,尽管理论上也是会有问题的。可是确实能够正常的取到结果。
Unity怎样在Editor下运行协程(coroutine)的更多相关文章
- Unity协程Coroutine使用总结和一些坑
原文摘自 Unity协程Coroutine使用总结和一些坑 MonoBehavior关于协程提供了下面几个接口: 可以使用函数或者函数名字符串来启动一个协程,同时可以用函数,函数名字符串,和Corou ...
- 【Unity】协程Coroutine及Yield常见用法
最近学习协程Coroutine,参考了别人的文章和视频教程,感觉协程用法还是相当灵活巧妙的,在此简单总结,方便自己以后回顾.Yield关键字的语意可以理解为“暂停”. 首先是yield return的 ...
- Python进阶----异步同步,阻塞非阻塞,线程池(进程池)的异步+回调机制实行并发, 线程队列(Queue, LifoQueue,PriorityQueue), 事件Event,线程的三个状态(就绪,挂起,运行) ,***协程概念,yield模拟并发(有缺陷),Greenlet模块(手动切换),Gevent(协程并发)
Python进阶----异步同步,阻塞非阻塞,线程池(进程池)的异步+回调机制实行并发, 线程队列(Queue, LifoQueue,PriorityQueue), 事件Event,线程的三个状态(就 ...
- Unity协程(Coroutine)管理类——TaskManager工具分享
博客分类: Unity3D插件学习,工具分享 源码分析 Unity协程(Coroutine)管理类——TaskManager工具分享 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处 ...
- (zt)Lua的多任务机制——协程(coroutine)
原帖:http://blog.csdn.net/soloist/article/details/329381 并发是现实世界的本质特征,而聪明的计算机科学家用来模拟并发的技术手段便是多任务机制.大致上 ...
- 协程coroutine
协程(coroutine)顾名思义就是“协作的例程”(co-operative routines).跟具有操作系统概念的线程不一样,协程是在用户空间利用程序语言的语法语义就能实现逻辑上类似多任务的编程 ...
- Lua的多任务机制——协程(coroutine)
并发是现实世界的本质特征,而聪明的计算机科学家用来模拟并发的技术手段便是多任务机制.大致上有这么两种多任务技术,一种是抢占式多任务(preemptive multitasking),它让操作系统来决定 ...
- Python并发编程协程(Coroutine)之Gevent
Gevent官网文档地址:http://www.gevent.org/contents.html 基本概念 我们通常所说的协程Coroutine其实是corporate routine的缩写,直接翻译 ...
- Python之协程(coroutine)
Python之协程(coroutine) 标签(空格分隔): Python进阶 coroutine和generator的区别 generator是数据的产生者.即它pull data 通过 itera ...
随机推荐
- 将EXCEL中的列拼接成SQL insert插入语句
工作中经常需要将EXCEL文件中的数据导入到各种数据库,但是对于不熟悉数据库的人来说,如果直接使用命令执行导入,这无疑是一个难题,也是一个风险.这里我们直接在EXCEL文件中拼接成标准的SQL ins ...
- JavaScript“闭包”精解
一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. 详细了解 Javascript语言的特殊之处,就在于函数内部可以直接读 ...
- OD: First Step
开始学习 0Day 了,前进了小小一步:<0Day 安全:软件漏洞分析艺术>第一篇末尾的 crack_me 实验成功了. 纪念一下. 几个概念: PE: Portable Execu ...
- (原)anaconda 的安装与在pycharm中的版本切换
参考网页: http://continuum.io/blog/anaconda-python-3 http://conda.pydata.org/docs/intro.html#creating-py ...
- LIS n*log(n)的理解
很多时候lis 用二分的方法比较方便 这里写一下他的原理 这里仅对严格的最长上升子序列做讨论 这里有两个数列 一个数列是 原串的数列 a1-an 另一个数列是最长上升子序列辅助数列 s数列的长度为 ...
- uva 10929 - You can say 11
#include <cstdio> using namespace std; ]; int main() { while(gets(in)) { ] == ] == ) break; ; ...
- JQuery获取当前屏幕的高度宽度
JQuery获取浏览器窗口宽高,文档宽高的代码,使用jquery的朋友可以参考下. <script type="text/javascript"> $(document ...
- jQuery中对未来的元素绑定事件
对未来的元素绑定事件不能用bind, 1.可以用live代替,但是要注意jquery的版本,根据官方文档,从1.7开始就不推荐live和delegate了,1.9里就去掉live了. 2.推荐用on代 ...
- struts2标签自动错行、换行问题
<s:radio list="#{0:'男',1:'女' }" value="student.sex" name="student.sex&qu ...
- CSS XHTML规范化命名参考
CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...