DemoStarter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using SmartBowSDK;
  4. using SmartBowSDK.CMD;
  5. public class DemoStarter : MonoBehaviour
  6. {
  7. [SerializeField] Toggle _DetectingMacToggle;
  8. [SerializeField] Button _btnConnect;
  9. [SerializeField] Toggle _CMDToggle;
  10. [SerializeField] Button _btnClearDeviceInfo;
  11. [SerializeField] Text textMacAddress;
  12. [SerializeField] Text textBattery;
  13. [SerializeField] Button _btnCalibrateGyr;
  14. [SerializeField] Button _btnCalibrateMag;
  15. [SerializeField] Button _btnResetAim;
  16. [SerializeField] Button _btnStartRotationSensor;
  17. [SerializeField] Button _btnStopRotationSensor;
  18. [SerializeField] Button _btnStartShootingSensor;
  19. [SerializeField] Button _btnStopShootingSensor;
  20. [SerializeField] InputField _inputFieldGameID;
  21. [SerializeField] InputField _inputFieldChannelID;
  22. [SerializeField] InputField _inputFieldUsername;
  23. [SerializeField] InputField _inputFieldPassword;
  24. [SerializeField] Button _btnLogin;
  25. [SerializeField] Button _btnLogout;
  26. [SerializeField] Button _btnGetUserInfo;
  27. [SerializeField] Image _imgAvatar;
  28. [SerializeField] Text _textNickname;
  29. [SerializeField] Text _textGender;
  30. //数据存储标识
  31. string userTags = "DemoStarter-Test";
  32. //建立的设备信息id.
  33. int deviceId = 1;
  34. void Awake()
  35. {
  36. #if UNITY_ANDROID
  37. _CMDToggle.onValueChanged.AddListener((bool isValue)=>{
  38. BluetoothStatusEnum bluetoothStatus = SmartBowHelper.GetInstance().GetBluetoothStatus();
  39. if (bluetoothStatus == BluetoothStatusEnum.None)
  40. {
  41. SmartBowHelper.GetInstance().UseCompanionDeviceManager(isValue);
  42. }
  43. else
  44. {
  45. TipText.Show("断开连接再切换!");
  46. }
  47. });
  48. #else
  49. _CMDToggle.gameObject.SetActive(false);
  50. #endif
  51. _btnConnect.onClick.AddListener(() => {
  52. OnClick_Connect();
  53. });
  54. _btnClearDeviceInfo.onClick.AddListener(OnClick_ClearDeviceInfo);
  55. _btnCalibrateGyr.onClick.AddListener(OnClick_CalibrateGyr);
  56. _btnCalibrateMag.onClick.AddListener(OnClick_CalibrateMag);
  57. _btnResetAim.onClick.AddListener(OnClick_ResetAim);
  58. _btnStartRotationSensor.onClick.AddListener(OnClick_StartRotationSensor);
  59. _btnStopRotationSensor.onClick.AddListener(OnClick_StopRotationSensor);
  60. _btnStartShootingSensor.onClick.AddListener(OnClick_StartShootingSensor);
  61. _btnStopShootingSensor.onClick.AddListener(OnClick_StopShootingSensor);
  62. _btnLogin.onClick.AddListener(OnClick_Login);
  63. _btnLogout.onClick.AddListener(OnClick_Logout);
  64. _btnGetUserInfo.onClick.AddListener(OnClick_GetUserInfo);
  65. UpdateLoginUI(0);
  66. }
  67. void Start()
  68. {
  69. SmartBowLogger.isDebug = true;
  70. #if UNITY_ANDROID
  71. //使用CMD API连接,这个只支持Android
  72. SmartBowHelper.GetInstance().UseCompanionDeviceManager(true);
  73. //这个状态是cmd扫描
  74. SmartBowHelper.GetInstance().OnCMDState += OnCMDScanState;
  75. #endif
  76. SmartBowHelper.GetInstance().OnBluetoothStatusChanged += OnBluetoothStatusChanged;
  77. //这里是传统蓝牙
  78. SmartBowHelper.GetInstance().OnBluetoothError += OnBluetoothError;
  79. SmartBowHelper.GetInstance().OnBluetoothModuleInited += OnBluetoothModuleInited;
  80. SmartBowHelper.GetInstance().OnRotationUpdate += OnRotationUpdate;
  81. SmartBowHelper.GetInstance().OnShooting += OnShooting;
  82. SmartBowHelper.GetInstance().OnFunctionKeyPress += OnFunctionKeyPress;
  83. SmartBowHelper.GetInstance().OnFunctionKeyLongPress += OnFunctionKeyLongPress;
  84. }
  85. void OnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  86. {
  87. }
  88. /// <summary>
  89. /// cmd扫描状态
  90. /// </summary>
  91. /// <param name="state"></param>
  92. /// <param name="message"></param>
  93. void OnCMDScanState(CMDScanState state, string message)
  94. {
  95. Debug.Log("CMDScanState:" + state);
  96. if (state == CMDScanState.TIMEOUT)
  97. {
  98. TipText.Show("连接失败,未发现目标设备!");
  99. return;
  100. }
  101. TipText.Show(message);
  102. }
  103. /// <summary>
  104. /// cmd没有通过这里扫描
  105. /// </summary>
  106. /// <param name="error"></param>
  107. /// <param name="message"></param>
  108. void OnBluetoothError(BluetoothError error, string message)
  109. {
  110. Debug.Log("OnBluetoothError:" + error);
  111. if (error == BluetoothError.ScanNotFoundTargetDevice)
  112. {
  113. TipText.Show("连接失败,未发现目标设备!");
  114. return;
  115. }
  116. TipText.Show(message);
  117. }
  118. void OnBluetoothModuleInited()
  119. {
  120. SmartBowHelper.GetInstance().StartRotationSensor();
  121. SmartBowHelper.GetInstance().StartShootingSensor();
  122. }
  123. void OnRotationUpdate(Quaternion rotation)
  124. {
  125. //Debug.Log($"Rotation update: {rotation}");
  126. Bow.Instance.transform.eulerAngles = rotation.eulerAngles;
  127. }
  128. void OnShooting(float speed)
  129. {
  130. Bow.Instance.Shoot();
  131. }
  132. void OnFunctionKeyPress()
  133. {
  134. Debug.Log("功能键短按");
  135. OnClick_ResetAim();
  136. }
  137. void OnFunctionKeyLongPress()
  138. {
  139. Debug.Log("功能键长按");
  140. }
  141. void Update()
  142. {
  143. //更新显示蓝牙状态
  144. BluetoothStatusEnum bluetoothStatus = SmartBowHelper.GetInstance().GetBluetoothStatus();
  145. Text btnConnectText = _btnConnect.GetComponentInChildren<Text>();
  146. if (bluetoothStatus == BluetoothStatusEnum.None) btnConnectText.text = "<color=blue>未连接</color>(点击连接)";
  147. if (bluetoothStatus == BluetoothStatusEnum.Connecting) btnConnectText.text = "<color=#FF670D>连接中</color>";
  148. if (bluetoothStatus == BluetoothStatusEnum.Connected)
  149. {
  150. if (SmartBowHelper.GetInstance().IsBluetoothModuleInited()) btnConnectText.text = "<color=green>已连接</color>(点击断开)";
  151. else btnConnectText.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  152. }
  153. //更新显示电量
  154. int battery = SmartBowHelper.GetInstance().GetBattery();
  155. textBattery.text = battery > 0 ? $"电量值:{battery}" : "未获得电量值";
  156. //更新显示Mac地址
  157. string macAddress = SmartBowHelper.GetInstance().GetMacAddress();
  158. textMacAddress.text = macAddress != null ? $"Mac地址:{macAddress}" : "未获得Mac地址";
  159. //更新显示陀螺仪校准文本
  160. UpdateCalibrateGyrText();
  161. //更新显示地磁计校准文本
  162. UpdateCalibrateMagText();
  163. }
  164. public void OnClick_Connect()
  165. {
  166. if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connecting) return;
  167. if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None)
  168. {
  169. AimDeviceInfo aimDeviceInfo = SmartBowHelper.GetInstance().GetDeviceInfo(userTags, deviceId);
  170. bool bRecreateDeviceInfo = aimDeviceInfo == null;
  171. if (aimDeviceInfo == null || !aimDeviceInfo.bInitMac)
  172. {
  173. Debug.Log(message: $"连接蓝牙-开始连接蓝牙:aimDeviceInfo:null,bRecreateDeviceInfo:{bRecreateDeviceInfo}");
  174. SmartBowHelper.GetInstance().SetIsConnectName(true);
  175. SmartBowHelper.GetInstance().Connect(userTags, deviceId, bRecreateDeviceInfo);
  176. }
  177. else
  178. {
  179. Debug.Log(message: $"连接蓝牙-开始连接蓝牙:aimDeviceInfo:mac:{aimDeviceInfo.mac} id:{aimDeviceInfo.id}");
  180. SmartBowHelper.GetInstance().SetIsConnectName(false);
  181. SmartBowHelper.GetInstance().SetConnectMacStr(aimDeviceInfo.mac);
  182. SmartBowHelper.GetInstance().Connect(userTags, deviceId);
  183. }
  184. //默认需要检测mac,bRecreateDeviceInfo:false.如不需要:设置bRecreateDeviceInfo:true;
  185. //bool bRecreateDeviceInfo = _DetectingMacToggle.isOn ? false : true;
  186. //SmartBowHelper.GetInstance().Connect(userTags, deviceId, bRecreateDeviceInfo);
  187. return;
  188. }
  189. if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connected)
  190. {
  191. SmartBowHelper.GetInstance().Disconnect();
  192. return;
  193. }
  194. }
  195. public void OnClick_ClearDeviceInfo() {
  196. SmartBowHelper.GetInstance().ClearDeviceInfo(userTags , deviceId);
  197. }
  198. public void OnClick_ResetAim()
  199. {
  200. SmartBowHelper.GetInstance().ResetAim();
  201. }
  202. public void OnClick_CalibrateGyr()
  203. {
  204. if (SmartBowHelper.GetInstance().IsGyrCalibrating())
  205. {
  206. SmartBowHelper.GetInstance().StopGyrCalibration();
  207. }
  208. else
  209. {
  210. SmartBowHelper.GetInstance().StartGyrCalibration();
  211. }
  212. }
  213. void UpdateCalibrateGyrText()
  214. {
  215. Text text = _btnCalibrateGyr.GetComponentInChildren<Text>();
  216. int progress = (int)(SmartBowHelper.GetInstance().GetGyrProgress() * 100);
  217. string act = SmartBowHelper.GetInstance().IsGyrCalibrating() ? "停止" : "开始";
  218. text.text = $"点击{act}陀螺仪校准({progress}%)";
  219. }
  220. float _clickCalibrateMagTime = -100;
  221. public void OnClick_CalibrateMag()
  222. {
  223. _clickCalibrateMagTime = Time.realtimeSinceStartup;
  224. SmartBowHelper.GetInstance().StartMagCalibration();
  225. }
  226. void UpdateCalibrateMagText()
  227. {
  228. Text text = _btnCalibrateMag.GetComponentInChildren<Text>();
  229. if (SmartBowHelper.GetInstance().IsMagCompleted())
  230. {
  231. text.text = $"<color=green>地磁计校准完成</color>(点击重置)";
  232. }
  233. else
  234. {
  235. string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "<color=#FF670D>已重置</color>" : "点击重置";
  236. text.text = $"<color=blue>地磁计自动校准中</color>({tip})";
  237. }
  238. }
  239. public void OnClick_StartRotationSensor()
  240. {
  241. bool success = SmartBowHelper.GetInstance().StartRotationSensor();
  242. if (!success) TipText.Show("开启九轴传感失败\n原因\n模块未连接或未初始化完成");
  243. else TipText.Show("开启九轴传感\n指令发送成功");
  244. }
  245. public void OnClick_StopRotationSensor()
  246. {
  247. bool success = SmartBowHelper.GetInstance().StopRotationSensor();
  248. if (!success) TipText.Show("停止九轴传感失败\n原因\n模块未连接或未初始化完成");
  249. else TipText.Show("停止九轴传感\n指令发送成功");
  250. }
  251. public void OnClick_StartShootingSensor()
  252. {
  253. bool success = SmartBowHelper.GetInstance().StartShootingSensor();
  254. if (!success) TipText.Show("开启射箭传感失败\n原因\n模块未连接或未初始化完成");
  255. else TipText.Show("开启射箭传感\n指令发送成功");
  256. }
  257. public void OnClick_StopShootingSensor()
  258. {
  259. bool success = SmartBowHelper.GetInstance().StopShootingSensor();
  260. if (!success) TipText.Show("停止射箭传感失败\n原因\n模块未连接或未初始化完成");
  261. else TipText.Show("停止射箭传感\n指令发送成功");
  262. }
  263. public void OnClick_Login()
  264. {
  265. if (string.IsNullOrWhiteSpace(_inputFieldUsername.text) || string.IsNullOrWhiteSpace(_inputFieldPassword.text))
  266. {
  267. TipText.Show("请输入账号密码");
  268. return;
  269. }
  270. SmartBowHelper.GetInstance().Login(
  271. _inputFieldGameID.text,
  272. _inputFieldChannelID.text,
  273. _inputFieldUsername.text,
  274. _inputFieldPassword.text,
  275. (res) =>
  276. {
  277. if (res.success)
  278. {
  279. Debug.Log("登录成功");
  280. UpdateLoginUI(1);
  281. }
  282. TipText.Show(res.message);
  283. });
  284. }
  285. public void OnClick_Logout()
  286. {
  287. SmartBowHelper.GetInstance().Logout((success) =>
  288. {
  289. if (success)
  290. {
  291. TipText.Show($"登出成功");
  292. UpdateLoginUI(0);
  293. }
  294. else TipText.Show($"登出失败");
  295. });
  296. }
  297. public void OnClick_GetUserInfo()
  298. {
  299. SmartBowHelper.GetInstance().GetUserInfo((res) =>
  300. {
  301. if (res.success)
  302. {
  303. Debug.Log("获取用户信息成功");
  304. // UserInfo userInfo = res.userInfo;
  305. //Debug.Log(JsonConvert.SerializeObject(userInfo));
  306. ////渲染UI
  307. //StartCoroutine(SmartBowNetwork.LoadSprite(userInfo.avatarUrl, (sprite) =>
  308. //{
  309. // _imgAvatar.sprite = sprite;
  310. // _imgAvatar.enabled = true;
  311. //}));
  312. //_textNickname.text = userInfo.nickname;
  313. //_textNickname.enabled = true;
  314. //_textGender.text = userInfo.gender == 1 ? "男" : "女";
  315. //_textGender.enabled = true;
  316. }
  317. TipText.Show(res.message);
  318. });
  319. }
  320. void UpdateLoginUI(int step)
  321. {
  322. _inputFieldGameID.gameObject.SetActive(step == 0);
  323. _inputFieldChannelID.gameObject.SetActive(step == 0);
  324. _inputFieldUsername.gameObject.SetActive(step == 0);
  325. _inputFieldPassword.gameObject.SetActive(step == 0);
  326. _btnLogin.gameObject.SetActive(step == 0);
  327. _btnLogout.gameObject.SetActive(step == 1);
  328. _btnGetUserInfo.gameObject.SetActive(step == 1);
  329. _imgAvatar.gameObject.SetActive(step == 1);
  330. _textNickname.gameObject.SetActive(step == 1);
  331. _textGender.gameObject.SetActive(step == 1);
  332. _imgAvatar.enabled = false;
  333. _imgAvatar.sprite = null;
  334. _textNickname.enabled = false;
  335. _textGender.enabled = false;
  336. }
  337. }