如何在 Unity3D 中执行异步任务? [英] How to execute async task in Unity3D?

查看:48
本文介绍了如何在 Unity3D 中执行异步任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何机构知道统一执行异步任务的任何方式.我正在寻找一段时间,但找不到任何方法.我正在尝试做一个着色游戏,有一个需要一些时间来处理的洪水填充算法.感谢您的帮助.

Does any body know any way of doing async task in unity. I am looking for that for a while but couldn't find any way to do, yet. I am trying to do a coloring game, there is a flood fill algorithm that takes some time to process. Thanks for any help.

推荐答案

您可以在 Unity 中使用线程来执行异步任务.通常你运行分离的线程(从 Unity UI)来执行长时间运行的进程并检查结果(你不能从工作线程与 Unity 交互).常见的方法是使用一个类,该类表示将由 Unity 主线程初始化的线程作业.然后你在那个类的函数上启动一个工作线程,让它完成它的工作(协程在 Unity 主线程上运行,所以不是真正的线程.关于协程的最佳文章是 这里)

You can use threads in Unity to execute your async taks. Usually you run detached threads (from the Unity UI) to do long running processes and check on results (you can't interact with Unity from the working thread). The common approach is to use a class which represents a threading job which will be initialized by the Unity main thread. Then you start a worker thread on a function of that class and let it do it's job (Coroutines run on the Unity main thread so are not real threads. Best article on Coroutines is here)

以下是上述方法的示例(请参阅已接受的答案):

Here's an example of the approach described above (see accepted answer):

http://answers.unity3d.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html

您可能还想尝试一个 UnityGems 包,它可以实现相同的效果但提供便利(例如闭包支持):

You might also want to try a UnityGems package that achieves the same effect but provides convenience (such as closure support):

http://unitygems.com/threads/

HTH.最好!

这篇关于如何在 Unity3D 中执行异步任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆