- Home /
DIY Dialog System (type-writer effect)
I'm trying to come up with a dialogue 'system' that mimics the type-writer effect with each letter appearing at a time. I just threw this thing together and am lost on how to make this more efficient and easier to use. I'd appreciate any help I can get!
Here's what I have right now, all to produce a simple "Hello world!":
void OnGUI ()
{
GUI.Label (new Rect (Screen.width / 2, Screen.height / 2, 500, 500), msg01);
if (Time.time >= currentTime + .05 && msg01 == "H")
{
currentTime = Time.time;
msg01 += "E";
}
else if (Time.time >= currentTime + .05 && msg01 == "HE")
{
currentTime = Time.time;
msg01 += "L";
}
else if (Time.time >= currentTime + .05 && msg01 == "HEL")
{
currentTime = Time.time;
msg01 += "L";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELL")
{
currentTime = Time.time;
msg01 += "O";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELLO")
{
currentTime = Time.time;
msg01 += " ";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELLO ")
{
currentTime = Time.time;
msg01 += "W";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELLO W")
{
currentTime = Time.time;
msg01 += "O";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELLO WO")
{
currentTime = Time.time;
msg01 += "R";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELLO WOR")
{
currentTime = Time.time;
msg01 += "L";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELLO WORL")
{
currentTime = Time.time;
msg01 += "D";
}
else if (Time.time >= currentTime + .05 && msg01 == "HELLO WORLD")
{
msg01 += "!";
}
}
Answer by $$anonymous$$ · Mar 29, 2014 at 05:18 PM
You can ditch the whole if and else statements by updating the string from a full string with the entire word, try this:
public string msg01; //What should be shown in the label
private string output = "HELLO WORLD"; //The full string
private int pos = 0; //Char position in the output string
public float currentTime;
void Update ()
{
//Update text only when it reach the desired time and the message is different from the full word
if(Time.time >= currentTime + .05f && msg01 != output) {
pos++; //Increase char position by 1
currentTime = Time.time;
msg01 = output.Substring(0, pos); //Update msg01 string word by word using the pos index
}
}
// Use this for initialization
void OnGUI ()
{
GUI.Label (new Rect (Screen.width / 2, Screen.height / 2, 500, 500), msg01);
}
You can set pos to -1 if you want, this will prevent the word from starting from the first char unless this is the way you want it. I left some comments to explain what I did. This way you can build a more dynamic system by setting the full word that should be shown.
Just a note spaces are threated as chars too, so it will "type" any space, if you want to avoid that you can try by putting the final text into an array of chars and Split the spaces.
That is amazing! Thanks very much!
By the way, is there a particular reason why you set msg01 to public and output to private as opposed to the other way round? I'm asking because switching them now allows me to change what I'd like printed right in the editor.
Lol my bad, I did the other way around. Yeah you can keep msg01 on private and keep the output string on public, now is dynamic and you can change it by referencing it directly.
Your answer
Follow this Question
Related Questions
How to create a point/score system based on player performance. 2 Answers
Linking event or custom callback for gui elements by using custom UI System 1 Answer
Problems with simple dialogue 0 Answers
Creating a dialogue with Physics.OverlapSphere 0 Answers
On click add listener not working ! 0 Answers