UIManager.cs 6.3 KB

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