Skip to content
Snippets Groups Projects
Select Git revision
  • 1e726ba723f348f63166e67660aecba9deefc5a6
  • master default protected
2 results

FadeText.cs

Blame
  • FadeText.cs 938 B
    using System.Collections;
    using System.Collections.Generic;
    using TMPro;
    using UnityEngine;
    
    // source: https://www.youtube.com/watch?v=WiUUW9RSa5Y
    public class FadeText : MonoBehaviour
    {
        private readonly float fadeTime = 1f;
        private TMP_Text fadeAwayText;
        private float alphaValue;
        private readonly float FPS = 30f;
        private float da;
    
        private void Awake()
        {
            fadeAwayText = GetComponent<TMP_Text>();
        }
    
        void Start()
        {
            da = fadeTime / FPS;
            alphaValue = 1f;
            fadeAwayText.color = new Color(fadeAwayText.color.r, fadeAwayText.color.g, fadeAwayText.color.b, alphaValue);
            InvokeRepeating(nameof(ReduceOpacity), 1.5f, 1f / FPS);
        }
    
        private void ReduceOpacity()
        {
            if (alphaValue <= 0f) return;
            alphaValue -= da;
            fadeAwayText.color = new Color(fadeAwayText.color.r, fadeAwayText.color.g, fadeAwayText.color.b, alphaValue);
        }
    }