AutoResetView.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }else if (SceneManager.GetActiveScene().name.StartsWith("Game")) {
  31. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  32. }
  33. else if (SceneManager.GetActiveScene().name.StartsWith("DuckHunter")) {
  34. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  35. }
  36. else if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  37. {
  38. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  39. }
  40. else if (SceneManager.GetActiveScene().name.StartsWith("FruitMaster"))
  41. {
  42. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  43. }
  44. else {
  45. AimHandler.ins.DoIdentity();
  46. }
  47. }
  48. void Awake() {
  49. if (ins) {
  50. Destroy(gameObject);
  51. return;
  52. }
  53. ins = this;
  54. }
  55. void Start() {
  56. prepareTime = UserSettings.ins.calibrationTime;
  57. Debug.Log("弓箭重置时间:" + prepareTime);
  58. if (SceneManager.GetActiveScene().name == "Game") {
  59. (transform.Find("IconHumanShoot") as RectTransform).anchoredPosition = new Vector2(-193, -85);
  60. }
  61. GetGuideTip().textFormatArgs = new object[]{showedPrepareTime = Mathf.CeilToInt(prepareTime)};
  62. GetGuideTip().ApplyToText();
  63. ChallengeTargetForResetView.Show();
  64. onInstantiate?.Invoke();
  65. }
  66. public Action action_OnDestroy;
  67. void OnDestroy() {
  68. if (ins == this) ins = null;
  69. action_OnDestroy?.Invoke();
  70. }
  71. float prepareTime = 3;
  72. int showedPrepareTime;
  73. bool bEnd = false;
  74. void Update() {
  75. prepareTime -= Time.deltaTime;
  76. if (prepareTime <= 0) {
  77. //防止多次调用
  78. if (bEnd) return;
  79. bEnd = true;
  80. try {
  81. if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  82. {
  83. WildAttack.GameMananger.GetInstance().ResetAim();
  84. }
  85. else
  86. {
  87. if (AimHandler.ins.bRuning9Axis())
  88. {
  89. AimHandler.ins.DoIdentity();
  90. }
  91. else
  92. {
  93. //红外设备校准偏离点
  94. InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  95. }
  96. }
  97. }
  98. catch (Exception) {}
  99. //先隐藏子物体
  100. foreach (Transform child in transform)
  101. {
  102. child.gameObject.SetActive(false);
  103. }
  104. Destroy(gameObject,2.0f);
  105. } else {
  106. int curTime = Mathf.CeilToInt(prepareTime);
  107. if (showedPrepareTime != curTime) {
  108. showedPrepareTime = curTime;
  109. TextAutoLanguage2 gt = GetGuideTip();
  110. gt.textFormatArgs[0] = Mathf.CeilToInt(prepareTime);
  111. gt.ApplyToText();
  112. }
  113. }
  114. }
  115. TextAutoLanguage2 _guideTip;
  116. TextAutoLanguage2 GetGuideTip() {
  117. if (_guideTip == null) {
  118. if (AimHandler.ins.bRuning9Axis())
  119. {
  120. Transform text = transform.Find("FrameTip/Text");
  121. text.gameObject.SetActive(true);
  122. _guideTip = text.GetComponentInChildren<TextAutoLanguage2>();
  123. }
  124. else {
  125. Transform textInfrared = transform.Find("FrameTip/Text-Infrared");
  126. textInfrared.gameObject.SetActive(true);
  127. _guideTip = textInfrared.GetComponentInChildren<TextAutoLanguage2>();
  128. }
  129. }
  130. return _guideTip;
  131. }
  132. }