使用来自其他线程的统一API或调用主线程中的函数
updatetext.GetComponent<Text>().text = "From server: "+tempMesg;
getcomponentfastpath can only be called from the main thread
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;
慕慕森
相关分类