Unity3D线程和GameObjects

时间:2014-07-06 20:46:15

标签: multithreading unity3d gameobject

我在Unity3D中有一个应用程序(充当服务器),它接收来自外部应用程序(单个客户端)的消息,具有以下结构:

number(float)number(float)number(float)

前两个数字表示局部位置(x,z轴),最后一个数字表示旋转值(y轴)。

目标是使用此数据更新游戏场景中的Camera gameobject位置(使用LoadPositions方法)。从我读到的操作游戏对象,而外部Unity的主线程是不可能的。

有了这样说,我怎样才能改变Unity主线程,以便我可以监听消息并更新游戏对象的位置。 此外,任何人碰巧都知道Unity中一个简单TCP服务器的工作示例,而不必求助于线程?

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

public class ProxyThreadServer : MonoBehaviour {

float x; 
float z; 
float rot;

Vector3 updatePos;
Vector3 updateRot;

string ip_address  = "127.0.0.1";
string msgReceived;
string[] words;

int wordsNum; 
int port = 8000;
int numSurf;
int jumpInterval;

Thread listen_thread;
TcpListener tcp_listener;
Thread clientThread;
TcpClient tcp_client;
bool isTrue = true;

// Use this for initialization
void Start () 
{
    IPAddress ip_addy = IPAddress.Parse(ip_address);
    tcp_listener = new TcpListener(ip_addy, port);
    listen_thread = new Thread(new ThreadStart(ListenForClients));
    listen_thread.Start();
}

private void ListenForClients()
{
    this.tcp_listener.Start();

    while(isTrue == true)   
    {
        //blocks until a client has connected to the server
        TcpClient client = this.tcp_listener.AcceptTcpClient();

        //create a thread to handle communication 
        //with connected client
        clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
        clientThread.Start(client);

        Debug.Log("Got client " + client);

    }
}

private void HandleClientComm(object client)
{
    tcp_client = (TcpClient)client;
    NetworkStream client_stream = tcp_client.GetStream();

    byte[] message = new byte[4096];
    int bytes_read;

    while(isTrue == true)
    {
        bytes_read = 0;

        try
        {
            //blocks until a client sends a message
            bytes_read = client_stream.Read(message, 0, 4096);
            //Debug.Log(message);

        }
        catch (Exception e)
        {
            //a socket error has occurred
            Debug.Log(e.Message);
            break;
        }

        if(bytes_read == 0)
        {
            //client has disconnected
            Debug.Log("Disconnected");
            tcp_client.Close();
            break;
        }

        ASCIIEncoding encoder = new ASCIIEncoding();
        Debug.Log(encoder.GetString(message,0,bytes_read));

        msgReceived = encoder.GetString(message,0,bytes_read);
        LoadPositions(msgReceived);
    }

    if(isTrue == false)
    {
        tcp_client.Close();
        Debug.Log("closing tcp client");
    }

}

void OnApplicationQuit()
{
    try
    {
        tcp_client.Close();
        isTrue = false;
    }
    catch(Exception e)
    {
        Debug.Log(e.Message);
    }

    // You must close the tcp listener
    try
    {
        tcp_listener.Stop();
        isTrue = false;
    }
    catch(Exception e)
    {
        Debug.Log(e.Message);
    }
}


 void LoadPositions(string positions){

    // Split string on spaces. This will separate all the words.
    words = positions.Split(' ');
    wordsNum = words.Length;

    for (int i = 0; i <= wordsNum; i++) {

        x = float.Parse(words[0], System.Globalization.CultureInfo.InvariantCulture); 
        z = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture); 
        rot = float.Parse(words[2], System.Globalization.CultureInfo.InvariantCulture);

        Debug.Log("Reading position: " + "x: " + x + " z: " + z + " yRot: " + rot);

        updatePos = new Vector3(x, this.gameObject.transform.position.y, z);
        this.gameObject.transform.position = updatePos;

        updateRot = new Vector3(this.gameObject.transform.rotation.x, rot / 4, this.gameObject.transform.rotation.z);
        this.transform.localEulerAngles = updateRot;

        //UpdateCameraMatrix();
        //StartCoroutine(UpdateSurfs());
        }
   }

}

2 个答案:

答案 0 :(得分:1)

虽然我之前没有尝试过这样的事情,但假设存在如你所提到的限制,我的方法是使用队列来存储消息,然后按照它们进入的顺序处理它们团结线索。因此,不要在调用LoadPositions时将其添加到queue

pendingMessages.Enqueue(msgReceived);

然后在更新方法中处理它:

void Update()
{
    while (pendingMessages.Count() > 0)
        LoadPositions(pendingMessages.Dequeue());
}

答案 1 :(得分:0)

您可以使用.NET的异步TCP。它基于回调代理。 (使用它有点棘手)