GameUI.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using DG.Tweening;
  6. namespace DuckHunter
  7. {
  8. public class GameUI : MonoBehaviour
  9. {
  10. [SerializeField] Text textScore;
  11. [SerializeField] Text textScoreBest;
  12. [SerializeField] Text textHitScore;
  13. [SerializeField] Button btnReset;
  14. [SerializeField] Button btnAim;
  15. [SerializeField] Button btnBack;
  16. [SerializeField] RectTransform duckForGameStart;
  17. [SerializeField] GameObject textGameOver;
  18. [SerializeField] GameObject textGameCompleted;
  19. [SerializeField] GameObject bottomBox;
  20. [SerializeField] Slider levelSlider;
  21. public static GameUI Instance;
  22. void Awake()
  23. {
  24. Instance = this;
  25. btnReset.onClick.AddListener(() =>
  26. {
  27. AudioManager.Instance.PlayBtn();
  28. UnityEngine.SceneManagement.SceneManager.LoadScene("DuckHunter");
  29. });
  30. if (BluetoothAim.ins && BluetoothAim.ins.isMainConnectToHOUYIPRO())
  31. {
  32. btnAim.gameObject.SetActive(false);
  33. }
  34. else {
  35. btnAim.onClick.AddListener(() =>
  36. {
  37. if (btnAim.GetComponent<LongPressMonitor>().isLongPress) return;
  38. AudioManager.Instance.PlayBtn();
  39. SmartBowController.Instance.ResetAim();
  40. });
  41. btnAim.gameObject.AddComponent<LongPressMonitor>().onLongPress += () =>
  42. {
  43. AudioManager.Instance.PlayBtn();
  44. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  45. };
  46. }
  47. btnBack.onClick.AddListener(() => {
  48. AudioManager.Instance.PlayBtn();
  49. // UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  50. //野鸭退出时候操作一次分数
  51. Debug.Log("野鸭btnBack上传的积分:" );
  52. GameManager.Instance.onUploadScore();
  53. //结束游戏页面
  54. GameManager.Instance.userGameAnalyse1.showResultView(() => {
  55. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  56. });
  57. });
  58. InitLevelSlider();
  59. levelSlider.onValueChanged.AddListener(OnLevelSliderUpdate);
  60. }
  61. void OnDestroy()
  62. {
  63. if (Instance == this) Instance = null;
  64. DOTween.KillAll(false);
  65. }
  66. // Update is called once per frame
  67. void Update()
  68. {
  69. GameManager gm = GameManager.Instance;
  70. if (!gm) return;
  71. // int t = (int)(Time.time - startTime);
  72. // int m = t / 60;
  73. // int s = t % 60;
  74. // string mStr = m < 10 ? "0" + m : m.ToString();
  75. // string sStr = s < 10 ? "0" + s : s.ToString();
  76. // textTime.text = $"{mStr}:{sStr}";
  77. }
  78. void InitLevelSlider()
  79. {
  80. RenderLevelSliderText();
  81. levelSlider.SetValueWithoutNotify(GameManager.Instance.level);
  82. }
  83. void OnLevelSliderUpdate(float v)
  84. {
  85. GameManager.DefaultLevel = GameManager.Instance.level = (int)v;
  86. RenderLevelSliderText();
  87. }
  88. void RenderLevelSliderText()
  89. {
  90. var tal2 = levelSlider.transform.Find("Text").GetComponent<TextAutoLanguage2>();
  91. tal2.textFormatArgs = new object[] { GameManager.Instance.level };
  92. tal2.ApplyToText();
  93. }
  94. int _renderHitScore = 0;
  95. Tween _sequenceRenderHitScore;
  96. public void RenderHitScore(int score, int bestScore)
  97. {
  98. if (!GameManager.Instance) return;
  99. if (_sequenceRenderHitScore != null)
  100. {
  101. _sequenceRenderHitScore.Kill();
  102. _sequenceRenderHitScore = null;
  103. }
  104. _sequenceRenderHitScore = DOTween.To(() => _renderHitScore, v =>
  105. {
  106. _renderHitScore = v;
  107. textScore.text = _renderHitScore.ToString();
  108. }, score, 0.5f);
  109. if (bestScore > 0)
  110. {
  111. textScoreBest.gameObject.SetActive(true);
  112. var tal2 = textScoreBest.GetComponent<TextAutoLanguage2>();
  113. tal2.textFormatArgs = new object[] { bestScore };
  114. tal2.ApplyToText();
  115. }
  116. }
  117. public bool CheckHitDuckForGameStart(Vector2 screenPosition)
  118. {
  119. return RectTransformUtility.RectangleContainsScreenPoint(duckForGameStart, screenPosition);
  120. }
  121. public void HandleGameStart(System.Action callback, bool startImmediate)
  122. {
  123. if (startImmediate) TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.GAME_START_HIDE);
  124. else TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.GAME_START_FADEOUT);
  125. TweenCallback cb = () =>
  126. {
  127. bottomBox.SetActive(true);
  128. duckForGameStart.gameObject.SetActive(false);
  129. levelSlider.gameObject.SetActive(false);
  130. callback?.Invoke();
  131. };
  132. if (startImmediate) cb.Invoke();
  133. else
  134. {
  135. Sequence sequence = DOTween.Sequence();
  136. sequence.Append(duckForGameStart.transform.DOMoveX(Screen.width * 1.125f, 1f));
  137. sequence.AppendCallback(cb);
  138. }
  139. }
  140. List<Sequence> arrowsFadeSeqList = new List<Sequence>();
  141. [SerializeField] RectTransform arrowsRTF;
  142. public void RenderArrowCount(int count)
  143. {
  144. if (count == 0 && arrowsFadeSeqList.Count > 0) return;
  145. if (arrowsFadeSeqList.Count > 0)
  146. {
  147. for (int i = arrowsFadeSeqList.Count - 1; i >= 0; i--)
  148. {
  149. var seq = arrowsFadeSeqList[i];
  150. arrowsFadeSeqList.RemoveAt(i);
  151. seq.Kill();
  152. }
  153. }
  154. for (int i = 1; i < arrowsRTF.childCount; i++)
  155. {
  156. RectTransform iconRTF = arrowsRTF.GetChild(i) as RectTransform;
  157. Image iconIMG = iconRTF.GetComponent<Image>();
  158. iconIMG.color = Color.white;
  159. iconRTF.gameObject.SetActive(i <= count);
  160. if (count == 0)
  161. {
  162. if (i <= 3) iconRTF.gameObject.SetActive(true);
  163. var seq = DOTween.Sequence();
  164. arrowsFadeSeqList.Add(seq);
  165. seq.Append(iconIMG.DOFade(0.5f, 0.2f));
  166. seq.Append(iconIMG.DOFade(1f, 0f));
  167. seq.SetLoops(-1);
  168. }
  169. }
  170. }
  171. [SerializeField] RectTransform duckCountRTF;
  172. int _renderHitDuckCount = 0;
  173. List<Sequence> _duckIconListSequences = new List<Sequence>();
  174. /// <summary>
  175. /// 渲染鸭子的状态图标列表
  176. /// </summary>
  177. public void RenderHitDuckCount(int duckType)
  178. {
  179. if (duckType > 0)
  180. {
  181. _renderHitDuckCount++;
  182. RectTransform iconRTF = duckCountRTF.GetChild(_renderHitDuckCount) as RectTransform;
  183. Image iconIMG = iconRTF.GetComponent<Image>();
  184. iconIMG.sprite = Resources.Load<Sprite>("DuckHunter/Textures/Icons/Duck" + duckType);
  185. }
  186. else if (duckType == 0)
  187. {
  188. _renderHitDuckCount++;
  189. RectTransform iconRTF = duckCountRTF.GetChild(_renderHitDuckCount) as RectTransform;
  190. Image iconIMG = iconRTF.GetComponent<Image>();
  191. var seq = DOTween.Sequence();
  192. _duckIconListSequences.ForEach(e => e.Restart());
  193. _duckIconListSequences.Add(seq);
  194. seq.AppendCallback(() =>
  195. {
  196. Color c = iconIMG.color;
  197. c.a = 1;
  198. iconIMG.color = c;
  199. });
  200. seq.Append(iconIMG.DOFade(0.2f, 0.2f));
  201. seq.Append(iconIMG.DOFade(1f, 0f));
  202. seq.SetLoops(-1);
  203. }
  204. else
  205. {
  206. _duckIconListSequences.ForEach(e => e.Kill());
  207. for (int i = 1; i < duckCountRTF.childCount; i++)
  208. {
  209. RectTransform iconRTF = duckCountRTF.GetChild(i) as RectTransform;
  210. Image iconIMG = iconRTF.GetComponent<Image>();
  211. var seq = DOTween.Sequence();
  212. Color c = iconIMG.color;
  213. c.a = 1;
  214. iconIMG.color = c;
  215. seq.Append(iconIMG.DOFade(0.5f, 0.2f));
  216. seq.Append(iconIMG.DOFade(1f, 0f));
  217. seq.SetLoops(-1);
  218. }
  219. }
  220. }
  221. Sequence _sequenceShowTextHitScore;
  222. public void ShowTextHitScore(int score, Vector2 screenPosition)
  223. {
  224. if (_sequenceShowTextHitScore != null)
  225. {
  226. _sequenceShowTextHitScore.Kill();
  227. _sequenceShowTextHitScore = null;
  228. }
  229. textHitScore.gameObject.SetActive(true);
  230. textHitScore.text = score.ToString();
  231. textHitScore.transform.position = screenPosition;
  232. _sequenceShowTextHitScore = DOTween.Sequence();
  233. _sequenceShowTextHitScore.Append(textHitScore.DOFade(0.8f, 0));
  234. float lpy = textHitScore.transform.localPosition.y;
  235. _sequenceShowTextHitScore.Append(textHitScore.transform.DOLocalMoveY(lpy + 100, 0.5f));
  236. _sequenceShowTextHitScore.AppendInterval(1.5f);
  237. _sequenceShowTextHitScore.Append(textHitScore.DOFade(0, 0.5f));
  238. _sequenceShowTextHitScore.AppendCallback(() => textHitScore.gameObject.SetActive(false));
  239. }
  240. [SerializeField] Text textLevel;
  241. public void RenderLevel(int level)
  242. {
  243. var tal2 = textLevel.GetComponent<TextAutoLanguage2>();
  244. tal2.textFormatArgs = new object[] { level };
  245. tal2.ApplyToText();
  246. }
  247. [SerializeField] RectTransform arrowBox;
  248. public void AddArrowOnScreen(Vector2 screenPosition)
  249. {
  250. //arrowBox.GetChild(0)
  251. var o = Instantiate(arrowBox.GetChild((int)GlobalData.MyDeviceMode).gameObject, arrowBox);
  252. var tf = o.transform;
  253. tf.position = screenPosition;
  254. tf.gameObject.SetActive(true);
  255. Image img = tf.GetComponent<Image>();
  256. Sequence seq = DOTween.Sequence();
  257. seq.AppendInterval(2.5f);
  258. seq.Append(img.DOFade(0, 0.5f));
  259. seq.AppendCallback(() => Destroy(o));
  260. }
  261. }
  262. }