Guest User

Untitled

a guest
Feb 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. using extOSC;
  7.  
  8. public class OSCExampleCode : MonoBehaviour
  9. {
  10. #region Private Vars
  11.  
  12. public OSCTransmitter Transmitter;
  13.  
  14. public OSCReceiver Receiver;
  15.  
  16. #endregion
  17.  
  18. #region Unity Methods
  19.  
  20. protected void Awake()
  21. {
  22. Receiver.Bind("/example", ReceiveCallback);
  23. }
  24.  
  25. protected void Update()
  26. {
  27. var array = new int[] { 1, 2, 3, 4, 5, 6 };
  28. var bytes = IntArrayToBytes(array);
  29.  
  30.  
  31. var message = new OSCMessage("/example");
  32. message.AddValue(OSCValue.Blob(bytes));
  33.  
  34. Transmitter.Send(message);
  35. }
  36.  
  37. #endregion
  38.  
  39. #region Public Methods
  40.  
  41. public void ReceiveCallback(OSCMessage message)
  42. {
  43. var pattern = new OSCMatchPattern(OSCValueType.Blob);
  44.  
  45. if (message.IsMatch(pattern))
  46. {
  47. var blobValue = message.Values[0];
  48. var bytes = blobValue.BlobValue;
  49.  
  50. var array = BytesToIntArray(bytes);
  51.  
  52. var debug = "Received: ";
  53.  
  54. foreach (var item in array)
  55. {
  56. debug += item + ", ";
  57. }
  58.  
  59. Debug.Log(debug);
  60. }
  61. }
  62.  
  63. public static byte[] IntArrayToBytes(int[] array)
  64. {
  65. var bytes = new List<byte>(array.Length * 4);
  66.  
  67. for (int i = 0; i < array.Length; i++)
  68. {
  69. var data = BitConverter.GetBytes(array[i]);
  70.  
  71. if (BitConverter.IsLittleEndian)
  72. {
  73. Array.Reverse(data);
  74. }
  75.  
  76. bytes.AddRange(data);
  77. }
  78.  
  79. return bytes.ToArray();
  80. }
  81.  
  82. public static int[] BytesToIntArray(byte[] bytes)
  83. {
  84. int count = bytes.Length / 4;
  85. var array = new int[count];
  86.  
  87. for (int i = 0; i < count; i++)
  88. {
  89. var index = i * 4;
  90.  
  91. if (BitConverter.IsLittleEndian)
  92. {
  93. Array.Reverse(bytes, index, 4);
  94. }
  95.  
  96. array[i] = BitConverter.ToInt32(bytes, index);
  97. }
  98.  
  99. return array;
  100. }
  101.  
  102. #endregion
  103. }
Add Comment
Please, Sign In to add comment