FadeOutTextMesh.cs 923 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. namespace ARLocation.Utils
  5. {
  6. public class FadeOutTextMesh : MonoBehaviour
  7. {
  8. [FormerlySerializedAs("duration")] public float Duration = 2.0f;
  9. private TextMesh textMesh;
  10. // Use this for initialization
  11. void Start()
  12. {
  13. textMesh = GetComponent<TextMesh>();
  14. StartCoroutine("FadeOut");
  15. }
  16. IEnumerator FadeOut()
  17. {
  18. var t = Duration;
  19. var initialColor = textMesh.color;
  20. while (textMesh.color.a > 0.001f)
  21. {
  22. var color = textMesh.color;
  23. var target = new Color(color.r, color.g, color.b, 0);
  24. textMesh.color = Color.Lerp(initialColor, target, 1 - t / Duration);
  25. t -= Time.deltaTime;
  26. yield return null;
  27. }
  28. }
  29. }
  30. }