gpt4 book ai didi

c# - 从其他线程调用主线程中的方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:03:29 25 4
gpt4 key购买 nike

<分区>

我在 unity3d 控制台中出现这样的错误:

INTERNAL_CALL_Internal_InstantiateSingle 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.

我正在尝试的是似乎在主线程中定义的执行方法,如“Instantiate()”

如何调用这个方法或调用主线程来执行它?

编辑

protected static void processCommand(String data) {
...
} else if(data.StartsWith("LOGGEDIN")) {
...
Vector3 pos = new Vector3(0, 1.2F, 0);
Quaternion rot = Quaternion.identity;
myModel = (GameObject)Instantiate(player, pos, rot);
...

线程是:

Thread tidListen = new Thread(new ThreadStart(ListenThread));
tidListen.Start();

private static void ListenThread() {
while(connected) {
int lData = myStream.Read(myBuffer, 0, client.ReceiveBufferSize);
String myString = Encoding.ASCII.GetString(myBuffer);
myString = myString.Substring(0, lData);
processCommand(myString);
}
}

当我调用 processCommand 时,我有方法:Instantiate()

编辑 2

我也试过这个Link ,当我从 currentThread 获取 ManagedThreadId 时,我得到 1(这似乎是主线程),但我再次遇到同样的错误。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com