DeviceReconnectView.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. string sceneName = SceneManager.GetActiveScene().name;
  17. if (!sceneName.StartsWith("Game")) return null;
  18. try {
  19. GameObject view = Resources.Load<GameObject>("Prefabs/Views/DeviceReconnectView");
  20. return GameObject.Instantiate(view);
  21. } catch (Exception) {}
  22. return null;
  23. }
  24. void Awake()
  25. {
  26. if (ins)
  27. {
  28. Destroy(this.gameObject);
  29. } else {
  30. ins = this;
  31. }
  32. }
  33. void Start()
  34. {
  35. InitBtnForConnect();
  36. RenderDeviceNames();
  37. GameMgr.ins.addLockerForGamePause(this);
  38. //用于测试阶段,暂时屏蔽该页面
  39. Destroy(this.gameObject); if (onComplete != null) onComplete();
  40. }
  41. void OnDestroy()
  42. {
  43. GameMgr.ins.removeLockerForGamePause(this);
  44. }
  45. void Update()
  46. {
  47. UpdateBtnForConnect();
  48. btnClose.interactable = BluetoothStatus.IsAllConnected();
  49. }
  50. void RenderDeviceNames()
  51. {
  52. try {
  53. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  54. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  55. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  56. } catch (System.Exception) {}
  57. }
  58. void InitBtnForConnect()
  59. {
  60. btnConnectBow.GetComponent<Button>().onClick.AddListener(delegate() {
  61. BluetoothAim.ins.DoConnect();
  62. });
  63. btnConnectArrow.GetComponent<Button>().onClick.AddListener(delegate() {
  64. BluetoothShoot.ins.DoConnect();
  65. });
  66. }
  67. BluetoothStatusEnum bowStatus;
  68. BluetoothStatusEnum arrowStatus;
  69. void UpdateBtnForConnect() {
  70. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  71. bowStatus = BluetoothAim.ins.status;
  72. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  73. btnConnectBow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  74. btnConnectBow.GetComponentInChildren<Text>().color = color;
  75. }
  76. if (BluetoothShoot.ins && arrowStatus != BluetoothShoot.ins.status) {
  77. arrowStatus = BluetoothShoot.ins.status;
  78. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothShoot.ins.status);
  79. btnConnectArrow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  80. btnConnectArrow.GetComponentInChildren<Text>().color = color;
  81. }
  82. }
  83. public void BackHome() {
  84. AudioMgr.ins.PlayBtn();
  85. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  86. }
  87. public void Close() {
  88. AudioMgr.ins.PlayBtn();
  89. Destroy(this.gameObject);
  90. if (onComplete != null) {
  91. onComplete();
  92. }
  93. }
  94. }