Simple C# script to create self writing text effect in Unity UI.

 

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

Updated Script:

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

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

//Writes text letter by letter with time step

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

    [SerializeField] private float letterPause = 0.1f;

    [TextAreaAttribute(4, 15)]
    [SerializeField] private string textToShow;

    private string message;

    private void Start()
    {
        message = textToShow;

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

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

        textToUse.text = "";

        StartCoroutine(TypeText(textToUse, message, letterPause));
    }

    private IEnumerator TypeText(Text text, string textText, float timePause)
    {
        for(int i = 0; i < textText.Length; i++)
        {
            text.text += textText[i];

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

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

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

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

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

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

        if (newLetterPause > 0.0f)
        {
            StartCoroutine(TypeText(textToUse, message, letterPause));
            return;
        }
    }
}

 

Old Script:

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

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

//Writes text letter by letter with time step

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

    [SerializeField] private float letterPause = 0.1f;

    [TextAreaAttribute(4, 15)]
    [SerializeField] private string textToShow;

    private string message;

    private void Start()
    {
        message = textToShow;

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

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

        textToUse.text = "";

        StartCoroutine(TypeText());
    }

    IEnumerator TypeText()
    {
        for(int i = 0; i < message.Length; i++)
        {
            textToUse.text += message[i];

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

 



Unity UI C# Self Writing Text
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