- Home /
The question is answered, right answer was accepted
I have a "display text one letter at a time" script. Can I modify it to display a complete string of text on command?
This is a modified version of AutoType.js, a script from the UnifyCommunity Wiki. The purpose of the script is to display a string of text one letter at a time. It goes onto a GameObject with a GUIText component.
var currentPosition : int = 0;
var Delay : float = 0.1; //Or whatever you want the delay to be
var Text : String = "";
var additionalLines : String[];
function WriteText(aText : String) {
guiText.text = "";
currentPosition = 0;
Text = aText;
}
function Start(){
for ( var S : String in additionalLines )
Text += "\n" + S;
while (true){
if (currentPosition < Text.Length)
guiText.text += Text[currentPosition++];
yield WaitForSeconds (Delay);
}
}
I would like to modify this script so that if the user presses any key, the script will stop displaying text one letter at a time, and will just display the whole string of text. So far, I've been unsuccessful in achieving this. Can someone please explain to me how to pull it off?
Here ye go. Hit Enter and the text typing coroutine will stop and the entire message will show.
using UnityEngine;
using System.Collections;
public class mainText : $$anonymous$$onoBehaviour
{
public float letterPause = 0.05f;
public AudioClip sound;
public GUIStyle font;
string message;
string text;
void Start ()
{
message = "Welcome new warrior! This is a blue screen, you must fight your way out! " +
"Without any hands or other parts. This may take some time...";
text = "";
StartCoroutine(TypeText());
}
IEnumerator TypeText ()
{
foreach (char letter in message.ToCharArray())
{
text += letter;
if (sound)
audio.PlayOneShot (sound);
yield return 0;
yield return new WaitForSeconds (letterPause);
}
}
void OnGUI()
{
GUI.Label(new Rect(0, 0, 256, 1024), text, font);
}
void Update()
{
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return))
{
StopAllCoroutines();
text = message;
}
}
}
Answer by Seth-Bergman · Jul 29, 2012 at 10:38 AM
try something like:
var displayAll = false;
function Update(){
if(Input.GetButtonDown("Fire1")){
displayAll = true;
}
if(displayAll)
guiText.text = Text;
}
you'll want to add a check in the rest:
function Start(){
if(displayAll)
return;
for ( var S : String in additionalLines )
Text += "\n" + S;
while (true){
if (currentPosition < Text.Length)
guiText.text += Text[currentPosition++];
yield WaitForSeconds (Delay);
}
}
Follow this Question
Related Questions
Fix Blurry UI text? 10 Answers
Create GUIText from Javascript 3 Answers
Gui text, ammo counter 1 Answer