BLEView.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using SmartBowSDK;
  6. public class BLEView : MonoBehaviour
  7. {
  8. public Button btnConnect;
  9. public Button btnGyr;
  10. public Button btnMag;
  11. public Text textBattery;
  12. public int playerIndex;
  13. SmartBowHelper smartBowHelper;
  14. void Start()
  15. {
  16. smartBowHelper = SmartBowHelper.NewInstance();
  17. smartBowHelper.OnBluetoothModuleInited += () =>
  18. {
  19. UpdateConnectText();
  20. smartBowHelper.StartRotationSensor();
  21. smartBowHelper.StartShootingSensor();
  22. };
  23. smartBowHelper.OnBluetoothError += (error, message) =>
  24. {
  25. if (error == BluetoothError.ScanNotFoundTargetDevice)
  26. {
  27. PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
  28. return;
  29. }
  30. PopupMgr.ins.ShowTip(message);
  31. };
  32. smartBowHelper.OnBluetoothStatusChanged += (oldStatus, newStatus) =>
  33. {
  34. if (playerIndex == 0) //1号控制
  35. {
  36. if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected) SimulateMouseController.ins?.SetBleConnected(true);
  37. else SimulateMouseController.ins?.SetBleConnected(false);
  38. }
  39. UpdateConnectText();
  40. };
  41. smartBowHelper.OnRotationUpdate += (r) =>
  42. {
  43. GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r);
  44. if (playerIndex == 0) //1号控制
  45. {
  46. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(r);
  47. }
  48. };
  49. smartBowHelper.OnShooting += OnShot;
  50. smartBowHelper.OnFunctionKeyPress += () =>
  51. {
  52. smartBowHelper.ResetAim();
  53. };
  54. if (playerIndex == 0 && BluetoothWindows.IsWindows())
  55. BleWinHelper.RegisterTo(smartBowHelper.gameObject);
  56. btnConnect.onClick.AddListener(OnClick_Connect);
  57. btnGyr.onClick.AddListener(OnClick_CalibrateGyr);
  58. btnMag.onClick.AddListener(OnClick_CalibrateMag);
  59. }
  60. void Update()
  61. {
  62. UpdateCalibrateGyrText();
  63. UpdateCalibrateMagText();
  64. UpdateBatteryText();
  65. }
  66. float _lastShotTime = 0;
  67. void OnShot(float speed)
  68. {
  69. if (Time.time - _lastShotTime < 1) return;
  70. _lastShotTime = Time.time;
  71. GameController.ins.aimCrossHairs[playerIndex].Shoot(speed);
  72. if (playerIndex == 0) //1号控制
  73. {
  74. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  75. {
  76. SB_EventSystem.ins.ClickMouse();
  77. }
  78. }
  79. }
  80. void UpdateConnectText()
  81. {
  82. Text t = btnConnect.GetComponentInChildren<Text>();
  83. var newStatus = smartBowHelper.GetBluetoothStatus();
  84. if (newStatus == SmartBowSDK.BluetoothStatusEnum.None) t.text = "<color=blue>未连接</color>(点击连接)";
  85. if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting) t.text = "<color=#FF670D>连接中</color>";
  86. if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected)
  87. {
  88. if (smartBowHelper.IsBluetoothModuleInited()) t.text = "<color=green>已连接</color>(点击断开)";
  89. else t.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  90. }
  91. }
  92. void UpdateCalibrateGyrText()
  93. {
  94. Text text = btnGyr.GetComponentInChildren<Text>();
  95. int progress = (int)(smartBowHelper.GetGyrProgress() * 100);
  96. string act = smartBowHelper.IsGyrCalibrating() ? "停止" : "开始";
  97. text.text = $"点击{act}陀螺仪校准({progress}%)";
  98. }
  99. void UpdateCalibrateMagText()
  100. {
  101. Text text = btnMag.GetComponentInChildren<Text>();
  102. if (smartBowHelper.IsMagCompleted()) text.text = $"<color=green>地磁计校准完成</color>(点击重置)";
  103. else
  104. {
  105. string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "<color=#FF670D>已重置</color>" : "点击重置";
  106. text.text = $"<color=blue>地磁计校准中</color>({tip})";
  107. }
  108. }
  109. void UpdateBatteryText()
  110. {
  111. //更新显示电量
  112. int battery = smartBowHelper.GetBattery();
  113. textBattery.text = battery > 0 ? $"电量值:{battery}" : "未获得电量值";
  114. }
  115. void OnClick_Connect()
  116. {
  117. var s = smartBowHelper.GetBluetoothStatus();
  118. if (s == SmartBowSDK.BluetoothStatusEnum.Connecting) return;
  119. else if (s == SmartBowSDK.BluetoothStatusEnum.None) smartBowHelper.Connect();
  120. else if (s == SmartBowSDK.BluetoothStatusEnum.Connected) smartBowHelper.Disconnect();
  121. }
  122. void OnClick_CalibrateGyr()
  123. {
  124. if (smartBowHelper.IsGyrCalibrating()) smartBowHelper.StopGyrCalibration();
  125. else smartBowHelper.StartGyrCalibration();
  126. }
  127. float _clickCalibrateMagTime = -100;
  128. void OnClick_CalibrateMag()
  129. {
  130. _clickCalibrateMagTime = Time.realtimeSinceStartup;
  131. smartBowHelper.StartMagCalibration();
  132. }
  133. }