DeviceReconnectView.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. /* 设备断线重连界面(同时也检测进入游戏时是否已经连接所需设备) */
  8. public class DeviceReconnectView : MonoBehaviour
  9. {
  10. [SerializeField] GameObject btnConnectBow;
  11. [SerializeField] GameObject btnConnectArrow;
  12. [SerializeField] Button btnClose;
  13. static DeviceReconnectView ins;
  14. public Action onComplete = null;
  15. public static GameObject Show() {
  16. if (SceneManager.GetActiveScene().name != "Game") return null;
  17. try {
  18. GameObject view = Resources.Load<GameObject>("Prefabs/Views/DeviceReconnectView");
  19. return GameObject.Instantiate(view);
  20. } catch (Exception) {}
  21. return null;
  22. }
  23. void Awake()
  24. {
  25. if (ins)
  26. {
  27. Destroy(this.gameObject);
  28. } else {
  29. ins = this;
  30. }
  31. }
  32. void Start()
  33. {
  34. InitBtnForConnect();
  35. RenderDeviceNames();
  36. GameMgr.ins.addLockerForGamePause(this);
  37. //用于测试阶段,暂时屏蔽该页面
  38. // Destroy(this.gameObject); if (onComplete != null) onComplete();
  39. }
  40. void OnDestroy()
  41. {
  42. GameMgr.ins.removeLockerForGamePause(this);
  43. }
  44. void Update()
  45. {
  46. UpdateBtnForConnect();
  47. btnClose.interactable = BluetoothStatus.IsAllConnected();
  48. }
  49. void RenderDeviceNames()
  50. {
  51. try {
  52. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  53. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  54. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  55. } catch (System.Exception) {}
  56. }
  57. void InitBtnForConnect()
  58. {
  59. btnConnectBow.GetComponent<Button>().onClick.AddListener(delegate() {
  60. BluetoothAim.ins.DoConnect();
  61. });
  62. btnConnectArrow.GetComponent<Button>().onClick.AddListener(delegate() {
  63. BluetoothShoot.ins.DoConnect();
  64. });
  65. }
  66. BluetoothStatusEnum bowStatus;
  67. BluetoothStatusEnum arrowStatus;
  68. void UpdateBtnForConnect() {
  69. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  70. bowStatus = BluetoothAim.ins.status;
  71. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  72. btnConnectBow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  73. btnConnectBow.GetComponentInChildren<Text>().color = color;
  74. }
  75. if (BluetoothShoot.ins && arrowStatus != BluetoothShoot.ins.status) {
  76. arrowStatus = BluetoothShoot.ins.status;
  77. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothShoot.ins.status);
  78. btnConnectArrow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  79. btnConnectArrow.GetComponentInChildren<Text>().color = color;
  80. }
  81. }
  82. public void BackHome() {
  83. AudioMgr.ins.PlayBtn();
  84. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  85. }
  86. public void Close() {
  87. AudioMgr.ins.PlayBtn();
  88. Destroy(this.gameObject);
  89. if (onComplete != null) {
  90. onComplete();
  91. }
  92. }
  93. }