AutoResetView.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. try {
  49. AimHandler.ins.DoIdentity();
  50. Destroy(gameObject);
  51. }
  52. catch (System.Exception) {}
  53. Destroy(gameObject);
  54. } else {
  55. prepareTipText.text = $"{Mathf.CeilToInt(prepareTime)}秒后即将开始校准,请扶稳弓箭!";
  56. }
  57. }
  58. // if (step == 0) {
  59. // prepareTime -= Time.deltaTime;
  60. // if (prepareTime <= 0) {
  61. // step = 1;
  62. // prepareTipText.gameObject.SetActive(false);
  63. // rootBox.gameObject.SetActive(true);
  64. // tipText.text = "正在自动校准,请扶稳弓箭!";
  65. // SetProgress(0);
  66. // }
  67. // prepareTipText.text = $"{Mathf.CeilToInt(prepareTime)}秒后即将开始校准,请扶稳弓箭!";
  68. // } else if (step == 1) {
  69. // doTime += Time.deltaTime;
  70. // if (doTime > doTimeMax) {
  71. // doTime = doTimeMax;
  72. // step = 2;
  73. // }
  74. // SetProgress(doTime / doTimeMax);
  75. // } else if (step == 2) {
  76. // step = 3;
  77. // try {
  78. // AimHandler.ins.DoIdentity();
  79. // tipText.text = "校准完成";
  80. // }
  81. // catch (System.Exception) {
  82. // tipText.text = "校准失败";
  83. // }
  84. // } else if (step == 3) {
  85. // showTipTime -= Time.deltaTime;
  86. // if (showTipTime <= 0) {
  87. // Destroy(gameObject);
  88. // }
  89. // }
  90. }
  91. //最高1
  92. void SetProgress(float percent) {
  93. Vector3 sv = progressBar.localScale;
  94. sv.x = percent;
  95. progressBar.localScale = sv;
  96. progressText.text = Mathf.FloorToInt(percent * 100) + "%";
  97. }
  98. }
  99. /*
  100. * 自动视角归位
  101. * 每次开启app第一次进入游戏,在一个范围静止3秒钟直接调用归位函数
  102. */
  103. // public class AutoResetView : MonoBehaviour
  104. // {
  105. // [SerializeField] GameObject rootBox;
  106. // [SerializeField] Text tipText;
  107. // [SerializeField] Transform progressBar;
  108. // [SerializeField] Text progressText;
  109. // public static AutoResetView ins;
  110. // //是否已经对焦过(全局记录)
  111. // static bool doneReset = false;
  112. // bool finished = false;
  113. // public static void Create() {
  114. // GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  115. // }
  116. // void Awake() {
  117. // ins = this;
  118. // if (doneReset) Destroy(gameObject);
  119. // }
  120. // void Start() {
  121. // tipText.text = "正在自动对焦,请瞄准正前方并静止直至对焦完成!";
  122. // SetProgress(0);
  123. // }
  124. // void OnDestroy() {
  125. // if (ins == this) ins = null;
  126. // }
  127. // void Update() {
  128. // //如果游戏处于停止状态,则隐藏校准界面
  129. // if (GameMgr.ins.gamePause) {
  130. // if (rootBox.activeSelf) rootBox.SetActive(false);
  131. // } else {
  132. // if (!rootBox.activeSelf) rootBox.SetActive(true);
  133. // }
  134. // }
  135. // Quaternion cachedRotation;
  136. // int cachedRotationCount = 0;
  137. // float totalTime = 0;
  138. // const float needCheckTime = 3f;
  139. // public void CheckRotation(Quaternion quat) {
  140. // if (GameMgr.ins.gamePause) return;
  141. // if (doneReset) return;
  142. // if (finished) return;
  143. // if (cachedRotationCount == 0) {
  144. // cachedRotation = quat;
  145. // cachedRotationCount++;
  146. // totalTime = 0;
  147. // return;
  148. // }
  149. // float deltaAngle = Quaternion.Angle(quat, cachedRotation) / Time.deltaTime;
  150. // if (deltaAngle > 300) {
  151. // cachedRotationCount = 0; //重置
  152. // return;
  153. // }
  154. // totalTime += Time.deltaTime;
  155. // SetProgress(Mathf.Clamp(totalTime / needCheckTime, 0, 0.99f));
  156. // if (totalTime >= needCheckTime) {
  157. // try
  158. // {
  159. // AimHandler.ins.DoIdentity();
  160. // if (BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess) {
  161. // throw new Exception("还没有连接模块");
  162. // }
  163. // doneReset = true;
  164. // tipText.text = "对焦完成";
  165. // SetProgress(1);
  166. // }
  167. // catch (System.Exception)
  168. // {
  169. // tipText.text = "自动对焦失败,请检查是否已连接模块!";
  170. // }
  171. // finished = true;
  172. // DoTweenUtil.CallDelay(1f, () => {
  173. // Destroy(gameObject);
  174. // });
  175. // }
  176. // }
  177. // //最高1
  178. // void SetProgress(float percent) {
  179. // Vector3 sv = progressBar.localScale;
  180. // sv.x = percent;
  181. // progressBar.localScale = sv;
  182. // progressText.text = Mathf.FloorToInt(percent * 100) + "%";
  183. // }
  184. // }