DeviceReconnectView.cs 3.6 KB

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