Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ARRAYEWHY · Mar 29, 2014 at 04:49 PM · guisystemdialogue

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 += "!";
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ARRAYEWHY · Mar 29, 2014 at 05:39 PM 0
Share

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.

avatar image $$anonymous$$ · Mar 29, 2014 at 05:48 PM 0
Share

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.

avatar image ARRAYEWHY · Mar 29, 2014 at 06:37 PM 0
Share

Haha, okay, thanks again for the quick reply!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges