- Home /
Question by
KnightRiderGuy · Feb 19, 2018 at 03:59 PM ·
c#uitextscrollviewautomatic
Make UI Auto typer Scroll As text fill screen
I'm currently using the method below to auto type UI text into a panel from an external text document. I'm wondering is there an elegant way to make the content of my text box scroll upwards as text fills the screen, sort of like once the text has filled with text that if there is still more text printing out to have the current text on the screen scroll upwards to allow the new text to be visible on the screen. I'm pretty sure I would need a scroll rect for this but not really sure how to work that in. I thought it would just automatically scroll up as the content of the text typer filled the screen but I guess we need to have code do that somehow??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextBoxManager : MonoBehaviour {
public float letterPause = 0.2f;
private WaitForSeconds initialWaitTime;
private WaitForSeconds pauseWaitTime;
public AudioClip typeClick;
AudioSource audioSource;
string message;
public GameObject textBox;
public Text theText;
public TextAsset textFile;
//public TextAsset textFileTwo;
public string[] textLines;
public int currentLine;
public int startAtLine;
public int endAtLine;
private void Awake()
{
initialWaitTime = new WaitForSeconds(4.5f);
pauseWaitTime = new WaitForSeconds(letterPause);
}
// Message One
public void MessageOne ()
{
textFile = Resources.Load("BonnieBarstowInfoText") as TextAsset;
Debug.Log(textFile.text);
startAtLine = 0;
endAtLine = 6;
audioSource = GetComponent<AudioSource>();
message = textFile.text;
theText.text = "";
if (textFile != null)
{
textLines = (textFile.text.Split('\n'));
}
StartCoroutine(TypeText ());
}
IEnumerator TypeText ()
{
//yield return new WaitForSeconds (4.5f); // Wait Time
yield return initialWaitTime;
foreach (char letter in message.ToCharArray())
{
theText.text += letter;
if (typeClick)
audioSource.PlayOneShot(typeClick, 0.7F);
//yield return 0;
//yield return new WaitForSeconds (letterPause);
//WaitForSeconds letterPause = new WaitForSeconds(.08f);
yield return pauseWaitTime;
}
}
}
Comment