我的问题是我尝试使用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) 我需要在Unity中创建一个无限循环而不使用主线程?我看到了一些例子,但它并没有用:
while(true){
var aa;
debug.log("print.");
}
Run Code Online (Sandbox Code Playgroud)
我想添加一些延迟,例如2秒.如果有人知道解决方案请帮助.