UIManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. namespace HyperspaceGame
  7. {
  8. public class UIManager : MonoBehaviour
  9. {
  10. public static UIManager _ins;
  11. /// <summary>
  12. /// 记录一个总分
  13. /// </summary>
  14. public static int mHyperspaceScore { get; set; } = 0;
  15. [SerializeField] Button _btnQuit;
  16. [SerializeField] Button _btnReload;
  17. [SerializeField] Button _btnCrosshair;
  18. [SerializeField] Button _btnCalibrationOffset;
  19. [SerializeField] Material outlight;
  20. //准心部分
  21. private bool open = false;
  22. private bool visiable = false;
  23. private bool onlyShow = true;
  24. private Image image = null;
  25. private void Awake()
  26. {
  27. if (_ins != null && _ins != this)
  28. {
  29. //Destroy(this.gameObject); // 如果已经存在实例,则销毁当前对象
  30. return;
  31. }
  32. _ins = this;
  33. //DontDestroyOnLoad(this.gameObject); // 保证对象在场景切换时不被销毁
  34. //生成时候初始化一下状态
  35. if (ShootCheck.ins) ShootCheck.ins.bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading;
  36. }
  37. private void OnDestroy()
  38. {
  39. if (_ins == this)
  40. {
  41. _ins = null; // 在销毁时释放单例
  42. }
  43. GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
  44. }
  45. // Start is called before the first frame update
  46. void Start()
  47. {
  48. //关闭原来准心
  49. SimulateMouseController.ins?.RemoveOpenLocker("NotGame");
  50. image = GeneratingTarget.gm.shootingEvent.GetComponent<Image>();
  51. //获取设置值和一个存储值
  52. bool onInitOpen = InfraredDemo._ins ? InfraredDemo._ins.bInitCrosshairShow() : true;
  53. Image crossHairImage = _btnCrosshair.GetComponentInChildren<Image>();
  54. crossHairImage.material = onInitOpen ? outlight : null;
  55. _btnCrosshair.onClick.AddListener(delegate () {
  56. AudioMgr.ins.PlayBtn();
  57. bool onlyShow = !GetOnlyShow();
  58. SetOnlyShow(onlyShow);
  59. //记录准心值
  60. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  61. if (onlyShow)
  62. {
  63. crossHairImage.material = outlight;
  64. }
  65. else
  66. {
  67. crossHairImage.material = null;
  68. }
  69. });
  70. //校准
  71. _btnCalibrationOffset.onClick.AddListener(delegate () {
  72. AudioMgr.ins.PlayBtn();
  73. AutoResetView.DoIdentity();
  74. });
  75. _btnQuit.onClick.AddListener(OnShutDown);
  76. _btnReload.onClick.AddListener(Reload);
  77. SetOpen(UserSettings.ins.openCrossHair);
  78. visiable = open;
  79. OnValueChanged(visiable);
  80. UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
  81. GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;
  82. //CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
  83. //outBoundChecker.crossHair = transform as RectTransform;
  84. //读取准心值
  85. if (AimHandler.ins && !AimHandler.ins.bRuning9Axis() && InfraredDemo._ins) SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1);
  86. }
  87. void UpdateHideShow(bool mouseAwaked)
  88. {
  89. //transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
  90. SetOnlyShow(!mouseAwaked);
  91. }
  92. public void ShowQuit()
  93. {
  94. _btnQuit.gameObject.SetActive(true);
  95. }
  96. void OnShutDown()
  97. {
  98. //SceneManager.LoadScene("Home",LoadSceneMode.Single);
  99. GeneratingTarget.gm.onUploadScore();
  100. //结束游戏页面
  101. GeneratingTarget.gm.userGameAnalyse1.showResultView(() =>
  102. {
  103. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  104. });
  105. }
  106. public void Reload()
  107. {
  108. GeneratingTarget.gm.OnSeparation();
  109. GeneratingTarget.gm.OnLoading();
  110. }
  111. public bool GetArrowBtnState()
  112. {
  113. if (open)
  114. return true;
  115. return false;
  116. }
  117. // Update is called once per frame
  118. void Update()
  119. {
  120. if (AutoResetView.ins != null)
  121. {
  122. //进入校准时候,一定显示准心
  123. SetVisiable(true);
  124. }
  125. else
  126. {
  127. if (open) SetVisiable(onlyShow);
  128. }
  129. }
  130. void SetVisiable(bool value)
  131. {
  132. if (this.visiable == value) return;
  133. this.visiable = value;
  134. OnValueChanged(this.visiable);
  135. }
  136. void OnValueChanged(bool isOn)
  137. {
  138. var color = image.color;
  139. if (isOn)
  140. {
  141. color.a = 1;
  142. image.color = color;
  143. //_togImge.sprite = _togSprites[0];
  144. }
  145. else
  146. {
  147. color.a = 0;
  148. image.color = color;
  149. //_togImge.sprite = _togSprites[1];
  150. }
  151. }
  152. public void SetOpen(bool open)
  153. {
  154. this.open = open;
  155. if (!this.open)
  156. {
  157. SetVisiable(false);
  158. }
  159. }
  160. public bool GetOpen()
  161. {
  162. return this.open;
  163. }
  164. public void SetOnlyShow(bool onlyShow)
  165. {
  166. this.onlyShow = onlyShow;
  167. }
  168. public bool GetOnlyShow()
  169. {
  170. return this.onlyShow;
  171. }
  172. }
  173. }