Вы можете сделать это с компонентом Text
и функцией Mathf.Lerp
, переведя текст в цвет Color.clear
, подождав некоторое время, затем исчезнув и исчезнув.Этот пост описывает, как это сделать с помощью простой fadeInAndOut
функции.Перед затенением текста получите исходный цвет текста, затем включите компонент «Текст».После затухания восстановите цвет, затем отключите компонент «Текст».
Вот упрощенный тост с компонентом Текст:
void Start()
{
showToast("Hello", 2);
}
public Text txt;
void showToast(string text,
int duration)
{
StartCoroutine(showToastCOR(text, duration));
}
private IEnumerator showToastCOR(string text,
int duration)
{
Color orginalColor = txt.color;
txt.text = text;
txt.enabled = true;
//Fade in
yield return fadeInAndOut(txt, true, 0.5f);
//Wait for the duration
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
yield return null;
}
//Fade out
yield return fadeInAndOut(txt, false, 0.5f);
txt.enabled = false;
txt.color = orginalColor;
}
IEnumerator fadeInAndOut(Text targetText, bool fadeIn, float duration)
{
//Set Values depending on if fadeIn or fadeOut
float a, b;
if (fadeIn)
{
a = 0f;
b = 1f;
}
else
{
a = 1f;
b = 0f;
}
Color currentColor = Color.clear;
float counter = 0f;
while (counter < duration)
{
counter += Time.deltaTime;
float alpha = Mathf.Lerp(a, b, counter / duration);
targetText.color = new Color(currentColor.r, currentColor.g, currentColor.b, alpha);
yield return null;
}
}