using System.Collections;
using TMPro;
using UnityEngine;

namespace Animations
{
	public static class TextMeshFadeExtension
	{
		public static void TextFade(this TextMeshPro textMeshPro, float targetAlpha, float duration)
		{
			textMeshPro.StartCoroutine(TextFading(textMeshPro, targetAlpha, duration));
		}
		
		private static IEnumerator TextFading(TextMeshPro textMeshPro, float targetAlpha, float duration)
		{
			float progress = 0;
			float startAlpha = textMeshPro.alpha;
			
			while (progress < duration)
			{
				progress += Time.deltaTime;
				progress = Mathf.Min(progress, duration);
				textMeshPro.alpha = Mathf.Lerp(startAlpha, targetAlpha, progress / duration);
				yield return null;
			}
		}
	}
}