相关疑难解决方法(0)

从另一个Thread使用Unity API或调用主Thread中的函数

我的问题是我尝试使用Unity套接字来实现一些东西.每次,当我收到新消息时,我需要将其更新为updattext(它是Unity Text).但是,当我执行以下代码时,void update不会每次都调用.

我没有包含updatetext.GetComponent<Text>().text = "From server: "+tempMesg;在void getInformation中的原因是这个函数在线程中,当我在getInformation()中包含它时会出现错误:

getcomponentfastpath can only be called from the main thread

我认为问题是我不知道如何在C#中运行主线程和子线程?或者可能还有其他问题...希望有人可以提供帮助..有我的代码:

using UnityEngine;
using System.Collections;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine.UI;


public class Client : MonoBehaviour {

    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
    private Thread oThread;

//  for UI update
    public GameObject updatetext;
    String tempMesg = "Waiting...";

    // Use this for initialization
    void Start () {
        updatetext.GetComponent<Text>().text = "Waiting...";
        clientSocket.Connect("10.132.198.29", 8888);
        oThread = new Thread (new ThreadStart …
Run Code Online (Sandbox Code Playgroud)

c# multithreading unity-game-engine

25
推荐指数
4
解决办法
2万
查看次数

标签 统计

c# ×1

multithreading ×1

unity-game-engine ×1