Skip to content

Instantly share code, notes, and snippets.

@tm8r
Created May 24, 2016 10:07
Show Gist options
  • Select an option

  • Save tm8r/f5e547d6659e403c0292356c80bf6893 to your computer and use it in GitHub Desktop.

Select an option

Save tm8r/f5e547d6659e403c0292356c80bf6893 to your computer and use it in GitHub Desktop.

Revisions

  1. tm8r created this gist May 24, 2016.
    49 changes: 49 additions & 0 deletions UDPManager.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    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));
    }
    }
    }