unity3d - Unity 像线程一样使用协程?
问题描述
我有一个耗时的功能,例如...
Debug.Log("A");
void Froze_Function() {
for(int i=0; i<50000; ++i)
// take some time...
Debug.Log("B");
}
Debug.Log("C");
我不希望“Froze_Function”延迟我的统一程序,所以我希望它在其他线程中运行。
Unity有一个协程,但我不知道如何正确地使用协程来制作这个功能。
请不要说优化我的功能...
解决方案
void Start()
{
Debug.Log("A");
StartCoroutine(Froze_Function());
Debug.Log("C");
}
//Inside a method (class should be MonoBehavour) call: StartCoroutine(Froze_Function());
IEnumerator Froze_Function()
{
float dist;
for (int i = 0; i < 500; ++i)
{
dist = Vector3.Distance(Vector3.up, Vector3.left);
yield return 0;
}
Debug.Log("B");
yield return 0;
}
推荐阅读
- python - AdamOptimizer 的随机性如何?
- docker - Docker 和远程卷
- html - 是否可以仅在仅使用 html 或 css 检查单选按钮后启用选择输入?
- css - 在多语言网站中使用 BEM 管理特异性
- javascript - JavaScript:在移动设备上隐藏 div
- asp.net - 每次我回来时我的网站都会注销
- python - 我的程序真的很简单,但我的 fps 增加不超过 20
- jquery - Angular 6 JQuery嵌套属性单击事件不起作用
- tensorflow - 如何将自定义渐变摘要添加到 tf.keras.callbacks.TensorBoard
- html - 如何在 index.html 以外的文件中重用这个导航栏 React 组件?