【发布时间】:2021-04-30 16:54:30
【问题描述】:
我正在尝试将我的寻路转移到一个单独的线程。但我仍然有 1 个函数需要从搜索中每个节点的主线程调用。我找到了 UnityMainThreadDispatcher here,它旨在通过在主线程上运行给定的操作来解决这个问题。但是,在我从该操作返回值之前,我无法继续执行我的函数。 这是我目前正在尝试使用的代码。当我调试此代码时,回调中会返回一个有效的 returnVal,但是当我尝试等待该值时,即使用 while 循环时,我被卡住了在无限循环中。这是为什么呢?
var latlon = Vector2d.zero;
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
Debug.Log("Running on Main thread");
_monoBehaviour.StartCoroutine(
GetLatLonFromUnityCoords(point, returnVal =>
{
latlon = returnVal;
Debug.Log($"Calculated latlon: {latlon}");
}
));
Debug.Log("Finished on Main thread");
});
while (latlon = Vector2d.zero) { }
// latlon has been returned, continue
我知道这样做不好,我能做些什么呢? 谢谢
【问题讨论】:
标签: c# multithreading unity3d coroutine