如何在Android平台的unity3d中使用c#线程? [英] how to use c# threads in unity3d for android platform?

查看:86
本文介绍了如何在Android平台的unity3d中使用c#线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在线程中加载文件,场景和播放动画. 尝试在Android中通过www加载文件... 如何通过线程做其他的事情? 但是,为什么游戏引擎不允许我们创建线程? 还是我的理解是错误的? 如何在UNITY3D中创建线程?

I am in need to load files, scenes and play animations in threads.. Tried loading files via www in Android... how to do other stuff via threads? But how come a game engine doesn't allow us to create threads? or my understanding is wrong? how can one create threads in UNITY3D?

推荐答案

您可以在Unity中使用线程,但是引擎不是线程安全的.通常,您运行分离的线程(从Unity UI)来运行长时间运行的进程并检查结果(您无法从工作线程与Unity进行交互). 常用方法是使用一个类,该类表示将由Unity主线程初始化的线程作业.然后,在该类的函数上启动工作线程,并使其完成工作(协程在Unity主线程上运行,因此不是真正的线程.关于协程的最佳文章是

You can use threads in Unity but the engine is not thread safe. Usually you run detached threads (from the Unity UI) to do long running processes and check on results (you cannot 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). See this page

HTH. 最好!

这篇关于如何在Android平台的unity3d中使用c#线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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