BLEView.cs 8.9 KB

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