UIManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. GameResultView gameResultView;
  97. int num = 0;
  98. public void OnShutDown()
  99. {
  100. if (num == 0)
  101. {
  102. //SceneManager.LoadScene("Home",LoadSceneMode.Single);
  103. GeneratingTarget.gm.onUploadScore();
  104. //结束游戏页面
  105. gameResultView = GeneratingTarget.gm.userGameAnalyse1.showResultView(() =>
  106. {
  107. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  108. });
  109. num += 1;
  110. return;
  111. }
  112. if (num == 1)
  113. {
  114. gameResultView.OnClick_Back();
  115. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  116. Debug.Log("退出当前场景");
  117. }
  118. }
  119. public void Reload()
  120. {
  121. GeneratingTarget.gm.OnSeparation();
  122. GeneratingTarget.gm.OnLoading();
  123. }
  124. public bool GetArrowBtnState()
  125. {
  126. if (open)
  127. return true;
  128. return false;
  129. }
  130. // Update is called once per frame
  131. void Update()
  132. {
  133. if (AutoResetView.ins != null)
  134. {
  135. //进入校准时候,一定显示准心
  136. SetVisiable(true);
  137. }
  138. else
  139. {
  140. if (open) SetVisiable(onlyShow);
  141. }
  142. }
  143. void SetVisiable(bool value)
  144. {
  145. if (this.visiable == value) return;
  146. this.visiable = value;
  147. OnValueChanged(this.visiable);
  148. }
  149. void OnValueChanged(bool isOn)
  150. {
  151. var color = image.color;
  152. if (isOn)
  153. {
  154. color.a = 1;
  155. image.color = color;
  156. //_togImge.sprite = _togSprites[0];
  157. }
  158. else
  159. {
  160. color.a = 0;
  161. image.color = color;
  162. //_togImge.sprite = _togSprites[1];
  163. }
  164. }
  165. public void SetOpen(bool open)
  166. {
  167. this.open = open;
  168. if (!this.open)
  169. {
  170. SetVisiable(false);
  171. }
  172. }
  173. public bool GetOpen()
  174. {
  175. return this.open;
  176. }
  177. public void SetOnlyShow(bool onlyShow)
  178. {
  179. this.onlyShow = onlyShow;
  180. }
  181. public bool GetOnlyShow()
  182. {
  183. return this.onlyShow;
  184. }
  185. }
  186. }