Unity3D子线程无法修改主线程对象状态

Unity Socket通信后无法修改对象状态

问题出现场景

Unity Socket编程,我在通信之后Unity端从服务器接收到了一串JSON字符,然后根据JSON的值去修改了GameManager.cs里的一个GameObject的属性,然后就报错了,我修改的是一个Text文本框里的文字。出的错误如下,意思大概就是不能在子线程里修改游戏主线程里的东西,这个也好理解,因为Socket通信本身是会有一个通信进程的,这个不是Unity的主进程而是C#的一个独立进程,所以出了问题。

错误提示如下:

Internal_CloneSingle 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.
UnityEngine.Debug:Log (object)
SocketManagerController:Received () (at Assets/Scripts/SocketManagerController.cs:121)
System.Threading.ThreadHelper:ThreadStart ()

解决方案

我不知道正常是这么解决的,因为我不是专业做Unity的,但是我发现Socket进程是可以修改基础类型的,比如:string,int,bool这些类型。
所以我的做法是在Socket线程里对

猜你喜欢

转载自blog.csdn.net/lwb314/article/details/128496045