Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
By D.S.Qiu
尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com
熟悉Unity的developer都知道在Unity中的线程不能使用Unity的对象,但可以使用Unity的值类型变量,如Vector3等。这样就使得线程在Unity中显的很鸡肋和蹩脚,因为很多函数很都是UnityEngine类或函数的调用的,对于哪些是可以在多线程使用,风雨冲进行了如下总结:
0. 变量(都能指向相同的内存地址)都是共享的
1. 不是UnityEngine的API能在分线程运行
2. UnityEngine定义的基本结构(int,float,Struct定义的数据类型)可以在分线程计算,如 Vector3(Struct)可以 , 但Texture2d(class,根父类为Object)不可以。
3. UnityEngine定义的基本类型的函数可以在分线程运行,如
int i = 99;
print (i.ToString());
Vector3 x = new Vector3(0,0,9);
x.Normalize();
类的函数不能在分线程运行
obj.name
实际是get_name函数,分线程报错误:get_name can only be called from the main thread.
Texture2D tt = new Texture2D(10,10);
实际会调用UnityEngine里的Internal_Create,分线程报错误:Internal_Create can only be called from the main thread.
其他transform.position,Texture.Apply()等等都不能在分线程里运行。
结论: 分线程可以做 基本类型的计算, 以及非Unity(包括.Net及SDK)的API。
D.S.Qiu觉得Unity做了这个限制,主要是Unity的函数执行机制是帧序列调用,甚至连Unity的协程Coroutine的执行机制都是确定的,如果可以使用多线程访问UnityEngine的对象和api就得考虑同步问题了,也就是说Unity其实根本没有多线程的机制,协程只是达到一个延时或者是当指定条件满足是才继续执行的机制。
我们的项目目前还有没有比较耗时的计算,所以还没有看到Thread的使用。本来一直没有太考虑着方面的事情,直到在UnityGems.com看到Loom这个类,叹为观止呀。直接贴出人家的介绍(没必要翻译了):
Threads on a Loom
Our class is called Loom. Loom lets you easily run code on another thread and have that other thread run code on the main game thread when it needs to.
There are only two functions to worry about:
- RunAsync(Action) which runs a set of statements on another thread
- QueueOnMainThread(Action, [optional] float time) - which runs a set of statements on the main thread (with an optional delay).
You access Loom using Loom.Current - it deals with creating an invisible game object to interact with the games main thread.
我们只需要关系两个函数:RunAsync(Action)和QueueOnMainThread(Action, [optional] float time) 就可以轻松实现一个函数的两段代码在C#线程和Unity的主线程中交叉运行。原理也很简单:用线程池去运行RunAsync(Action)的函数,在Update中运行QueueOnMainThread(Acition, [optional] float time)传入的函数。
直接贴出源码,供拜读:
using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Threading; using System.Linq; public class Loom : MonoBehaviour { public static int maxThreads = 8; static int numThreads; private static Loom _current; private int _count; public static Loom Current { get { Initialize(); return _current; } } void Awake() { _current = this; initialized = true; } static bool initialized; static void Initialize() { if (!initialized) { if(!Application.isPlaying) return; initialized = true; var g = new GameObject("Loom"); _current = g.AddComponent<Loom>(); } } private List<Action> _actions = new List<Action>(); public struct DelayedQueueItem { public float time; public Action action; } private List<DelayedQueueItem> _delayed = new List<DelayedQueueItem>(); List<DelayedQueueItem> _currentDelayed = new List<DelayedQueueItem>(); public static void QueueOnMainThread(Action action) { QueueOnMainThread( action, 0f); } public static void QueueOnMainThread(Action action, float time) { if(time != 0) { lock(Current._delayed) { Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = action}); } } else { lock (Current._actions) { Current._actions.Add(action); } } } public static Thread RunAsync(Action a) { Initialize(); while(numThreads >= maxThreads) { Thread.Sleep(1); } Interlocked.Increment(ref numThreads); ThreadPool.QueueUserWorkItem(RunAction, a); return null; } private static void RunAction(object action) { try { ((Action)action)(); } catch { } finally { Interlocked.Decrement(ref numThreads); } } void OnDisable() { if (_current == this) { _current = null; } } // Use this for initialization void Start() { } List<Action> _currentActions = new List<Action>(); // Update is called once per frame void Update() { lock (_actions) { _currentActions.Clear(); _currentActions.AddRange(_actions); _actions.Clear(); } foreach(var a in _currentActions) { a(); } lock(_delayed) { _currentDelayed.Clear(); _currentDelayed.AddRange(_delayed.Where(d=>d.time <= Time.time)); foreach(var item in _currentDelayed) _delayed.Remove(item); } foreach(var delayed in _currentDelayed) { delayed.action(); } } }
怎么实现一个函数内使用多线程计算又保持函数体内代码的顺序执行,印象中使用多线程就是要摆脱代码块的顺序执行,但这里是把原本一个函数分拆成为两部分:一部分在C#线程中使用,另一部还是得在Unity的MainThread中使用,怎么解决呢,还得看例子:
//Scale a mesh on a second thread void ScaleMesh(Mesh mesh, float scale) { //Get the vertices of a mesh var vertices = mesh.vertices; //Run the action on a new thread Loom.RunAsync(()=>{ //Loop through the vertices for(var i = 0; i < vertices.Length; i++) { //Scale the vertex vertices[i] = vertices[i] * scale; } //Run some code on the main thread //to update the mesh Loom.QueueOnMainThread(()=>{ //Set the vertices mesh.vertices = vertices; //Recalculate the bounds mesh.RecalculateBounds(); }); }); }
这个例子是对Mesh的顶点进行放缩,同时也是一个使用闭包(closure)和lambda表达式的一个很好例子。看完例子,是不是很有把项目中一些耗时的函数给拆分出来,D.S.Qiu就想用这个方法来改进下NGUI的底层机制(看下性能不能改进)。
小结:
D.S.Qiu在编程技术掌握还是一个菜鸟,Thread还是停留在实现Runable接口或继承Thread的一个水平上,对多线程编程的认识还只是九牛一毛。本来我以为Loom的实现会比较复杂,当我发现只有100多行的代码是大为惊叹,这也得益于现在语言的改进,至少从语言使用的便利性上还是有很大的进步的。
有了Loom这个工具类,在很多涉及UnityEngine对象的耗时计算还是可以得到一个解决方法的:
如在场景中用A*算法进行大量的数据计算
变形网格中操作大量的顶点
持续的要运行上传数据到服务器
二维码识别等图像处理
Loom简单而又巧妙,佩服Loom的作者。
如果您对D.S.Qiu有任何建议或意见可以在文章后面评论,或者发邮件(gd.s.qiu@gmail.com)交流,您的鼓励和支持是我前进的动力,希望能有更多更好的分享。
转载请在文首注明出处:http://dsqiu.iteye.com/blog/2028503
更多精彩请关注D.S.Qiu的博客和微博(ID:静水逐风)
参考:
①UnityGems:http://unitygems.com/threads/
相关推荐
Unity异步线程调用主线程脚本程序,在Unity中异步线程调用主线程会报错,所以编写了一个Loom
unity多线程开发框架,内含3个demo Flocking Massive:多线程Flocking Massive演示,prept运行在三星Galaxy S3 7000 Boids!!! 纹理模糊:表明它是多么容易使用这个框架,重复的任务,使用...
unity多线程开发框架,内含3个demo Flocking Massive:多线程Flocking Massive演示,prept运行 Boids!!! 纹理模糊:表明它是多么容易使用这个框架,重复的任务,使用“startmultithreadedworkloadexecution”...
Unity多线程工具类
Unity除了一些基本的数据类型,几乎所有的API都不能在子线程中调用,如果项目中有一段很耗时操作,unity可能会出现“卡死...因此针对这个问题再加上查找了一些资料,弄出了一个小工具,可以子线程与主线程的相互访问。
[插件资源] Unity线程助手 Unity Thread Helper
c#实现unity下可用的http多线程下载。主要是WWW接口的bundle函数不好用,而且性能也不高。支持多线程。支持单个文件和多个文件下载
C#子线程刷新主线程示例源码 功能介绍: 使用线程操作 1、实时显示当前时间 2、输入加数和被加数,自动出现... 使用了多线程实现了子线程刷新主线程 ,使用委托刷新主线程。 注意: 开发环境为Visual Studio 2012
主要实现了unity下开启线程,在线程中调用unity的内容,比如unity文本框,按钮和下拉框等unity自带的组件,必须通过相关的转换才能实现线程中调用unity的内容,否则,运行直接报错。只需要在unity项目开始调用的位置...
Unity 积雪和交互效果 积雪和交互效果 积雪和交互效果 积雪和交互效果 积雪和交互效果 积雪和交互效果 积雪和交互效果积雪和交互效果积雪和交互效果积雪和交互效果积雪和交互效果积雪和交互效果积雪和交互效果积雪和...
unity 支持多线程进行更新远程服务器assetbundel资源
如果直接在主线程保存日志和发送日志,涉及到文件的读写,如果读写内容过大,会明显抢占主线程资源,造成卡顿,这里使用 Thread 进行日志的保存和邮件发送,避免造成主线程卡顿,这里简单说明,如果你有更好的方法,...
在unity开发中,解决了在分线程没有办法调用mono的api的问题
NULL 博文链接:https://dsqiu.iteye.com/blog/2022992
Unity3D中的多线程及使用多线程1
当前我们在Unity中搞了一个Naitve Plugin插件,开一个线程,主要处理纹理更新的线程,这样就不会影响主线程的帧率。 适合人群:研究多线程渲染的人群 语言:Unity以及C++语言 在压缩包中主要为2个压缩包,一个为...
2、继承 Unity的 PointerInputModule 类,重写和添加方法控制凝视/按键/鼠标交互方式的类的选择、未选择、选中的交互动态 3、添加一个类了动态调整凝视点圈的位置 4、Unity 一个简单的控制陀螺仪实现在场景中空间...
Unity3D与c# Winform交互,包含Unity3D Web与Winform交互跟 Winform与Unity3D exe之间的交互
5、UI 使用 Curved UI ,设置为 Custom_Ray 交互,更改对应 EventSystem 交互系统,即可正常 UGUI world 模式使用 6、Shake Phone 摇晃手机 可以Reset 镭射交互方向置于屏幕中间位置区域,便于简单修复由于手机陀螺...