Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using MiniJSON;
public class UDPReceive : MonoBehaviour
{
CharactorActor c_actor;
int LOCAL_PORT = 22222;
static UdpClient udp;
Thread thread;
public string parameter;
public static Action<string> DataCallBack;
void Start ()
{
UDPReceive.DataCallBack += Method;
UDPStart();
}
public void UDPStart(){
udp = new UdpClient(LOCAL_PORT);
thread = new Thread(new ThreadStart(ThreadMethod));
thread.Start();
}
public void Method(string data){
parameter = data;
}
void OnApplicationQuit()
{
thread.Abort();
}
private static void ThreadMethod()
{
while(true)
{
IPEndPoint remoteEP = null;
byte[] data = udp.Receive(ref remoteEP);
string text = Encoding.ASCII.GetString(data);
DataCallBack(text);
//Debug.Log(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.