48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
|
|
public class UIFadeText : MonoBehaviour
|
|
{
|
|
public Text uiText; // Das UI-Text-Element
|
|
public float fadeDuration = 1.5f; // Dauer des Fade-Effekts
|
|
public float waitTime = 1.0f; // Wartezeit zwischen Fade-Out und Fade-In
|
|
|
|
private void Start()
|
|
{
|
|
if (uiText == null)
|
|
{
|
|
uiText = GetComponent<Text>(); // Falls kein Text zugewiesen ist, wird dieser gesucht.
|
|
}
|
|
StartCoroutine(FadeTextLoop());
|
|
}
|
|
|
|
private IEnumerator FadeTextLoop()
|
|
{
|
|
while (true)
|
|
{
|
|
yield return StartCoroutine(FadeText(1, 0)); // Text ausblenden
|
|
yield return new WaitForSeconds(waitTime);
|
|
yield return StartCoroutine(FadeText(0, 1)); // Text einblenden
|
|
yield return new WaitForSeconds(waitTime);
|
|
}
|
|
}
|
|
|
|
private IEnumerator FadeText(float startAlpha, float endAlpha)
|
|
{
|
|
float elapsedTime = 0;
|
|
Color textColor = uiText.color;
|
|
|
|
while (elapsedTime < fadeDuration)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float newAlpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / fadeDuration);
|
|
uiText.color = new Color(textColor.r, textColor.g, textColor.b, newAlpha);
|
|
yield return null;
|
|
}
|
|
|
|
uiText.color = new Color(textColor.r, textColor.g, textColor.b, endAlpha);
|
|
}
|
|
}
|
|
|
|
//https://chatgpt.com |