Skip to content

Instantly share code, notes, and snippets.

@tm8r
Created May 24, 2016 10:07
Show Gist options
  • Save tm8r/f5e547d6659e403c0292356c80bf6893 to your computer and use it in GitHub Desktop.
Save tm8r/f5e547d6659e403c0292356c80bf6893 to your computer and use it in GitHub Desktop.
UnityでUDP受信するやつ
using UnityEngine;
using UniRx;
using UdpReceiverUniRx;
using System.IO;
namespace Viewer
{
public class UDPManager : SingletonMonoBehaviour<UDPManager>
{
public UdpReceiverRx _udpReceiverRx;
private IObservable<UdpState> myUdpSequence;
void Start ()
{
myUdpSequence = _udpReceiverRx._udpSequence;
myUdpSequence
.ObserveOnMainThread ()
.Subscribe (x => ReceiveAction (x.UdpMsg))
.AddTo (this);
}
void ReceiveAction (string message)
{
UdpMessage data = null;
try {
data = JsonUtility.FromJson<UdpMessage> (message);
} catch {
Debug.LogFormat ("[invalid format] message:{0}", message);
return;
}
switch (data.action) {
case "Load":
LoadModel (data);
break;
default:
Debug.LogFormat ("[not implemented action] message:{0}", message);
break;
}
}
void LoadModel (UdpMessage message)
{
var path = Directory.GetParent (message.path).Name + "/" + Path.GetFileNameWithoutExtension (message.path);
StartCoroutine (MenuPresenter.Instance.LoadModel (path));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment