BluetoothDispatcher.cs 1013 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class BluetoothDispatcher : MonoBehaviour
  6. {
  7. public static System.Action<byte[]> aim;
  8. public static System.Action<byte[]> shoot;
  9. void Start()
  10. {
  11. BluetoothClient.onDataReceived = Dispatch;
  12. }
  13. void Dispatch(byte sign, byte[] data)
  14. {
  15. if (!IsWindows()) return;
  16. string logStr = sign + ", LEN " + data.Length;
  17. logStr += ", Bytes " + String.Join(",", data);
  18. Debug.Log(logStr);
  19. if (sign == 0 && aim != null)
  20. {
  21. aim(data);
  22. }
  23. else if (sign == 1 && shoot != null)
  24. {
  25. shoot(data);
  26. }
  27. }
  28. int platformID = -1;
  29. void SetPlatformID()
  30. {
  31. if (Application.platform == RuntimePlatform.WindowsEditor) platformID = 1;
  32. else platformID = 2;
  33. }
  34. bool IsWindows()
  35. {
  36. if (platformID == -1) SetPlatformID();
  37. return platformID == 1;
  38. }
  39. }