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
4
Question by Fuhans92 · Mar 30, 2014 at 11:16 AM · c#text

Scrolling typewriter effect

I would like to make a game where the text already been defined, and when I run the game or program, it will display the text, but not fully text on once, but as one word by words. Something like this:

http://wigflip.com/screedbot/ and http://www.youtube.com/watch?v=uFQ6MspAMq8 (The youtube video is already give the example, but it is on c# xna)

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

5 Replies

· Add your reply
  • Sort: 
avatar image
14

Answer by Laik · Jun 16, 2015 at 05:43 AM

Here is one i wrote that i feel works better:

     public Text textBox;

     //Store all your text in this string array
 string[] goatText = new string[]{"1. Laik's super awesome custom typewriter script", "2. You can click to skip to the next text", "3.All text is stored in a single string array", "4. Ok, now we can continue","5. End Kappa"};

 int currentlyDisplayingText = 0;

 void Awake () {
     StartCoroutine(AnimateText());
 }

     //This is a function for a button you press to skip to the next text
 public void SkipToNextText(){
     StopAllCoroutines();
     currentlyDisplayingText++;
    //If we've reached the end of the array, do anything you want. I just restart the example text
     if (currentlyDisplayingText>goatText.Length) {
         currentlyDisplayingText=0;
     }
     StartCoroutine(AnimateText());
 }

    //Note that the speed you want the typewriter effect to be going at is the yield waitforseconds (in my case it's 1 letter for every      0.03 seconds, replace this with a public float if you want to experiment with speed in from the editor)
 IEnumerator AnimateText(){
     
     for (int i = 0; i < (goatText[currentlyDisplayingText].Length+1); i++)
     {
         goatTalkingText.text = goatText[currentlyDisplayingText].Substring(0, i);
         yield return new WaitForSeconds(.03f);
     }

 }



And that's it! Works perfectly well and doesn't clog up Update

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 e4ajith · Feb 04, 2016 at 07:12 AM 0
Share

Thank you Laik. Works like charm..

avatar image vinternet2000 · Mar 04, 2016 at 05:19 AM 0
Share

This is great, thank you! $$anonymous$$y next step for my game is to figure out how to predict the number of lines of text that will be displayed on the screen, and center the text vertically in my dialog box prior to all of it being there. Right now, it re-centers as the typehead gets to the second line.

avatar image XDSoftworks · Aug 28, 2016 at 06:27 PM 0
Share

nvm fixed it :D im stupid XD .

avatar image
1

Answer by patrik-org · Mar 31, 2014 at 01:22 PM

I'm assuming you already know how to display text on the screen. Next you need a script that updates this text every frame:

 public string textShownOnScreen;
 public string fullText = "The text you want shown on screen with typewriter effect.";
 public float wordsPerSecond = 2; // speed of typewriter
 private float timeElapsed = 0;   
 
 void Update()
 {
     timeElapsed += Time.deltaTime;
     textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond);
 }
 
 private string GetWords(string text, int wordCount)
 {
     int words = wordCount;

     // loop through each character in text
     for (int i = 0; i < text.Length; i++)
     { 
         if (text[i] == ' ')
         {
             words--;
         }

         if (words <= 0)
         {
             return text.Substring(0, i);
         }
     }

     return text;
 }

The above code updates the textShownOnScreen string to get a typewriter effect. You might want to modify GetWords() in how it counts words - right now any space it encounters in the text it interprets as being a new word.. I'm sure there are better ways but this might help you get started.

Comment
Add comment · Show 2 · 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 Madcowe · Dec 03, 2014 at 03:11 PM 0
Share

Hey @exmakina , even though it isn't marked as solved this helped me out quite a bit :P

However it says I can't convert a float to int... (on the textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond); part)... I tried creating an int for that but it only gets changed every second so it's weird...

But great and simple answer :)

avatar image USMANHEART · Dec 21, 2016 at 09:24 AM 0
Share

You are great. nice work

avatar image
1

Answer by USMANHEART · Dec 21, 2016 at 09:41 AM

If you want to show On your textbox also alphabet per frame, then follow this

public class Testing : MonoBehaviour {

     public string textShownOnScreen;
 public Text textBox;
 public string fullText = "The text you want shown on screen with typewriter effect.";
 public float wordsPerSecond = 2; // speed of typewriter
 private float timeElapsed = 0;   
 

void Update() {

  timeElapsed += Time.deltaTime;

     textShownOnScreen = GetWords(fullText,  timeElapsed * wordsPerSecond);

}

 private string GetWords(string text, float wordCount) {
     float words = wordCount;

  // loop through each character in text
  for (int i = 0; i < text.Length; i++)
  { 
         
  
             words--;
             textBox.text = textShownOnScreen;

      
      if (words <= 0)
      {
          return text.Substring(0, i);
      }
  }
  return text;

} }

Comment
Add comment · 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
0

Answer by Stoneheart · Mar 10, 2015 at 02:40 PM

In the provided code do this: public float wordsPerSecond = 2f; // speed of typewriter private float timeElapsed = 0f;

Comment
Add comment · 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
0

Answer by MetalStorm80 · Jun 14, 2015 at 07:31 AM

This was very handy, I still don't get the return text.Substring(0, i); and return text; but it works and I will slowly figure out why.

Comment
Add comment · 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

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

How to create a texture, fill it with some color, add a text at a center and to set this texture to some object in C#? 0 Answers

How to display text on a cube and how to change it dynamically? 2 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