Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity3d从线程访问游戏对象:InternalGetGameObject错误_C#_User Interface_Tcp_Unity3d - Fatal编程技术网

C# Unity3d从线程访问游戏对象:InternalGetGameObject错误

C# Unity3d从线程访问游戏对象:InternalGetGameObject错误,c#,user-interface,tcp,unity3d,C#,User Interface,Tcp,Unity3d,我正在使用自定义TCP套接字客户端类连接到服务器。当服务器响应特定的消息/命令时,我想更改活动菜单,但Unity3D告诉我无法从其他线程访问游戏对象,但无法从主线程访问 目前,我正在使用一个在IO中使用blockin的接收线程。我尝试使用BeginReceive接收消息并等待特定命令更改活动菜单,但它给了我同样恼人的错误 有没有简单而好的方法来解决这个问题?这是因为你不能从其他线程访问游戏对象。您可以在主线程中实现侦听器: void Start(){ StartCoroutine(Che

我正在使用自定义TCP套接字客户端类连接到服务器。当服务器响应特定的消息/命令时,我想更改活动菜单,但Unity3D告诉我无法从其他线程访问游戏对象,但无法从主线程访问

目前,我正在使用一个在IO中使用blockin的接收线程。我尝试使用BeginReceive接收消息并等待特定命令更改活动菜单,但它给了我同样恼人的错误


有没有简单而好的方法来解决这个问题?

这是因为你不能从其他线程访问游戏对象。您可以在主线程中实现侦听器:

void Start(){
    StartCoroutine(CheckOtherThreadsEveryFrame());
    StartCoroutine(CheckOtherThreadsOnceASecond());
}

IEnumerator CheckOtherThreadsEveryFrame(){
    while(true){
        //check if another thread has put some data where this method can see 
        yield return null; //wait until next frame      
    }
}
IEnumerator CheckOtherThreadsOnceASecond(){     
    while(true){            
        //check if another thread has put some data where this method can see           
        yield return new WaitForSeconds(1); //wait for 1 second
    }