BLEView.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. public GameObject autoResetView;
  15. void Start()
  16. {
  17. smartBowHelper = SmartBowHelper.NewInstance();
  18. smartBowHelper.OnBluetoothModuleInited += () =>
  19. {
  20. UpdateConnectText();
  21. smartBowHelper.StartRotationSensor();
  22. smartBowHelper.StartShootingSensor();
  23. };
  24. smartBowHelper.OnBluetoothError += (error, message) =>
  25. {
  26. if (error == BluetoothError.ScanNotFoundTargetDevice)
  27. {
  28. PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
  29. return;
  30. }
  31. PopupMgr.ins.ShowTip(message);
  32. };
  33. smartBowHelper.OnBluetoothStatusChanged += (oldStatus, newStatus) =>
  34. {
  35. if (playerIndex == 0) //1号控制
  36. {
  37. if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected) SimulateMouseController.ins?.SetBleConnected(true);
  38. else SimulateMouseController.ins?.SetBleConnected(false);
  39. }
  40. UpdateConnectText();
  41. };
  42. smartBowHelper.OnRotationUpdate += (r) =>
  43. {
  44. if (playerIndex == 0) //1号控制
  45. {
  46. //new Vector4(0,Screen.width*0.5f,Screen.height,0)
  47. GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r, new Vector4(0, Screen.width * 0.5f, Screen.height, 0));
  48. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(r);
  49. }
  50. else if (playerIndex == 1) {
  51. GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r, new Vector4(Screen.width * 0.5f, Screen.width, Screen.height, 0));
  52. }
  53. };
  54. smartBowHelper.OnShooting += OnShot;
  55. smartBowHelper.OnFunctionKeyPress += () =>
  56. {
  57. //smartBowHelper.ResetAim();
  58. if (playerIndex == 0) //1号控制
  59. {
  60. if (GameObject.Find("AutoResetViewNewLeft")) return;
  61. GameObject resetView = Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetViewNew"));
  62. resetView.name = "AutoResetViewNewLeft";
  63. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  64. autoResetViewNewScript.setPosLeft();
  65. autoResetViewNewScript.action_OnDestroy += () => {
  66. smartBowHelper.ResetAim();
  67. };
  68. }
  69. else if (playerIndex == 1)
  70. {
  71. if (GameObject.Find("AutoResetViewNewRight")) return;
  72. GameObject resetView = Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetViewNew"));
  73. resetView.name = "AutoResetViewNewRight";
  74. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  75. autoResetViewNewScript.setPosRight();
  76. autoResetViewNewScript.action_OnDestroy += () => {
  77. smartBowHelper.ResetAim();
  78. };
  79. }
  80. };
  81. if (playerIndex == 0 && BluetoothWindows.IsWindows())
  82. BleWinHelper.RegisterTo(smartBowHelper.gameObject);
  83. btnConnect.onClick.AddListener(OnClick_Connect);
  84. btnGyr.onClick.AddListener(OnClick_CalibrateGyr);
  85. btnMag.onClick.AddListener(OnClick_CalibrateMag);
  86. }
  87. void Update()
  88. {
  89. //if (playerIndex == 0 && Input.GetKeyUp(KeyCode.Alpha1))
  90. //{
  91. // Debug.Log("Alpha1");
  92. // if (GameObject.Find("AutoResetViewNewLeft")) return;
  93. // GameObject resetView = Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetViewNew"));
  94. // resetView.name = "AutoResetViewNewLeft";
  95. // AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  96. // autoResetViewNewScript.setPosLeft();
  97. // autoResetViewNewScript.action_OnDestroy += () =>
  98. // {
  99. // smartBowHelper.ResetAim();
  100. // };
  101. //}
  102. //if (playerIndex == 1 && Input.GetKeyUp(KeyCode.Alpha2))
  103. //{
  104. // Debug.Log("Alpha2");
  105. // if (GameObject.Find("AutoResetViewNewRight")) return;
  106. // GameObject resetView = Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetViewNew"));
  107. // resetView.name = "AutoResetViewNewRight";
  108. // AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  109. // autoResetViewNewScript.setPosRight();
  110. // autoResetViewNewScript.action_OnDestroy += () =>
  111. // {
  112. // smartBowHelper.ResetAim();
  113. // };
  114. //}
  115. UpdateCalibrateGyrText();
  116. UpdateCalibrateMagText();
  117. UpdateBatteryText();
  118. }
  119. float _lastShotTime = 0;
  120. void OnShot(float speed)
  121. {
  122. if (Time.time - _lastShotTime < 1) return;
  123. _lastShotTime = Time.time;
  124. GameController.ins.aimCrossHairs[playerIndex].Shoot(speed);
  125. if (playerIndex == 0) //1号控制
  126. {
  127. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  128. {
  129. SB_EventSystem.ins.ClickMouse();
  130. }
  131. }
  132. }
  133. void UpdateConnectText()
  134. {
  135. Text t = btnConnect.GetComponentInChildren<Text>();
  136. var newStatus = smartBowHelper.GetBluetoothStatus();
  137. if (newStatus == SmartBowSDK.BluetoothStatusEnum.None) t.text = "<color=blue>未连接</color>(点击连接)";
  138. if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting) t.text = "<color=#FF670D>连接中</color>";
  139. if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected)
  140. {
  141. if (smartBowHelper.IsBluetoothModuleInited()) t.text = "<color=green>已连接</color>(点击断开)";
  142. else t.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  143. }
  144. }
  145. void UpdateCalibrateGyrText()
  146. {
  147. Text text = btnGyr.GetComponentInChildren<Text>();
  148. int progress = (int)(smartBowHelper.GetGyrProgress() * 100);
  149. string act = smartBowHelper.IsGyrCalibrating() ? "停止" : "开始";
  150. text.text = $"点击{act}陀螺仪校准({progress}%)";
  151. }
  152. void UpdateCalibrateMagText()
  153. {
  154. Text text = btnMag.GetComponentInChildren<Text>();
  155. if (smartBowHelper.IsMagCompleted()) text.text = $"<color=green>地磁计校准完成</color>(点击重置)";
  156. else
  157. {
  158. string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "<color=#FF670D>已重置</color>" : "点击重置";
  159. text.text = $"<color=blue>地磁计校准中</color>({tip})";
  160. }
  161. }
  162. void UpdateBatteryText()
  163. {
  164. //更新显示电量
  165. int battery = smartBowHelper.GetBattery();
  166. textBattery.text = battery > 0 ? $"电量值:{battery}" : "未获得电量值";
  167. }
  168. void OnClick_Connect()
  169. {
  170. var s = smartBowHelper.GetBluetoothStatus();
  171. if (s == SmartBowSDK.BluetoothStatusEnum.Connecting) return;
  172. else if (s == SmartBowSDK.BluetoothStatusEnum.None) smartBowHelper.Connect();
  173. else if (s == SmartBowSDK.BluetoothStatusEnum.Connected) smartBowHelper.Disconnect();
  174. }
  175. void OnClick_CalibrateGyr()
  176. {
  177. if (smartBowHelper.IsGyrCalibrating()) smartBowHelper.StopGyrCalibration();
  178. else smartBowHelper.StartGyrCalibration();
  179. }
  180. float _clickCalibrateMagTime = -100;
  181. void OnClick_CalibrateMag()
  182. {
  183. _clickCalibrateMagTime = Time.realtimeSinceStartup;
  184. smartBowHelper.StartMagCalibration();
  185. }
  186. }