- Home /
Using AutoType script on GUI.Label?
Hey all,
I found this neat little script on the UnifyWiki, which sort of types the text of a GUIText out like a typewriter.
var letterPause = 0.2; private var word;
function Start () { word = guiText.text; guiText.text = ""; TypeText (); }
function TypeText () { for (var letter in word.ToCharArray()) { guiText.text += letter; yield WaitForSeconds (letterPause); }
}
I would very much like to use this functionality in a GUI.Label, and my question is: can I do that?
I'm afraid I don't possess the necessary experience to adequately judge whether this is an actual possibility or just wishful thinking.
-Veliremus
Answer by Mike 3 · May 07, 2010 at 11:32 AM
Edit: Added code to let you add new words on: careful though, it will go a bit iffy if you call the AddText function too quickly before the other finishes, but i'm sure you can use it as an example to make a better one :)
var letterPause = 0.2;
var word = "Test"; // change this one in the inspector
private var currentWord = "";
function Start ()
{
TypeText (word);
}
function AddText(newText : String)
{
word = newText;
TypeText(word);
}
private function TypeText (compareWord : String) {
for (var letter in word.ToCharArray()) {
if (word != compareWord) break;
currentWord += letter;
yield WaitForSeconds (letterPause);
//for added fun, use this instead :D ...
//yield WaitForSeconds(letterPause * Random.Range(0.5, 2));
}
}
function OnGUI()
{
GUI.Label(new Rect(50, 50, 200, 30), currentWord);
}
Cool, thanks, that does work. But when I change the text of the label, the "typing" is of course over. So I would have to call the function again. But, when I do that, the old word variable remains.
I have an X$$anonymous$$L reader that feeds new lines into the label dynamically, is there a way for me to detect when this happens and then launch the typing effect again?
I can't get it work with my current array system. I'll ask someone I know for help, but thank you, as the system works. Little thing to add, I added a "currentWord = """; in your AddText function, to clear the existing word :).
Cleaned up the formatting here, so it's more readable for other people.
Your answer
Follow this Question
Related Questions
Create GUIText from Javascript 3 Answers
The next problem with gui. 1 Answer
How do I change the text of a gui image text 1 Answer
GUI Text to GUI Label Script? 1 Answer