Simple C# script to create flashing UI text effect.

Content not available.
Please allow cookies by clicking Accept on the banner

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// © 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

//Shows string and then hides it with time step

public class FlashingText: MonoBehaviour {
    [SerializeField] private Text textToUse;
    [SerializeField] private bool useThisText = false;
    [SerializeField] private bool useThisTextText = false;
    [SerializeField] private string flashingString;

    [SerializeField] private float textPause = 0.5f;

    private void Start()
    {
        if (useThisText)
        {
            textToUse = GetComponent<Text>();
        }

        if (useThisTextText)
        {
            flashingString = textToUse.text;
        }

        textToUse.text = "";

        StartCoroutine(TypeText(textToUse, flashingString, textPause));
    }

    private IEnumerator TypeText(Text text, string stringToUse, float timePause)
    {
        bool show = true;

        while (true)
        {
            if (show)
            {
                textToUse.text = stringToUse;
            }
            else
            {
                textToUse.text = "";
            }

            show = !show;

            yield return 0;
            yield return new WaitForSeconds(timePause);
        }
    }

    public void WriteText(Text newText = null, string newTextToShow = null, float newTextPause = -1.0f)
    {
        if (newText != null && newTextToShow != null && newTextPause > 0.0f)
        {
            StartCoroutine(TypeText(newText, newTextToShow, newTextPause));
            return;
        }

        if (newText != null && newTextToShow != null)
        {
            StartCoroutine(TypeText(newText, newTextToShow, textPause));
            return;
        }

        if (newText != null && newTextPause > 0.0f)
        {
            StartCoroutine(TypeText(newText, flashingString, newTextPause));
            return;
        }

        if (newTextToShow != null && newTextPause > 0.0f)
        {
            StartCoroutine(TypeText(textToUse, newTextToShow, newTextPause));
            return;
        }

        if (newTextToShow != null)
        {
            StartCoroutine(TypeText(textToUse, newTextToShow, textPause));
            return;
        }

        if (newTextPause > 0.0f)
        {
            StartCoroutine(TypeText(textToUse, flashingString, textPause));
            return;
        }
    }
}

 



Unity UI C# Flashing Text Effect
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close