AutoResetView.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  19. if (SceneManager.GetActiveScene().name.StartsWith("GameDouble"))
  20. {
  21. if (GameObject.Find("AutoResetViewNewLeft")) return;
  22. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  23. resetView.name = "AutoResetViewNewLeft";
  24. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  25. autoResetViewNewScript.setPosLeft();
  26. autoResetViewNewScript.action_OnDestroy += () =>
  27. {
  28. //使用旧模式重置1p
  29. //AimHandler.ins?.DoIdentity();
  30. };
  31. }
  32. else if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  33. {
  34. if (GameObject.Find("AutoResetViewNewLeft")) return;
  35. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  36. resetView.name = "AutoResetViewNewLeft";
  37. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  38. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  39. autoResetViewNewScript.setPosLeft();
  40. autoResetViewNewScript.action_OnDestroy += () =>
  41. {
  42. if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  43. {
  44. InfraredDemo._ins?.SetAdjustPointsOffset(PlayerType.FirstPlayer);
  45. }
  46. };
  47. }
  48. else if (SceneManager.GetActiveScene().name.StartsWith("Game"))
  49. {
  50. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  51. }
  52. else if (SceneManager.GetActiveScene().name.StartsWith("DuckHunter"))
  53. {
  54. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  55. }
  56. else if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  57. {
  58. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  59. }
  60. else if (SceneManager.GetActiveScene().name.StartsWith("FruitMaster"))
  61. {
  62. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  63. }
  64. else if (SceneManager.GetActiveScene().name.StartsWith("Hyperspace"))
  65. {
  66. //Hyperspace01 Hyperspace02 Hyperspace03
  67. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  68. }
  69. else
  70. {
  71. Debug.LogWarning("没有重置函数!");
  72. }
  73. }
  74. /// <summary>
  75. /// 引导页面弹出校准
  76. /// </summary>
  77. public static void onInfraredGuiderAutoResetView() {
  78. CreateResetViewPath("Prefabs/Views/AutoResetView");
  79. }
  80. /// <summary>
  81. /// 带背景的校准
  82. /// </summary>
  83. public static void onInfraredGuiderAutoResetViewOrBg()
  84. {
  85. CreateResetViewPath("Prefabs/Views/AutoResetViewOrBg");
  86. }
  87. static void CreateResetViewPath(string path) {
  88. AutoResetView autoResetView = Instantiate(Resources.Load<GameObject>(path)).GetComponent<AutoResetView>();
  89. autoResetView.bInfraredGuider = true;
  90. autoResetView.infraredGuiderTime = GlobalData.MyDeviceMode == DeviceMode.Gun ? CommonConfig.calibrationTimeGun : CommonConfig.calibrationTimeArchery;
  91. }
  92. /// <summary>
  93. /// 是否是引导
  94. /// </summary>
  95. public bool bInfraredGuider = false;
  96. public float infraredGuiderTime = 10;
  97. void Awake() {
  98. if (ins) {
  99. Destroy(gameObject);
  100. return;
  101. }
  102. ins = this;
  103. }
  104. void Start() {
  105. //进入引导游戏场景时候,设置时间
  106. if (bInfraredGuider)
  107. {
  108. prepareTime = infraredGuiderTime;
  109. }
  110. else {
  111. prepareTime = UserSettings.ins.calibrationTime;
  112. }
  113. Debug.Log("弓箭重置时间:" + prepareTime);
  114. if (SceneManager.GetActiveScene().name == "Game") {
  115. (transform.Find("IconHumanShoot") as RectTransform).anchoredPosition = new Vector2(-193, -85);
  116. }
  117. GetGuideTip().textFormatArgs = new object[]{showedPrepareTime = Mathf.CeilToInt(prepareTime)};
  118. GetGuideTip().ApplyToText();
  119. ChallengeTargetForResetView.Show();
  120. onInstantiate?.Invoke();
  121. }
  122. public Action action_OnDestroy;
  123. void OnDestroy() {
  124. if (ins == this) ins = null;
  125. action_OnDestroy?.Invoke();
  126. }
  127. float prepareTime = 3;
  128. int showedPrepareTime;
  129. bool bEnd = false;
  130. void Update() {
  131. // 使用 unscaledDeltaTime 代替 deltaTime
  132. prepareTime -= Time.unscaledDeltaTime;//Time.deltaTime;
  133. if (prepareTime <= 0) {
  134. //防止多次调用
  135. if (bEnd) return;
  136. bEnd = true;
  137. try {
  138. if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  139. {
  140. WildAttack.GameMananger.GetInstance().ResetAim();
  141. }
  142. else
  143. {
  144. //红外设备校准偏离点
  145. InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  146. }
  147. }
  148. catch (Exception) {}
  149. //先隐藏子物体
  150. foreach (Transform child in transform)
  151. {
  152. child.gameObject.SetActive(false);
  153. }
  154. Destroy(gameObject,2.0f);
  155. } else {
  156. int curTime = Mathf.CeilToInt(prepareTime);
  157. if (showedPrepareTime != curTime) {
  158. showedPrepareTime = curTime;
  159. TextAutoLanguage2 gt = GetGuideTip();
  160. gt.textFormatArgs[0] = Mathf.CeilToInt(prepareTime);
  161. gt.ApplyToText();
  162. }
  163. }
  164. }
  165. TextAutoLanguage2 _guideTip;
  166. TextAutoLanguage2 GetGuideTip() {
  167. if (_guideTip == null) {
  168. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  169. {
  170. Transform textInfrared = transform.Find("FrameTip/Text-Infrared-Gun");
  171. textInfrared.gameObject.SetActive(true);
  172. _guideTip = textInfrared.GetComponentInChildren<TextAutoLanguage2>();
  173. }
  174. else
  175. {
  176. Transform textInfrared = transform.Find("FrameTip/Text-Infrared");
  177. textInfrared.gameObject.SetActive(true);
  178. _guideTip = textInfrared.GetComponentInChildren<TextAutoLanguage2>();
  179. }
  180. }
  181. return _guideTip;
  182. }
  183. }