Connexion | M’enregistrer




Poster un nouveau sujet Répondre au sujet  [ 3 messages ] 
Auteur Message
 Sujet du message: Unity3D OSC Processing scripts
UNREAD_POSTPosté: Mer Avr 27, 2011 7:29 am 
NooFondateur
Avatar de l’utilisateur
Inscription: Mar Jan 09, 2007 3:21 am
Messages: 1160
OSCTest Sender

Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


/// <summary>
/// Simple OSC test communication script
/// </summary>
[AddComponentMenu("Scripts/OSCTestSender")]
public class OSCTestSender : MonoBehaviour
{

    private Osc oscHandler;

    public string remoteIp;
    public int sendToPort;
    public int listenerPort;


    ~OSCTestSender()
    {
/*
Commented out by Pierre
        if (oscHandler != null)
        {           
            oscHandler.Cancel();
        }
*/
        // speed up finalization
        oscHandler = null;
        System.GC.Collect();
    }

    /// <summary>
    /// Update is called every frame, if the MonoBehaviour is enabled.
    /// </summary>
    void Update()
    {
        Debug.LogWarning("time = " + Time.time);       
//        OscMessage oscM = Osc.StringToOscMessage("/test1 TRUE 23 0.501 bla");
        OscMessage oscM = Osc.StringToOscMessage("/vibrate " + Random.Range(0, 255));
        oscHandler.Send(oscM); 
    }

    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    void Awake()
    {
       
    }

    void OnDisable()
    {
        // close OSC UDP socket
        Debug.Log("closing OSC UDP socket in OnDisable");
        oscHandler.Cancel();
        oscHandler = null;
    }

    /// <summary>
    /// Start is called just before any of the Update methods is called the first time.
    /// </summary>
    void Start()
    {
       
        UDPPacketIO udp = GetComponent<UDPPacketIO>();
        udp.init(remoteIp, sendToPort, listenerPort);
       
       oscHandler = GetComponent<Osc>();
        oscHandler.init(udp);
       
        oscHandler.SetAddressHandler("/hand1", Example);
    }

    public static void Example(OscMessage m)
    {
        Debug.Log("--------------> OSC example message received: ("+m+")");
    }



}


 Hors ligne
 
 Sujet du message: Re: Unity3D OSC Processing scripts
UNREAD_POSTPosté: Mer Avr 27, 2011 7:29 am 
NooFondateur
Avatar de l’utilisateur
Inscription: Mar Jan 09, 2007 3:21 am
Messages: 1160
UDPPacket IO
Code:
using System;
using System.IO;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;

  /// <summary>
  /// UdpPacket provides packetIO over UDP
  /// </summary>
  public class UDPPacketIO : MonoBehaviour
  {
    private UdpClient Sender;
    private UdpClient Receiver;
    private bool socketsOpen;
    private string remoteHostName;
    private int remotePort;
    private int localPort;

    void Start()
    {
        //do nothing. init must be called     
    }

     public void init(string hostIP, int remotePort, int localPort){
        RemoteHostName = hostIP;
        RemotePort = remotePort;
        LocalPort = localPort;
        socketsOpen = false;
     }
     

    ~UDPPacketIO()
    {
        // latest time for this socket to be closed
        if (IsOpen())
            Close();
    }

    /// <summary>
    /// Open a UDP socket and create a UDP sender.
    ///
    /// </summary>
    /// <returns>True on success, false on failure.</returns>
    public bool Open()
    {
        try
        {
            Sender = new UdpClient();
            //Debug.Log("opening udpclient listener on port " + localPort);

            IPEndPoint listenerIp = new IPEndPoint(IPAddress.Any, localPort);
            Receiver = new UdpClient(listenerIp);
            socketsOpen = true;
           
            return true;
        }
        catch (Exception e)
        {
            Debug.LogWarning("cannot open udp client interface at port "+localPort);
            Debug.LogWarning(e);
        }

        return false;
    }

    /// <summary>
    /// Close the socket currently listening, and destroy the UDP sender device.
    /// </summary>
    public void Close()
    {   
        if(Sender != null)
            Sender.Close();
       
        if (Receiver != null)
        {
            Receiver.Close();
            // Debug.Log("UDP receiver closed");
        }
        Receiver = null;
        socketsOpen = false;

    }

    public void OnDisable()
    {
        Close();
    }

    /// <summary>
    /// Query the open state of the UDP socket.
    /// </summary>
    /// <returns>True if open, false if closed.</returns>
    public bool IsOpen()
    {
      return socketsOpen;
    }

    /// <summary>
    /// Send a packet of bytes out via UDP.
    /// </summary>
    /// <param name="packet">The packet of bytes to be sent.</param>
    /// <param name="length">The length of the packet of bytes to be sent.</param>
    public void SendPacket(byte[] packet, int length)
    {   
        if (!IsOpen())
            Open();
        if (!IsOpen())
            return;
     
        Sender.Send(packet, length, remoteHostName, remotePort);
        Debug.Log("osc message sent to "+remoteHostName+" port "+remotePort+" len="+length);
    }

    /// <summary>
    /// Receive a packet of bytes over UDP.
    /// </summary>
    /// <param name="buffer">The buffer to be read into.</param>
    /// <returns>The number of bytes read, or 0 on failure.</returns>
    public int ReceivePacket(byte[] buffer)
    {
        if (!IsOpen())
            Open();
        if (!IsOpen())
            return 0;

      IPEndPoint iep = new IPEndPoint(IPAddress.Any, localPort);
      byte[] incoming = Receiver.Receive( ref iep );
      int count = Math.Min(buffer.Length, incoming.Length);
      System.Array.Copy(incoming, buffer, count);
      return count;
    }



    /// <summary>
    /// The address of the board that you're sending to.
    /// </summary>
    public string RemoteHostName
    {
      get
      {
        return remoteHostName;
      }
      set
      {
        remoteHostName = value;
      }
    }
 
    /// <summary>
    /// The remote port that you're sending to.
    /// </summary>
    public int RemotePort
    {
      get
      {
        return remotePort;
      }
      set
      {
        remotePort = value;
      }
    }

    /// <summary>
    /// The local port you're listening on.
    /// </summary>
    public int LocalPort
    {
      get
      {
        return localPort;
      }
      set
      {
        localPort = value;
      }
    }
}


 Hors ligne
 
 Sujet du message: Re: Unity3D OSC Processing scripts
UNREAD_POSTPosté: Mer Avr 27, 2011 7:31 am 
NooFondateur
Avatar de l’utilisateur
Inscription: Mar Jan 09, 2007 3:21 am
Messages: 1160
OSCReceiver
Code:
//You can set these variables in the scene because they are public
public var RemoteIP : String = "127.0.0.1";
public var SendToPort : int = 57131;
public var ListenerPort : int = 57130;
public var controller : Transform;
private var handler : Osc;



public function Start ()
{
   //Initializes on start up to listen for messages
   //make sure this game object has both UDPPackIO and OSC script attached
   var udp : UDPPacketIO = GetComponent("UDPPacketIO");
   udp.init(RemoteIP, SendToPort, ListenerPort);
   handler = GetComponent("Osc");
   handler.init(udp);
         
   handler.SetAddressHandler("/1/toggle1", Example1);
   handler.SetAddressHandler("/1/toggle2", Example2);
}
Debug.Log("Running");
//these fucntions are called when messages are received
public function Example1(oscMessage : OscMessage) : void
{   
   //How to access values:
   //oscMessage.Values[0], oscMessage.Values[1], etc
   Debug.Log("Called Example One > " + Osc.OscMessageToString(oscMessage));
}

//these fucntions are called when messages are received
public function Example2(oscMessage : OscMessage) : void
{
   //How to access values:
   //oscMessage.Values[0], oscMessage.Values[1], etc
   Debug.Log("Called Example Two > " + Osc.OscMessageToString(oscMessage));
}


 Hors ligne
 

Afficher les messages postés depuis:  Trier par  

Poster un nouveau sujet Répondre au sujet  [ 3 messages ] 


Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 1 invité

Panel

Haut Vous ne pouvez pas poster de nouveaux sujets
Vous ne pouvez pas répondre aux sujets
Vous ne pouvez pas éditer vos messages
Vous ne pouvez pas supprimer vos messages
Rechercher:
Aller à:  
 cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Traduction par: phpBB-fr.com
Dizayn Ercan Koc