C# 通过UDP从本地服务器更新Unity3D中对象的变换组件
对于一些人来说,我的问题的解决方案可能看起来非常明显和简单,但是,不幸的是,我是Unity和C#的完全初学者(过去使用虚幻引擎,现在在Unity中有点迷失)。 基本上,我正在尝试做的是,通过服务器(不是Unity应用程序)通过UDP发送的数据来控制Unity中的对象(仅限位置和旋转)。因此,来自服务器的消息是变换组件的每个组件(x、y、z、俯仰、滚动、偏航)的值字符串。该字符串每帧更新一次(目前随机更新) 我一直在尝试采用大量不同的代码,但没有成功。 例如或 serve的源代码如下(不是我写的):C# 通过UDP从本地服务器更新Unity3D中对象的变换组件,c#,unity3d,networking,udp,C#,Unity3d,Networking,Udp,对于一些人来说,我的问题的解决方案可能看起来非常明显和简单,但是,不幸的是,我是Unity和C#的完全初学者(过去使用虚幻引擎,现在在Unity中有点迷失)。 基本上,我正在尝试做的是,通过服务器(不是Unity应用程序)通过UDP发送的数据来控制Unity中的对象(仅限位置和旋转)。因此,来自服务器的消息是变换组件的每个组件(x、y、z、俯仰、滚动、偏航)的值字符串。该字符串每帧更新一次(目前随机更新) 我一直在尝试采用大量不同的代码,但没有成功。 例如或 serve的源代码如下(不是我写的)
我明白,边做边学不是最好的策略,但是请帮我写下这个统一目标的脚本,我的时间非常短。提前谢谢。这是最后的工作脚本(但必须清理)。对象接收坐标并正确更新变换组件
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class PlayerBehavior : MonoBehaviour {
private UdpClient udpServer;
public GameObject cube;
private Vector3 tempPos;
private Thread t;
public float movementSpeed;
private long lastSend;
private IPEndPoint remoteEP;
private float[] transformPosition = new float[3] ;
void Start()
{
udpServer = new UdpClient(1234);
t = new Thread(() => {
while (true) {
this.receiveData();
}
});
t.Start();
t.IsBackground = true;
remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 41234);
}
void Update()
{
transform.position = new Vector3(transformPosition[0], transformPosition[1], transformPosition[2]) ;
}
private void OnApplicationQuit()
{
udpServer.Close();
t.Abort();
}
private void receiveData() {
byte[] data = udpServer.Receive(ref remoteEP);
if (data.Length > 0)
{
var str = System.Text.Encoding.Default.GetString(data);
Debug.Log("Received Data" + str);
string[] messageParts = str.Split(',');
transformPosition[0] = float.Parse(messageParts[0]) ;
transformPosition[1] = float.Parse(messageParts[1]) ;
transformPosition[2] = float.Parse(messageParts[2]) ;
}
}
}
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class PlayerBehavior : MonoBehaviour {
private UdpClient udpServer;
public GameObject cube;
private Vector3 tempPos;
private Thread t;
public float movementSpeed;
private long lastSend;
private IPEndPoint remoteEP;
private float[] transformPosition = new float[3] ;
void Start()
{
udpServer = new UdpClient(1234);
t = new Thread(() => {
while (true) {
this.receiveData();
}
});
t.Start();
t.IsBackground = true;
remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 41234);
}
void Update()
{
transform.position = new Vector3(transformPosition[0], transformPosition[1], transformPosition[2]) ;
}
private void OnApplicationQuit()
{
udpServer.Close();
t.Abort();
}
private void receiveData() {
byte[] data = udpServer.Receive(ref remoteEP);
if (data.Length > 0)
{
var str = System.Text.Encoding.Default.GetString(data);
Debug.Log("Received Data" + str);
string[] messageParts = str.Split(',');
transformPosition[0] = float.Parse(messageParts[0]) ;
transformPosition[1] = float.Parse(messageParts[1]) ;
transformPosition[2] = float.Parse(messageParts[2]) ;
}
}
}