如何在Unity方法中使用UDP [英] How to use UDP with Unity methods
问题描述
我创建了一个Cube对象并附加了此脚本.
I created a Cube object and attached this script.
using UnityEngine;
using System.Collections;
public class CubeMove : MonoBehaviour {
void Start () {
}
void Update () {
}
public void Move () {
Vector3 moveVector = new Vector3(10, 0, 0);
transform.Translate(moveVector);
}
}
我想使用UDP来控制多维数据集的移动,所以我创建了UDPManager.
I wanted to use UDP to control cube move, so I created UDPManager.
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class UDPManager : MonoBehaviour
{
static UdpClient udp;
Thread thread;
public GameObject cube;
public CubeMove cubemove;
void Start ()
{
udp = new UdpClient(12345);
cubemove = cube.GetComponent<CubeMove>();
thread = new Thread(new ThreadStart(ThreadMethod));
thread.Start();
}
void Update ()
{
}
void OnApplicationQuit()
{
udp.Close();
thread.Abort();
}
private void ThreadMethod()
{
while(true)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Debug.Log(returnData);
if (returnData == "1\n") {
cube.SendMessage ("Move");
// or
cubemove.Move();
}
}
}
}
但是这些不适用于以下错误.
but these doesn't works with below errors.
- SendMessage can only be called from the main thread.
- get_transform can only be called from the main thread.
收到udp命令后可以调用统一方法吗?
Can I call unity methods when I receive udp command?
推荐答案
您不能从另一个线程调用Unity API.如何在Unity中使用线程:
You can't call Unity API from another Thread. How to use Thread in Unity:
1 .启动线程
2 .在该新线程中处理输入
2. Process Input in that new thread
3 .告诉Unity您已完成处理.您可以通过将全局boolean
变量设置为true
来实现.将output
数据存储在另一个全局变量中.
3. Tell Unity that you are done processing. You can do this by setting a global boolean
variable to true
. Store output
data in another global variable.
4 .检查Update()
函数中的boolean
变量是否已更改.如果设置为false
.处理输出...
4. Check if the the boolean
variable changed in the Update()
function. Set it false
if it did. Process output...
还将udp = new UdpClient(12345);
从Start
功能移到ThreadMethod
功能.
static readonly object lockObject = new object();
string returnData = "";
bool precessData = false;
void Start ()
{
cubemove = cube.GetComponent<CubeMove>();
thread = new Thread(new ThreadStart(ThreadMethod));
thread.Start();
}
void Update()
{
if (precessData)
{
/*lock object to make sure there data is
*not being accessed from multiple threads at thesame time*/
lock (lockObject)
{
precessData = false;
cube.SendMessage("Move");
// or
cubemove.Move();
//Process received data
Debug.Log("Received: " + returnData);
//Reset it for next read(OPTIONAL)
returnData = "";
}
}
}
private void ThreadMethod()
{
udp = new UdpClient(12345);
while (true)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint);
/*lock object to make sure there data is
*not being accessed from multiple threads at thesame time*/
lock (lockObject)
{
returnData = Encoding.ASCII.GetString(receiveBytes);
Debug.Log(returnData);
if (returnData == "1\n")
{
//Done, notify the Update function
precessData = true;
}
}
}
}
这篇关于如何在Unity方法中使用UDP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!