Created
March 4, 2018 05:50
-
-
Save tak-km/1bd9860794434f35d2725f83b82e9987 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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