AutoResetView.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 void DoIdentity() {
  16. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) {
  17. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  18. } else if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("DuckHunter")) {
  19. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  20. } else {
  21. AimHandler.ins.DoIdentity();
  22. }
  23. }
  24. void Awake() {
  25. if (ins) {
  26. Destroy(gameObject);
  27. return;
  28. }
  29. ins = this;
  30. }
  31. void Start() {
  32. if (SceneManager.GetActiveScene().name == "Game") {
  33. (transform.Find("IconHumanShoot") as RectTransform).anchoredPosition = new Vector2(-193, -85);
  34. }
  35. GetGuideTip().textFormatArgs = new object[]{showedPrepareTime = Mathf.CeilToInt(prepareTime)};
  36. GetGuideTip().ApplyToText();
  37. ChallengeTargetForResetView.Show();
  38. }
  39. public Action action_OnDestroy;
  40. void OnDestroy() {
  41. if (ins == this) ins = null;
  42. action_OnDestroy?.Invoke();
  43. }
  44. float prepareTime = 3;
  45. int showedPrepareTime;
  46. void Update() {
  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. int curTime = Mathf.CeilToInt(prepareTime);
  57. if (showedPrepareTime != curTime) {
  58. showedPrepareTime = curTime;
  59. TextAutoLanguage2 gt = GetGuideTip();
  60. gt.textFormatArgs[0] = Mathf.CeilToInt(prepareTime);
  61. gt.ApplyToText();
  62. }
  63. }
  64. }
  65. TextAutoLanguage2 _guideTip;
  66. TextAutoLanguage2 GetGuideTip() {
  67. if (_guideTip == null) _guideTip = transform.Find("FrameTip").GetComponentInChildren<TextAutoLanguage2>();
  68. return _guideTip;
  69. }
  70. }