AutoResetView.cs 6.5 KB

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