LoginSignup
7
10

More than 5 years have passed since last update.

Unity: UDPを送る

Last updated at Posted at 2016-05-18

UDP Event -> BrightSign

動画プレイヤーとしてそれなりに有名だと思われるBrightSign
BrightAuthorを使って動画再生のプログラムを作ることもできる。あるイベントによって、動画再生を開始したり、違う動画を再生するといったものだ。イベントタイプの一つにUDPが用意されている。展示用に画像のようなシステムをつくった。
canon_wireless.png

UnityでUDP送信するiOSアプリの通信部分を以下で簡単に説明。

UDP Sender

UnityでUDPを受けるのは確か大変だった気がするが、送るのは簡単。このフォーラムの投稿のコードをほぼそのまま使うとできる。UDPを送るメソッドにはmodifier publicを付与すればinspector上でOnClickイベントで実行設定できる。

using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class UDPSender : MonoBehaviour {
    // UDP sender based on the code from the following url..http://forum.unity3d.com/threads/sending-data-using-udp.316834/
    public string IP = "255.255.255.255"; // default is local
    public int port = 5000;
    IPEndPoint remoteEndPoint;
    UdpClient client;

    // variables for timer
    public bool isTimerOn = false;
    float timerCounter = 180.0f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void GoBack() {
        IP = "255.255.255.255";
        port = 5000; // port number
        remoteEndPoint = new IPEndPoint (IPAddress.Parse (IP), port);

        client = new UdpClient ();
        byte[] data = Encoding.UTF8.GetBytes("Back"); // string message must be matched with the label name of the bright sign project.
        client.Send (data, data.Length, remoteEndPoint);
    }

    public void Projection(string targetScene) {
        IP = "255.255.255.255";
        port = 5000;
        remoteEndPoint = new IPEndPoint (IPAddress.Parse (IP), port);
        client = new UdpClient ();
        byte[] data = Encoding.UTF8.GetBytes (targetScene);
        client.Send (data, data.Length, remoteEndPoint);

    }
}
7
10
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10