- Home /
The question is answered, right answer was accepted
Making Text Boxes With Letters Appearing One At A Time
I've been trying to do something like this to tell the story in my game.
But I've been unable to figure out how to do it, mainly because I can't think of any ways to make one letter appear on the screen at a time, rather than a character's entire line.
So I must ask. How can I make text boxes that have letters appear one at a time instead of all at once? While I'm asking, links to any tutorials and such regarding things like that would be appreciated., since I'm fairly new to Unity.
Answer by Berenger · Feb 21, 2012 at 06:09 AM
The GUI stuff isn't really related here. How Fill a string one letter at a time ?
private var str: string; function Start(){ AnimateText("Pretty cool text"); }
function AnimateText(string strComplete){ var i: int = 0; str = ""; while( i < strComplete.Length ){ str += strComplete[i++]; yield WaitForSeconds(0.5); } }
and C# (need to be explicitely inside a class, but I guess you already know that)
private string str; void Start(){ StartCoroutine( AnimateText("Pretty cool text") ); }
IEnumerator AnimateText(string strComplete){ int i = 0; str = ""; while( i < strComplete.Length ){ str += strComplete[i++]; yield return new WaitForSeconds(0.5F); } }
Then the GUI part is obvious.
Can I get that in C#? I keep getting a load of errors when trying to convert it myself.
Facepalm is correct. You just need to call animateText with StartCoroutine() then. And animateText doesn't need to be public.
O$$anonymous$$ thanks. What I'm not getting now is how exactly I'm supposed to display that. I tried making another class and putting it in like so:
void OnGUI(){ GUI.Box(new Rect(10,400,1260,225), "placeholder"); }
With a whole slew of things in the slot where you see the placeholder... but not a single one of the many things I've tried actually shows this self-filling string ins$$anonymous$$d of just bringing up an error message.
use the variable str ins$$anonymous$$d of "placeholder". If that's what you mean, I'm not sure.
Answer by Facepalm · Feb 21, 2012 at 08:20 AM
public class AnimatedText : MonoBehaviour {
public AnimatedText(){}
public void animateText(string strComplete){
int i = 0;
string str = "";
while(i < strComplete.Length){
str += strComplete[i++];
new WaitForSeconds(0.5f);
}
}
}
Follow this Question
Related Questions
How to get a pop-up window floating next to a gameobject? 1 Answer
Providing a start location for GUILayout.beginHorizontal ? 0 Answers
How can I clear a textbox and repeat the text with my effect? 0 Answers
Simple text in a box - on player collision 2 Answers
How do you get a caret to show in GUILayout.TextField ??? 0 Answers