Skip to content

Instantly share code, notes, and snippets.

@serinuntius
Created June 24, 2016 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serinuntius/dbedaf642fb633f6d524ad675bcfa325 to your computer and use it in GitHub Desktop.
Save serinuntius/dbedaf642fb633f6d524ad675bcfa325 to your computer and use it in GitHub Desktop.
UnityのUDPでメッセ受け取って、GUIに表示するやつ
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class udp_recieve_gui : MonoBehaviour {
int LOCA_LPORT = 22222;
static UdpClient udp;
Thread thread;
public static string text;
void Start()
{
udp = new UdpClient(LOCA_LPORT);
udp.Client.ReceiveTimeout = 100000;
thread = new Thread(new ThreadStart(ThreadMethod));
thread.Start();
}
void Update()
{
}
void OnGUI()
{
if (text != "")
{
GUI.Label(new Rect(10, 10, 150, 100), text);
}
}
void OnApplicationQuit()
{
thread.Abort();
}
private static void ThreadMethod()
{
while (true)
{
IPEndPoint remoteEP = null;
byte[] data = udp.Receive(ref remoteEP);
text = Encoding.UTF8.GetString(data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment