AutoResetView.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) {
  18. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  19. } else if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("DuckHunter")) {
  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. if (SceneManager.GetActiveScene().name == "Game") {
  34. (transform.Find("IconHumanShoot") as RectTransform).anchoredPosition = new Vector2(-193, -85);
  35. }
  36. GetGuideTip().textFormatArgs = new object[]{showedPrepareTime = Mathf.CeilToInt(prepareTime)};
  37. GetGuideTip().ApplyToText();
  38. ChallengeTargetForResetView.Show();
  39. onInstantiate?.Invoke();
  40. }
  41. public Action action_OnDestroy;
  42. void OnDestroy() {
  43. if (ins == this) ins = null;
  44. action_OnDestroy?.Invoke();
  45. }
  46. float prepareTime = 3;
  47. int showedPrepareTime;
  48. void Update() {
  49. prepareTime -= Time.deltaTime;
  50. if (prepareTime <= 0) {
  51. try {
  52. AimHandler.ins.DoIdentity();
  53. Destroy(gameObject);
  54. }
  55. catch (System.Exception) {}
  56. Destroy(gameObject);
  57. } else {
  58. int curTime = Mathf.CeilToInt(prepareTime);
  59. if (showedPrepareTime != curTime) {
  60. showedPrepareTime = curTime;
  61. TextAutoLanguage2 gt = GetGuideTip();
  62. gt.textFormatArgs[0] = Mathf.CeilToInt(prepareTime);
  63. gt.ApplyToText();
  64. }
  65. }
  66. }
  67. TextAutoLanguage2 _guideTip;
  68. TextAutoLanguage2 GetGuideTip() {
  69. if (_guideTip == null) _guideTip = transform.Find("FrameTip").GetComponentInChildren<TextAutoLanguage2>();
  70. return _guideTip;
  71. }
  72. }