UIManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. //b端单人
  52. if (CommonConfig.StandaloneModeOrPlatformB)
  53. {
  54. _btnCrosshair.gameObject.SetActive(false);
  55. _btnCalibrationOffset.gameObject.SetActive(false);
  56. } else {
  57. //获取设置值和一个存储值
  58. bool onInitOpen = InfraredDemo._ins ? InfraredDemo._ins.bInitCrosshairShow() : true;
  59. Image crossHairImage = _btnCrosshair.GetComponentInChildren<Image>();
  60. crossHairImage.material = onInitOpen ? outlight : null;
  61. _btnCrosshair.onClick.AddListener(delegate () {
  62. AudioMgr.ins.PlayBtn();
  63. bool onlyShow = !GetOnlyShow();
  64. SetOnlyShow(onlyShow);
  65. //记录准心值
  66. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  67. if (onlyShow)
  68. {
  69. crossHairImage.material = outlight;
  70. }
  71. else
  72. {
  73. crossHairImage.material = null;
  74. }
  75. });
  76. //校准
  77. _btnCalibrationOffset.onClick.AddListener(delegate () {
  78. AudioMgr.ins.PlayBtn();
  79. AutoResetView.DoIdentity();
  80. });
  81. }
  82. _btnQuit.onClick.AddListener(OnShutDown);
  83. _btnReload.onClick.AddListener(Reload);
  84. SetOpen(UserSettings.ins.openCrossHair);
  85. visiable = open;
  86. OnValueChanged(visiable);
  87. UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
  88. GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;
  89. //CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
  90. //outBoundChecker.crossHair = transform as RectTransform;
  91. //读取准心值
  92. if (AimHandler.ins && !AimHandler.ins.bRuning9Axis() && InfraredDemo._ins) SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1);
  93. }
  94. void UpdateHideShow(bool mouseAwaked)
  95. {
  96. //transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
  97. SetOnlyShow(!mouseAwaked);
  98. }
  99. public void ShowQuit()
  100. {
  101. _btnQuit.gameObject.SetActive(true);
  102. }
  103. GameResultView gameResultView;
  104. int num = 0;
  105. public void OnShutDown()
  106. {
  107. if (num == 0)
  108. {
  109. //SceneManager.LoadScene("Home",LoadSceneMode.Single);
  110. GeneratingTarget.gm.onUploadScore();
  111. //结束游戏页面
  112. gameResultView = GeneratingTarget.gm.userGameAnalyse1.showResultView(() =>
  113. {
  114. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  115. });
  116. num += 1;
  117. return;
  118. }
  119. if (num == 1)
  120. {
  121. gameResultView.OnClick_Back();
  122. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  123. Debug.Log("退出当前场景");
  124. }
  125. }
  126. public void Reload()
  127. {
  128. GeneratingTarget.gm.OnSeparation();
  129. GeneratingTarget.gm.OnLoading();
  130. }
  131. public bool GetArrowBtnState()
  132. {
  133. if (open)
  134. return true;
  135. return false;
  136. }
  137. // Update is called once per frame
  138. void Update()
  139. {
  140. if (AutoResetView.ins != null)
  141. {
  142. //进入校准时候,一定显示准心
  143. SetVisiable(true);
  144. }
  145. else
  146. {
  147. if (open)
  148. SetVisiable(onlyShow);
  149. else
  150. SetVisiable(false);
  151. }
  152. }
  153. void SetVisiable(bool value)
  154. {
  155. if (this.visiable == value) return;
  156. this.visiable = value;
  157. OnValueChanged(this.visiable);
  158. }
  159. void OnValueChanged(bool isOn)
  160. {
  161. var color = image.color;
  162. if (isOn)
  163. {
  164. color.a = 1;
  165. image.color = color;
  166. //_togImge.sprite = _togSprites[0];
  167. }
  168. else
  169. {
  170. color.a = 0;
  171. image.color = color;
  172. //_togImge.sprite = _togSprites[1];
  173. }
  174. }
  175. public void SetOpen(bool open)
  176. {
  177. this.open = open;
  178. if (!this.open)
  179. {
  180. SetVisiable(false);
  181. }
  182. }
  183. public bool GetOpen()
  184. {
  185. return this.open;
  186. }
  187. public void SetOnlyShow(bool onlyShow)
  188. {
  189. this.onlyShow = onlyShow;
  190. }
  191. public bool GetOnlyShow()
  192. {
  193. return this.onlyShow;
  194. }
  195. }
  196. }