DemoStarter.cs 11 KB

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