DeviceReconnectView.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. GameMgr.ins.addLockerForGamePause(this);
  35. }
  36. void OnDestroy()
  37. {
  38. GameMgr.ins.removeLockerForGamePause(this);
  39. }
  40. void Update()
  41. {
  42. UpdateBtnForConnect();
  43. btnClose.interactable = BluetoothStatus.IsAllConnected();
  44. }
  45. void InitBtnForConnect()
  46. {
  47. btnConnectBow.GetComponent<Button>().onClick.AddListener(delegate() {
  48. BluetoothAim.ins.DoConnect();
  49. });
  50. btnConnectArrow.GetComponent<Button>().onClick.AddListener(delegate() {
  51. BluetoothShoot.ins.DoConnect();
  52. });
  53. }
  54. BluetoothStatusEnum bowStatus;
  55. BluetoothStatusEnum arrowStatus;
  56. void UpdateBtnForConnect() {
  57. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  58. bowStatus = BluetoothAim.ins.status;
  59. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  60. btnConnectBow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  61. btnConnectBow.GetComponentInChildren<Text>().color = color;
  62. if (BluetoothAim.ins.status == BluetoothStatusEnum.Connect) {
  63. btnConnectBow.GetComponent<Button>().enabled = true;
  64. } else {
  65. btnConnectBow.GetComponent<Button>().enabled = false;
  66. }
  67. }
  68. if (BluetoothShoot.ins && arrowStatus != BluetoothShoot.ins.status) {
  69. arrowStatus = BluetoothShoot.ins.status;
  70. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothShoot.ins.status);
  71. btnConnectArrow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  72. btnConnectArrow.GetComponentInChildren<Text>().color = color;
  73. if (BluetoothShoot.ins.status == BluetoothStatusEnum.Connect) {
  74. btnConnectArrow.GetComponent<Button>().enabled = true;
  75. } else {
  76. btnConnectArrow.GetComponent<Button>().enabled = false;
  77. }
  78. }
  79. }
  80. public void BackHome() {
  81. AudioMgr.ins.PlayBtn();
  82. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  83. }
  84. public void Close() {
  85. AudioMgr.ins.PlayBtn();
  86. Destroy(this.gameObject);
  87. if (onComplete != null) {
  88. onComplete();
  89. }
  90. }
  91. }