Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections.Generic;
- using extOSC;
- public class OSCExampleCode : MonoBehaviour
- {
- #region Private Vars
- public OSCTransmitter Transmitter;
- public OSCReceiver Receiver;
- #endregion
- #region Unity Methods
- protected void Awake()
- {
- Receiver.Bind("/example", ReceiveCallback);
- }
- protected void Update()
- {
- var array = new int[] { 1, 2, 3, 4, 5, 6 };
- var bytes = IntArrayToBytes(array);
- var message = new OSCMessage("/example");
- message.AddValue(OSCValue.Blob(bytes));
- Transmitter.Send(message);
- }
- #endregion
- #region Public Methods
- public void ReceiveCallback(OSCMessage message)
- {
- var pattern = new OSCMatchPattern(OSCValueType.Blob);
- if (message.IsMatch(pattern))
- {
- var blobValue = message.Values[0];
- var bytes = blobValue.BlobValue;
- var array = BytesToIntArray(bytes);
- var debug = "Received: ";
- foreach (var item in array)
- {
- debug += item + ", ";
- }
- Debug.Log(debug);
- }
- }
- public static byte[] IntArrayToBytes(int[] array)
- {
- var bytes = new List<byte>(array.Length * 4);
- for (int i = 0; i < array.Length; i++)
- {
- var data = BitConverter.GetBytes(array[i]);
- if (BitConverter.IsLittleEndian)
- {
- Array.Reverse(data);
- }
- bytes.AddRange(data);
- }
- return bytes.ToArray();
- }
- public static int[] BytesToIntArray(byte[] bytes)
- {
- int count = bytes.Length / 4;
- var array = new int[count];
- for (int i = 0; i < count; i++)
- {
- var index = i * 4;
- if (BitConverter.IsLittleEndian)
- {
- Array.Reverse(bytes, index, 4);
- }
- array[i] = BitConverter.ToInt32(bytes, index);
- }
- return array;
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment