LoadingObject.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using DG.Tweening;
  6. public class LoadingObject : MonoBehaviour
  7. {
  8. public Animator anim;
  9. public Text LoadingText;
  10. public GameObject AnimationParent;
  11. public GameObject Meat;
  12. public GameObject Agumon;
  13. Vector3 defaultAgumonPos = new Vector3(250, 0 ,0);
  14. public float speed = 700f;
  15. public IEnumerator StartLoading(string DefaultString)
  16. {
  17. this.transform.parent.gameObject.SetActive(true);
  18. this.gameObject.SetActive(true);
  19. anim.SetInteger("Close", 0);
  20. LoadingText.gameObject.SetActive(true);
  21. yield return new WaitWhile(() => !this.gameObject.activeSelf || !this.transform.parent.gameObject.activeSelf);
  22. if(ContinuousController.instance != null)
  23. {
  24. ContinuousController.instance.StartCoroutine(SetLoadingText(DefaultString));
  25. }
  26. else
  27. {
  28. StartCoroutine(SetLoadingText(DefaultString));
  29. }
  30. if (AnimationParent.activeSelf)
  31. {
  32. Agumon.transform.localPosition = defaultAgumonPos;
  33. moveAgumonCoroutine = StartCoroutine(moveAgumonIEnumerator());
  34. }
  35. }
  36. Coroutine moveAgumonCoroutine = null;
  37. IEnumerator SetLoadingText(string DefaultString)
  38. {
  39. float waitTime = 0.18f;
  40. int count = 0;
  41. while(true)
  42. {
  43. count++;
  44. if(count >= 4)
  45. {
  46. count = 0;
  47. }
  48. LoadingText.text = DefaultString;
  49. for(int i=0;i<count;i++)
  50. {
  51. LoadingText.text += ".";
  52. }
  53. yield return new WaitForSeconds(waitTime);
  54. }
  55. }
  56. IEnumerator moveAgumonIEnumerator()
  57. {
  58. while(true)
  59. {
  60. Agumon.transform.localPosition -= new Vector3(speed*Time.deltaTime, 0 ,0);
  61. if(Mathf.Abs(Agumon.transform.localPosition.x - Meat.transform.localPosition.x) < speed * Time.deltaTime * 2)
  62. {
  63. Agumon.transform.localPosition = Meat.transform.localPosition;
  64. yield break;
  65. }
  66. yield return null;
  67. }
  68. }
  69. public IEnumerator EndLoading()
  70. {
  71. if(moveAgumonCoroutine != null)
  72. {
  73. StopCoroutine(moveAgumonCoroutine);
  74. }
  75. if(AnimationParent.activeSelf)
  76. {
  77. bool end = false;
  78. Sequence sequence = DOTween.Sequence();
  79. sequence
  80. .Append(Agumon.transform.DOLocalMove(Meat.transform.localPosition, 0.1f))
  81. .AppendCallback(() => end = true);
  82. sequence.Play();
  83. yield return new WaitWhile(() => !end);
  84. end = false;
  85. }
  86. anim.SetInteger("Close", 1);
  87. LoadingText.gameObject.SetActive(false);
  88. yield return new WaitWhile(() => this.gameObject.activeSelf);
  89. if (AnimationParent.activeSelf)
  90. {
  91. Agumon.transform.localPosition = defaultAgumonPos;
  92. }
  93. }
  94. public void Off()
  95. {
  96. this.gameObject.SetActive(false);
  97. }
  98. }