MainPanel.cs 15 KB

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