MainPanel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace WildAttack
  7. {
  8. public class MainPanel : MonoBehaviour
  9. {
  10. #region Members
  11. [SerializeField] Button _btnConnect;
  12. [SerializeField] Text textMacAddress;
  13. [SerializeField] Text textBattery;
  14. [SerializeField] Button _btnCalibrateGyr;
  15. [SerializeField] Button _btnCalibrateMag;
  16. [SerializeField] Button _btnResetAim;
  17. [SerializeField] Button _btnStartRotationSensor;
  18. [SerializeField] Button _btnStopRotationSensor;
  19. [SerializeField] Button _btnStartShootingSensor;
  20. [SerializeField] Button _btnStopShootingSensor;
  21. [SerializeField] Button _btnChangeShootType;
  22. [SerializeField] InputField _inputFieldGameID;
  23. [SerializeField] InputField _inputFieldChannelID;
  24. [SerializeField] InputField _inputFieldUsername;
  25. [SerializeField] InputField _inputFieldPassword;
  26. [SerializeField] Button _btnLogin;
  27. [SerializeField] Button _btnLogout;
  28. [SerializeField] Button _btnGetUserInfo;
  29. [SerializeField] Image _imgAvatar;
  30. [SerializeField] Text _textNickname;
  31. [SerializeField] Text _textGender;
  32. [SerializeField] Text btnConnectText;
  33. [SerializeField] GameObject _processObj;
  34. [SerializeField] Text _textProcessTitle;
  35. [SerializeField] Text _textProcess;
  36. [SerializeField] Text _textEnemy;
  37. [SerializeField] Text _textHighScore;
  38. [SerializeField] Text _textHpBarTitle;
  39. [SerializeField] Button _btnQuit;
  40. #endregion
  41. #region Lifecycle
  42. // Start is called before the first frame update
  43. void Start()
  44. {
  45. btnConnectText.text = "<color=blue>未连接</color>(点击连接)";
  46. _textHighScore.text = StringModule.GetInstance().GetData("highScore") + ":";
  47. textBattery.color = new Color(1, 1, 1, 0);
  48. textMacAddress.color = new Color(1, 1, 1, 0);
  49. _textProcessTitle.text = StringModule.GetInstance().GetData("wave") + ":";
  50. Transform wall = GameObject.Find("Wall").transform;
  51. var wallScreenPos = Camera.main.WorldToScreenPoint(wall.position);
  52. RegistEvent();
  53. }
  54. // Update is called once per frame
  55. //void Update()
  56. //{
  57. // UpdateProcess();
  58. // //更新显示陀螺仪校准文本
  59. // UpdateCalibrateGyrText();
  60. // //更新显示地磁计校准文本
  61. // UpdateCalibrateMagText();
  62. //}
  63. #endregion
  64. #region ButtonClickEvent
  65. void RegistEvent()
  66. {
  67. // SDK
  68. //SmartBowHelper.GetInstance().OnBluetoothModuleInited += OnBluetoothModuleInited;
  69. //SmartBowHelper.GetInstance().OnBluetoothStatusChanged += OnBluetoothStatusChanged;
  70. // SDK相关btn
  71. //_btnConnect.onClick.AddListener(OnClick_Connect);
  72. //_btnCalibrateGyr.onClick.AddListener(OnClick_CalibrateGyr);
  73. //_btnCalibrateMag.onClick.AddListener(OnClick_CalibrateMag);
  74. _btnResetAim.onClick.AddListener(OnClick_ResetAim);
  75. //_btnStopRotationSensor.onClick.AddListener(OnClick_StopRotationSensor);
  76. //_btnStopShootingSensor.onClick.AddListener(OnClick_StopShootingSensor);
  77. //_btnLogin.onClick.AddListener(OnClick_Login);
  78. //_btnLogout.onClick.AddListener(OnClick_Logout);
  79. //_btnGetUserInfo.onClick.AddListener(OnClick_GetUserInfo);
  80. _btnQuit.onClick.AddListener(OnClick_Quit);
  81. }
  82. /// <summary>
  83. /// 退出游戏
  84. /// </summary>
  85. private void OnClick_Quit()
  86. {
  87. //#if UNITY_EDITOR
  88. // UnityEditor.EditorApplication.isPlaying = false;
  89. //#else
  90. // Application.Quit();
  91. //#endif
  92. AudioMgr.ins.PlayBtn();
  93. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  94. }
  95. //public void OnClick_Connect()
  96. //{
  97. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connecting) return;
  98. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None)
  99. // {
  100. // SmartBowHelper.GetInstance().Connect();
  101. // return;
  102. // }
  103. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connected)
  104. // {
  105. // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited())
  106. // {
  107. // SmartBowHelper.GetInstance().Disconnect();
  108. // return;
  109. // }
  110. // else
  111. // {
  112. // return;
  113. // }
  114. // }
  115. //}
  116. //public void OnClick_CalibrateGyr()
  117. //{
  118. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() != BluetoothStatusEnum.Connected
  119. // || !SmartBowHelper.GetInstance().IsBluetoothModuleInited()) return;
  120. // if (SmartBowHelper.GetInstance().IsGyrCalibrating())
  121. // {
  122. // SmartBowHelper.GetInstance().StopGyrCalibration();
  123. // }
  124. // else
  125. // {
  126. // SmartBowHelper.GetInstance().StartGyrCalibration();
  127. // }
  128. //}
  129. ///// <summary>
  130. ///// 地磁计校准
  131. ///// </summary>
  132. //float _clickCalibrateMagTime = -100;
  133. //public void OnClick_CalibrateMag()
  134. //{
  135. // _clickCalibrateMagTime = Time.realtimeSinceStartup;
  136. // SmartBowHelper.GetInstance().StartMagCalibration();
  137. //}
  138. ////private bool isResetAim = false;
  139. public void OnClick_ResetAim()
  140. {
  141. // todo: 不连接时 归位正中间
  142. // UI改回1280 归位按钮放大
  143. AudioMgr.ins.PlayBtn();
  144. AutoResetView.DoIdentity();
  145. //TipText.Show("OnClick_ResetAim");
  146. //isResetAim = true;
  147. }
  148. //public void OnClick_StopRotationSensor()
  149. //{
  150. // bool success = SmartBowHelper.GetInstance().StopRotationSensor();
  151. // if (!success) TipText.Show("停止九轴传感失败\n原因\n模块未连接或未初始化完成");
  152. // else TipText.Show("停止九轴传感\n指令发送成功");
  153. // // 移除监听
  154. // SmartBowHelper.GetInstance().OnRotationUpdate -= GameMananger.GetInstance().OnRotationUpdate;
  155. //}
  156. //public void OnClick_StopShootingSensor()
  157. //{
  158. // bool success = SmartBowHelper.GetInstance().StopShootingSensor();
  159. // if (!success) TipText.Show("停止射箭传感失败\n原因\n模块未连接或未初始化完成");
  160. // else TipText.Show("停止射箭传感\n指令发送成功");
  161. // // 移除onshooting监听
  162. // SmartBowHelper.GetInstance().OnShooting -= GameMananger.GetInstance().OnShooting;
  163. //}
  164. /// <summary>
  165. /// 切换射击模式
  166. /// </summary>
  167. //private void OnClick_ChangeShootType()
  168. //{
  169. // ShootTypeEnum type = GameMananger.GetInstance().GetShootType();
  170. // if (type == ShootTypeEnum.FixedScreen)
  171. // {
  172. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.FixedCrossHair);
  173. // }
  174. // else if (type == ShootTypeEnum.FixedCrossHair)
  175. // {
  176. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.moreScreen);
  177. // }
  178. // else if (type == ShootTypeEnum.moreScreen)
  179. // {
  180. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.FixedScreen);
  181. // }
  182. // TipText.Show(GameMananger.GetInstance().GetShootType().ToString());
  183. //}
  184. //public void OnClick_Login()
  185. //{
  186. // if (string.IsNullOrWhiteSpace(_inputFieldUsername.text) || string.IsNullOrWhiteSpace(_inputFieldPassword.text))
  187. // {
  188. // TipText.Show("请输入账号密码");
  189. // return;
  190. // }
  191. // SmartBowHelper.GetInstance().Login(
  192. // _inputFieldGameID.text,
  193. // _inputFieldChannelID.text,
  194. // _inputFieldUsername.text,
  195. // _inputFieldPassword.text,
  196. // (res) =>
  197. // {
  198. // if (res.success)
  199. // {
  200. // Debug.Log("登录成功");
  201. // UpdateLoginUI(1);
  202. // }
  203. // TipText.Show(res.message);
  204. // });
  205. //}
  206. //public void OnClick_Logout()
  207. //{
  208. // SmartBowHelper.GetInstance().Logout((success) =>
  209. // {
  210. // if (success)
  211. // {
  212. // TipText.Show($"登出成功");
  213. // UpdateLoginUI(0);
  214. // }
  215. // else TipText.Show($"登出失败");
  216. // });
  217. //}
  218. ///// <summary>
  219. ///// 获取用户信息按钮点击事件
  220. ///// </summary>
  221. //public void OnClick_GetUserInfo()
  222. //{
  223. // SmartBowHelper.GetInstance().GetUserInfo((res) =>
  224. // {
  225. // if (res.success)
  226. // {
  227. // Debug.Log("获取用户信息成功");
  228. // UserInfo userInfo = res.userInfo;
  229. // //渲染UI
  230. // StartCoroutine(SmartBowNetwork.LoadSprite(userInfo.avatarUrl, (sprite) =>
  231. // {
  232. // _imgAvatar.sprite = sprite;
  233. // _imgAvatar.enabled = true;
  234. // }));
  235. // _textNickname.text = userInfo.nickname;
  236. // _textNickname.enabled = true;
  237. // _textGender.text = userInfo.gender == 1 ? "男" : "女";
  238. // _textGender.enabled = true;
  239. // // todo: 更新用户头像,保存最高分 GameOverPanel也用得到
  240. // GameMananger.GetInstance().SetHighScore(0);
  241. // }
  242. // TipText.Show(res.message);
  243. // });
  244. //}
  245. //#endregion
  246. //#region Functions
  247. ///// <summary>
  248. ///// 登录相关文字更新
  249. ///// </summary>
  250. ///// <param name="step"></param>
  251. //void UpdateLoginUI(int step)
  252. //{
  253. // _inputFieldGameID.gameObject.SetActive(step == 0);
  254. // _inputFieldChannelID.gameObject.SetActive(step == 0);
  255. // _inputFieldUsername.gameObject.SetActive(step == 0);
  256. // _inputFieldPassword.gameObject.SetActive(step == 0);
  257. // _btnLogin.gameObject.SetActive(step == 0);
  258. // _btnLogout.gameObject.SetActive(step == 1);
  259. // _btnGetUserInfo.gameObject.SetActive(step == 1);
  260. // _imgAvatar.gameObject.SetActive(step == 1);
  261. // _textNickname.gameObject.SetActive(step == 1);
  262. // _textGender.gameObject.SetActive(step == 1);
  263. // _imgAvatar.enabled = false;
  264. // _imgAvatar.sprite = null;
  265. // _textNickname.enabled = false;
  266. // _textGender.enabled = false;
  267. //}
  268. ///// <summary>
  269. ///// 陀螺仪文字更新
  270. ///// </summary>
  271. //void UpdateCalibrateGyrText()
  272. //{
  273. // Text text = _btnCalibrateGyr.GetComponentInChildren<Text>();
  274. // int progress = (int)(SmartBowHelper.GetInstance().GetGyrProgress() * 100);
  275. // string act = SmartBowHelper.GetInstance().IsGyrCalibrating() ? "停止" : "开始";
  276. // text.text = $"点击{act}陀螺仪校准({progress}%)";
  277. //}
  278. ///// <summary>
  279. ///// 地磁计文字更新
  280. ///// </summary>
  281. //void UpdateCalibrateMagText()
  282. //{
  283. // Text text = _btnCalibrateMag.GetComponentInChildren<Text>();
  284. // if (SmartBowHelper.GetInstance().IsMagCompleted())
  285. // {
  286. // text.text = $"<color=green>校准完成</color>(点击重置)";
  287. // }
  288. // else
  289. // {
  290. // string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "<color=#FF670D>已重置</color>" : "点击重置";
  291. // text.text = $"<color=blue>自动校准中</color>({tip})";
  292. // }
  293. //}
  294. ///// <summary>
  295. ///// 进度文字更新
  296. ///// </summary>
  297. //public void UpdateProcess()
  298. //{
  299. // if (ProcessManager.GetInstance().CurrLevel <= 0) return;
  300. // _textProcess.text = $"{(ProcessManager.GetInstance().CurrLevel > ProcessModule.GetInstance().processDataDic.Count ? ProcessModule.GetInstance().processDataDic.Count : ProcessManager.GetInstance().CurrLevel)}/{ProcessModule.GetInstance().processDataDic.Count}";
  301. //}
  302. public void SetProcessInfoActive(bool active)
  303. {
  304. _processObj.gameObject.SetActive(active);
  305. _textEnemy.gameObject.SetActive(active);
  306. }
  307. //private void OnBluetoothModuleInited()
  308. //{
  309. // textBattery.color = new Color(1, 1, 1, 1);
  310. // textMacAddress.color = new Color(1, 1, 1, 1);
  311. // // 初始化完成
  312. // // 真 连接成功 文本表现 见OnBluetoothStatusChanged注释
  313. // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited()) btnConnectText.text = "<color=green>已连接</color>(点击断开)";
  314. // else btnConnectText.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  315. // // 开始get电量和macAdd (在update处理
  316. //}
  317. ///// <summary>
  318. ///// 蓝牙状态变化
  319. ///// </summary>
  320. ///// <param name="oldStatus"></param>
  321. ///// <param name="newStatus"></param>
  322. //void OnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  323. //{
  324. // if (newStatus == BluetoothStatusEnum.None) btnConnectText.text = "<color=blue>未连接</color>(点击连接)";
  325. // if (newStatus == BluetoothStatusEnum.Connecting) btnConnectText.text = "<color=#FF670D>连接中</color>";
  326. // if (newStatus == BluetoothStatusEnum.Connected) btnConnectText.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  327. //}
  328. #endregion
  329. }
  330. }