0

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

数字(浮点数) 数字(浮点数) 数字(浮点数)

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

目标是使用此数据更新游戏场景中的 Camera 游戏对象位置(使用 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());
        }
   }

}
4

2 回答 2

1

虽然我以前没有尝试过这样做,但假设确实存在您提到的限制,我的方法是使用队列来存储消息,然后按照它们进入统一线程的顺序处理它们。因此,与其调用 LoadPositions,不如将其添加到队列中

pendingMessages.Enqueue(msgReceived);

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

void Update()
{
    while (pendingMessages.Count() > 0)
        LoadPositions(pendingMessages.Dequeue());
}
于 2014-07-07T16:05:59.397 回答
0

您可以使用 .NET 的 Async TCP。它基于回调委托。(虽然使用它有点棘手)

于 2014-12-28T21:27:17.733 回答