using UnityEngine;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
public class GetBs : MonoBehaviour
{
//数据传输部分
Thread receiveThread;
UdpClient client;
public int port = 5054;
public bool startRecieving = true;
public bool printToConsole = false;
public string data;
public bool isReceive;
public IEnumerator Start()
{
this.GetComponent<GetBs>().enabled = false;
yield return new WaitForSeconds(0.5f);
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
isReceive = true;
}
public void Update()
{
//data为接收到的数据
}
//接收数据的方法
private void ReceiveData()
{
client = new UdpClient(port);
while (startRecieving)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] dataByte = client.Receive(ref anyIP);
data = Encoding.UTF8.GetString(dataByte);
if (printToConsole) { print(data); }
}
catch (Exception err)
{
print(err.ToString());
}
}
}
}