L'utilisation de l'Unité de l'API à partir d'un autre Thread ou appeler une fonction dans le Thread principal

Mon problème est que j'essaie d'utiliser l'Unité prise pour mettre en œuvre quelque chose. Chaque fois, quand je reçois un nouveau message j'ai besoin de mettre à jour vers la updattext(c'est une Unité de Texte). Cependant, Quand je fais le code suivant, le vide mise à jour n'a pas d'appeler à chaque fois.

La raison pour laquelle je ne comprend pas updatetext.GetComponent<Text>().text = "From server: "+tempMesg;dans le vide getInformation est cette fonction est dans le fil, quand je l'inclure dans getInformation (), il viendra avec une erreur:

getcomponentfastpath can only be called from the main thread

Je pense que le problème c'est que je ne sais pas comment faire pour exécuter le thread principal et l'enfant thread en C# à l'ensemble? Il ya peut-être d'autres problèmes... j'Espère que quelqu'un peut aider..
Il y a mon code:

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 (getInformation));
oThread.Start ();
Debug.Log ("Running the client");
}
//Update is called once per frame
void Update () {
updatetext.GetComponent<Text>().text = "From server: "+tempMesg;
Debug.Log (tempMesg);
}
void getInformation(){
while (true) {
try {
NetworkStream networkStream = clientSocket.GetStream ();
byte[] bytesFrom = new byte[10025];
networkStream.Read (bytesFrom, 0, (int)bytesFrom.Length);
string dataFromClient = System.Text.Encoding.ASCII.GetString (bytesFrom);
dataFromClient = dataFromClient.Substring (0, dataFromClient.IndexOf ("$"));
Debug.Log (" >> Data from Server - " + dataFromClient);
tempMesg = dataFromClient;
string serverResponse = "Last Message from Server" + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes (serverResponse);
networkStream.Write (sendBytes, 0, sendBytes.Length);
networkStream.Flush ();
Debug.Log (" >> " + serverResponse);
} catch (Exception ex) {
Debug.Log ("Exception error:" + ex.ToString ());
oThread.Abort ();
oThread.Join ();
}
//         Thread.Sleep (500);
}
}
}

OriginalL'auteur user6142261 | 2016-12-26