AutoResetView.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /*
  6. 按下自动校准键出现倒计时3秒,同时伴有文字提示用户“三秒后即将开始校准,
  7. 请扶稳弓箭。”倒计时3秒后出现一个进度条也是三秒,用户在3秒内自己尽量扶稳弓即可,
  8. 进度条完成后,取一个平均值作为校准值。
  9. */
  10. public class AutoResetView : MonoBehaviour
  11. {
  12. [SerializeField] Text prepareTipText;
  13. [SerializeField] GameObject rootBox;
  14. [SerializeField] Text tipText;
  15. [SerializeField] Transform progressBar;
  16. [SerializeField] Text progressText;
  17. static AutoResetView ins;
  18. public static void DoIdentity() {
  19. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) {
  20. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  21. } else {
  22. AimHandler.ins.DoIdentity();
  23. }
  24. }
  25. void Awake() {
  26. if (ins) {
  27. Destroy(gameObject);
  28. return;
  29. }
  30. ins = this;
  31. }
  32. void Start() {
  33. rootBox.gameObject.SetActive(false);
  34. prepareTipText.gameObject.SetActive(true);
  35. prepareTipText.text = "";
  36. }
  37. void OnDestroy() {
  38. if (ins == this) ins = null;
  39. }
  40. float prepareTime = 3;
  41. float doTime = 0, doTimeMax = 3;
  42. float showTipTime = 1;
  43. int step = 0;
  44. void Update() {
  45. if (step == 0) {
  46. prepareTime -= Time.deltaTime;
  47. if (prepareTime <= 0) {
  48. step = 1;
  49. prepareTipText.gameObject.SetActive(false);
  50. rootBox.gameObject.SetActive(true);
  51. tipText.text = "正在自动校准,请扶稳弓箭!";
  52. SetProgress(0);
  53. }
  54. prepareTipText.text = $"{Mathf.CeilToInt(prepareTime)}秒后即将开始校准,请扶稳弓箭!";
  55. } else if (step == 1) {
  56. doTime += Time.deltaTime;
  57. if (doTime > doTimeMax) {
  58. doTime = doTimeMax;
  59. step = 2;
  60. }
  61. SetProgress(doTime / doTimeMax);
  62. } else if (step == 2) {
  63. step = 3;
  64. try {
  65. AimHandler.ins.DoIdentity();
  66. tipText.text = "校准完成";
  67. }
  68. catch (System.Exception) {
  69. tipText.text = "校准失败";
  70. }
  71. } else if (step == 3) {
  72. showTipTime -= Time.deltaTime;
  73. if (showTipTime <= 0) {
  74. Destroy(gameObject);
  75. }
  76. }
  77. }
  78. //最高1
  79. void SetProgress(float percent) {
  80. Vector3 sv = progressBar.localScale;
  81. sv.x = percent;
  82. progressBar.localScale = sv;
  83. progressText.text = Mathf.FloorToInt(percent * 100) + "%";
  84. }
  85. }
  86. /*
  87. * 自动视角归位
  88. * 每次开启app第一次进入游戏,在一个范围静止3秒钟直接调用归位函数
  89. */
  90. // public class AutoResetView : MonoBehaviour
  91. // {
  92. // [SerializeField] GameObject rootBox;
  93. // [SerializeField] Text tipText;
  94. // [SerializeField] Transform progressBar;
  95. // [SerializeField] Text progressText;
  96. // public static AutoResetView ins;
  97. // //是否已经对焦过(全局记录)
  98. // static bool doneReset = false;
  99. // bool finished = false;
  100. // public static void Create() {
  101. // GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  102. // }
  103. // void Awake() {
  104. // ins = this;
  105. // if (doneReset) Destroy(gameObject);
  106. // }
  107. // void Start() {
  108. // tipText.text = "正在自动对焦,请瞄准正前方并静止直至对焦完成!";
  109. // SetProgress(0);
  110. // }
  111. // void OnDestroy() {
  112. // if (ins == this) ins = null;
  113. // }
  114. // void Update() {
  115. // //如果游戏处于停止状态,则隐藏校准界面
  116. // if (GameMgr.ins.gamePause) {
  117. // if (rootBox.activeSelf) rootBox.SetActive(false);
  118. // } else {
  119. // if (!rootBox.activeSelf) rootBox.SetActive(true);
  120. // }
  121. // }
  122. // Quaternion cachedRotation;
  123. // int cachedRotationCount = 0;
  124. // float totalTime = 0;
  125. // const float needCheckTime = 3f;
  126. // public void CheckRotation(Quaternion quat) {
  127. // if (GameMgr.ins.gamePause) return;
  128. // if (doneReset) return;
  129. // if (finished) return;
  130. // if (cachedRotationCount == 0) {
  131. // cachedRotation = quat;
  132. // cachedRotationCount++;
  133. // totalTime = 0;
  134. // return;
  135. // }
  136. // float deltaAngle = Quaternion.Angle(quat, cachedRotation) / Time.deltaTime;
  137. // if (deltaAngle > 300) {
  138. // cachedRotationCount = 0; //重置
  139. // return;
  140. // }
  141. // totalTime += Time.deltaTime;
  142. // SetProgress(Mathf.Clamp(totalTime / needCheckTime, 0, 0.99f));
  143. // if (totalTime >= needCheckTime) {
  144. // try
  145. // {
  146. // AimHandler.ins.DoIdentity();
  147. // if (BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess) {
  148. // throw new Exception("还没有连接模块");
  149. // }
  150. // doneReset = true;
  151. // tipText.text = "对焦完成";
  152. // SetProgress(1);
  153. // }
  154. // catch (System.Exception)
  155. // {
  156. // tipText.text = "自动对焦失败,请检查是否已连接模块!";
  157. // }
  158. // finished = true;
  159. // DoTweenUtil.CallDelay(1f, () => {
  160. // Destroy(gameObject);
  161. // });
  162. // }
  163. // }
  164. // //最高1
  165. // void SetProgress(float percent) {
  166. // Vector3 sv = progressBar.localScale;
  167. // sv.x = percent;
  168. // progressBar.localScale = sv;
  169. // progressText.text = Mathf.FloorToInt(percent * 100) + "%";
  170. // }
  171. // }