C# script to make text change after time with random/forward/backward order options.

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


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

public class ChangingText : MonoBehaviour {
    [SerializeField] private Text textToUse;
    [SerializeField] private bool useThisText = false;
    [SerializeField] private List<string> textsToShow;
    [SerializeField] private float timeToChangeText;
    [SerializeField] private bool changeTextAfterTime = false;
    [SerializeField] private bool changeTextRandomly = false;
    [SerializeField] private bool changeTextForward = false;

    private float timeToChangeTextStore;

    private int prevTextNum = 0;

    private void Start()
    {
        timeToChangeTextStore = timeToChangeText;

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

        textToUse.text = "";

        timeToChangeText = -1.0f;

        if (changeTextForward)
        {
            prevTextNum = textsToShow.Count - 1;
        }
    }

    private void Update()
    {
        if (changeTextAfterTime)
        {
            if (timeToChangeText <= 0.0f) {
                ChangeText();

                timeToChangeText = timeToChangeTextStore;
            }

            timeToChangeText -= Time.deltaTime;
        }
    }

    public void ChangeText()
    {
        if (changeTextRandomly)
        {
            int nextNum = Random.Range(0, textsToShow.Count);

            if (nextNum == prevTextNum)
            {
                if (nextNum == 0)
                {
                    nextNum++;
                }
                else
                {
                    nextNum--;
                }
            }

            textToUse.text = textsToShow[nextNum];

            prevTextNum = nextNum;
        }
        else
        {
            if (changeTextForward)
            {
                if (prevTextNum == textsToShow.Count - 1)
                {
                    prevTextNum = 0;
                }
                else
                {
                    prevTextNum++;
                }
            }
            else
            {
                if (prevTextNum == 0)
                {
                    prevTextNum = textsToShow.Count - 1;
                }
                else
                {
                    prevTextNum--;
                }
            }

            textToUse.text = textsToShow[prevTextNum];
        }
    }

    public void StartStopChanging()
    {
        changeTextAfterTime = !changeTextAfterTime;
    }

    public void StartChangingRandomly()
    {
        changeTextRandomly = true;
    }

    public void StopChangingRandomly()
    {
        changeTextRandomly = false;
    }

    public void ChangeTextForward()
    {
        changeTextForward = true;
    }

    public void ChangeTextBackward()
    {
        changeTextForward = false;
    }
}

 

Unity UI C# Changing Text In Order
Tagged on:         

Leave a Reply

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