AutoResetView.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. /*
  8. 按下自动校准键出现倒计时3秒,同时伴有文字提示用户“三秒后即将开始校准,
  9. 请扶稳弓箭。”倒计时3秒后出现一个进度条也是三秒,用户在3秒内自己尽量扶稳弓即可,
  10. 进度条完成后,取一个平均值作为校准值。
  11. */
  12. public class AutoResetView : MonoBehaviour
  13. {
  14. public static AutoResetView ins;
  15. public static Action onInstantiate;
  16. public static void DoIdentity() {
  17. //if (InfraredDemo.running) return;
  18. if (SceneManager.GetActiveScene().name.StartsWith("GameDouble"))
  19. {
  20. if (GameObject.Find("AutoResetViewNewLeft")) return;
  21. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  22. resetView.name = "AutoResetViewNewLeft";
  23. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  24. autoResetViewNewScript.setPosLeft();
  25. autoResetViewNewScript.action_OnDestroy += () =>
  26. {
  27. //使用旧模式重置1p
  28. AimHandler.ins?.DoIdentity();
  29. };
  30. }
  31. else if (SceneManager.GetActiveScene().name.StartsWith("Game"))
  32. {
  33. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  34. }
  35. else if (SceneManager.GetActiveScene().name.StartsWith("DuckHunter"))
  36. {
  37. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  38. }
  39. else if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  40. {
  41. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  42. }
  43. else if (SceneManager.GetActiveScene().name.StartsWith("FruitMaster"))
  44. {
  45. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  46. }
  47. else if (SceneManager.GetActiveScene().name.StartsWith("Hyperspace")) {
  48. //Hyperspace01 Hyperspace02 Hyperspace03
  49. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  50. }
  51. else
  52. {
  53. AimHandler.ins.DoIdentity();
  54. }
  55. }
  56. /// <summary>
  57. /// 引导页面弹出校准
  58. /// </summary>
  59. public static void onInfraredGuiderAutoResetView() {
  60. AutoResetView autoResetView = Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView")).GetComponent<AutoResetView>();
  61. autoResetView.bInfraredGuider = true;
  62. autoResetView.infraredGuiderTime = 10;
  63. }
  64. /// <summary>
  65. /// 是否是引导
  66. /// </summary>
  67. public bool bInfraredGuider = false;
  68. public float infraredGuiderTime = 10;
  69. void Awake() {
  70. if (ins) {
  71. Destroy(gameObject);
  72. return;
  73. }
  74. ins = this;
  75. }
  76. void Start() {
  77. //进入引导游戏场景时候,设置时间
  78. if (bInfraredGuider)
  79. {
  80. prepareTime = infraredGuiderTime;
  81. }
  82. else {
  83. prepareTime = UserSettings.ins.calibrationTime;
  84. }
  85. Debug.Log("弓箭重置时间:" + prepareTime);
  86. if (SceneManager.GetActiveScene().name == "Game") {
  87. (transform.Find("IconHumanShoot") as RectTransform).anchoredPosition = new Vector2(-193, -85);
  88. }
  89. GetGuideTip().textFormatArgs = new object[]{showedPrepareTime = Mathf.CeilToInt(prepareTime)};
  90. GetGuideTip().ApplyToText();
  91. ChallengeTargetForResetView.Show();
  92. onInstantiate?.Invoke();
  93. }
  94. public Action action_OnDestroy;
  95. void OnDestroy() {
  96. if (ins == this) ins = null;
  97. action_OnDestroy?.Invoke();
  98. }
  99. float prepareTime = 3;
  100. int showedPrepareTime;
  101. bool bEnd = false;
  102. void Update() {
  103. prepareTime -= Time.deltaTime;
  104. if (prepareTime <= 0) {
  105. //防止多次调用
  106. if (bEnd) return;
  107. bEnd = true;
  108. try {
  109. if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  110. {
  111. WildAttack.GameMananger.GetInstance().ResetAim();
  112. }
  113. else
  114. {
  115. if (AimHandler.ins.bRuning9Axis())
  116. {
  117. AimHandler.ins.DoIdentity();
  118. }
  119. else
  120. {
  121. //红外设备校准偏离点
  122. InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  123. }
  124. }
  125. }
  126. catch (Exception) {}
  127. //先隐藏子物体
  128. foreach (Transform child in transform)
  129. {
  130. child.gameObject.SetActive(false);
  131. }
  132. Destroy(gameObject,2.0f);
  133. } else {
  134. int curTime = Mathf.CeilToInt(prepareTime);
  135. if (showedPrepareTime != curTime) {
  136. showedPrepareTime = curTime;
  137. TextAutoLanguage2 gt = GetGuideTip();
  138. gt.textFormatArgs[0] = Mathf.CeilToInt(prepareTime);
  139. gt.ApplyToText();
  140. }
  141. }
  142. }
  143. TextAutoLanguage2 _guideTip;
  144. TextAutoLanguage2 GetGuideTip() {
  145. if (_guideTip == null) {
  146. if (AimHandler.ins.bRuning9Axis())
  147. {
  148. Transform text = transform.Find("FrameTip/Text");
  149. text.gameObject.SetActive(true);
  150. _guideTip = text.GetComponentInChildren<TextAutoLanguage2>();
  151. }
  152. else {
  153. Transform textInfrared = transform.Find("FrameTip/Text-Infrared");
  154. textInfrared.gameObject.SetActive(true);
  155. _guideTip = textInfrared.GetComponentInChildren<TextAutoLanguage2>();
  156. }
  157. }
  158. return _guideTip;
  159. }
  160. }