TweenAlphaImage.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. using System.Collections;
  5. using UnityEngine.UI;
  6. using UnityEngine;
  7. namespace RadarComponents
  8. {
  9. [RequireComponent(typeof(Image))]
  10. public class TweenAlphaImage : AbstractExtensionTarget
  11. {
  12. [Header("Ping Radar Option")]
  13. [SerializeField]
  14. private bool pingPong = default;
  15. [Header("Lenght of animation:")]
  16. [SerializeField]
  17. private float durationAnim = 0.5f;
  18. [SerializeField]
  19. private Color32 startColor = default;
  20. [SerializeField]
  21. private Color32 resultColor = default;
  22. private Coroutine coroutine = null;
  23. private Image image;
  24. private bool busy = false;
  25. private void Awake()
  26. {
  27. image = GetComponent<Image>();
  28. image.enabled = !pingPong;
  29. }
  30. public override void UpdateExtensionView(Transform player, ITarget inputTarget)
  31. {
  32. StartLerp();
  33. }
  34. /// <summary>
  35. /// StartCoroutine if she already played
  36. /// </summary>
  37. public void StartLerp()
  38. {
  39. if (!busy || !pingPong)
  40. {
  41. if (coroutine != null)
  42. {
  43. StopCoroutine(coroutine);
  44. }
  45. coroutine = pingPong ? StartCoroutine(LerpPing()) : StartCoroutine(LerpOnce());
  46. }
  47. }
  48. //FIXME: This part code is soooooooooooooooooo bad >:)
  49. private IEnumerator LerpOnce()
  50. {
  51. float ElapsedTime = 0f;
  52. float end = Time.unscaledDeltaTime + durationAnim;
  53. busy = true;
  54. while (ElapsedTime < end)
  55. {
  56. ElapsedTime += Time.deltaTime;
  57. image.color = Color.Lerp(startColor, resultColor, (ElapsedTime / end));
  58. yield return null;
  59. }
  60. busy = false;
  61. }
  62. private IEnumerator LerpPing()
  63. {
  64. float ElapsedTime = 0f;
  65. float end = Time.unscaledDeltaTime + durationAnim;
  66. busy = true;
  67. image.enabled = true;
  68. while (ElapsedTime < end)
  69. {
  70. ElapsedTime += Time.deltaTime;
  71. image.color = Color.Lerp(startColor, resultColor, (ElapsedTime / end));
  72. yield return null;
  73. }
  74. ElapsedTime = 0f;
  75. end = Time.unscaledDeltaTime + durationAnim;
  76. while (ElapsedTime < end)
  77. {
  78. ElapsedTime += Time.deltaTime;
  79. image.color = Color.Lerp(resultColor, startColor, (ElapsedTime / end));
  80. yield return null;
  81. }
  82. busy = false;
  83. image.enabled = false;
  84. }
  85. }
  86. }