有人能说说这样的建筑是怎么做的吗?当前返回 UnityExtension。 好吧,或者建议将非 MonoBehaviour 代码的一部分移动到单独的线程中是多么容易。
UnityException: get_canAccess can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
private async void Awake()
{
Mesh protoMesh = gameObject.GetComponent<MeshFilter>().mesh;
SelfMesh = await InitMeshAsync(protoMesh);
}
protected async Task<CustomMesh> InitMeshAsync(Mesh protoMesh)
{
return await Task.Run(() => new CustomMesh(protoMesh.vertices, protoMesh.triangles));
}
最佳答案
通过查看异常,CustomMesh 使用的代码无法从单独的线程运行。 protoMesh.vertices, protoMesh.triangles
需要在开始新线程之前获取。
private async void Awake()
{
Mesh protoMesh = gameObject.GetComponent<MeshFilter>().mesh;
SelfMesh = await InitMeshAsync(protoMesh);
}
protected async Task<CustomMesh> InitMeshAsync(Mesh protoMesh)
{
var vertices = protoMesh.vertices;
var triangles = protoMesh.triangles;
return await Task.Run(() => new CustomMesh(vertices, triangles));
}
因为什么可以从单独的线程运行,什么不能从一个单独的线程运行,这很棘手,你需要不断地查看给定的字段/方法是如何实现的,但一个好的经验法则是始终复制基类型的请求字段像 int
, string
, float[]
before 你开始另一个线程。
如果你愿意去 GitHub您会看到 vertices
使用 GetAllocArrayFromChannel
,它使用 GetAllocArrayFromChannelImpl
,这是不能从单独线程使用的 native 方法。
https://stackoverflow.com/questions/63846965/
相关文章:
python - 无法通过 Pip 安装 TensorFlow
r - 如何使用 R 中的 rcv 和 networkD3 包在 D3 Sankey 图的节点中添加
android - 约束布局 : center view on full screen but li
python - 在使用 pyspark 和预定义的结构模式读取嵌套的 JSON 时,如何将缺失的列
python-3.x - python 3.8 flask apache 2.4 wsgi 多处理运
reactjs - 我怎样才能拥有同一个顶级片段容器的多个兄弟实例?