Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Barsonax · Apr 14, 2016 at 06:00 PM · uitextscaling

How to make a textbox like they have in pokemon

How do you make a text box like they have in pokemon? I managed to make something similar with the following code:

 using UnityEngine;
 using System.Collections;
 using Extensions;
 using UnityEngine.UI;
 
 public class DialogController : MonoBehaviour
 {
     private Text _textComponent;
     private string _textToDraw = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum eros ultricies tristique tincidunt. Nulla tristique rhoncus elit vel fringilla. Nunc egestas bibendum risus eget accumsan. Integer mauris";
 
     void Start()
     {
         _textComponent = gameObject.FindInChildren("Text").GetComponent<Text>();
         _textComponent.text = string.Empty;
         StartRollingText(_textToDraw,0.1f);
     }
 
     public void StartRollingText(string text, float time)
     {
         StartCoroutine(DrawText(text,time));
     }
 
     IEnumerator DrawText(string text,float time) //Keeps adding more characters to the Text component text
     {
         foreach (char c in text)
         {
             _textComponent.text += c;
             yield return new WaitForSeconds(time);
         }
     }
 }

I should probably use something like substring to boost performance but just to test this out its fine.

There is however a issue with this code. Sometimes when its at the end of a line it can suddenly make the word jump to the next line while its typing it because it gets too long. Now i can probably think of some hacky way to fix this but whats the best practice for this?

Comment
Add comment · Show 7
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 Blue-Cut · Apr 14, 2016 at 06:10 PM 0
Share

Is it important to you to do this with a coroutine rather than activating an Update function as long as your text is displaying ? Coroutines are a bit more tricky I think, and this way to do might the cause of your problem. (Anyway, even if you try another way than coroutine, we still can answer the original question, it will be usefull for everybody)

avatar image Blue-Cut · Apr 14, 2016 at 06:16 PM 0
Share

Another thing is that I don't think I have ever seen examples of coroutines with a "for" loop. Did you try something like writing a coroutine that just do the "wait" then add a letter. And this coroutine is called by a coroutine with a while loop that waits for the complete chain. Don't know if it's helping but in my head, that is the way I would try to perform this.

avatar image Barsonax Blue-Cut · Apr 14, 2016 at 06:21 PM 0
Share

I just used coroutine here because i wanted a delay between the updates but without blocking anything. I could do this with a for loop aswell but foreach was faster to write. It works fine except for the fact that the words sometimes jump to the next line because the previous line gets too long.

avatar image Blue-Cut Barsonax · Apr 14, 2016 at 06:28 PM 0
Share

So you mean that what you need to do is calcultating the size of the next word and check if it has to be written on the following line ? (by for I meant foreach)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessespike · Apr 14, 2016 at 07:30 PM

It's easier to determine if a word needs a new line if the font has fixed width characters, because then you can just check if the word exceeds the character limit on a line. This isn't the case though, so what this snippet does is:

  • makes a backup of the current visible text, and stores the text component's height,

  • add the next word to the text component, and check the components height, compare it to the previous height calculation.

  • if they are different than add a new line character to the visible text.

  • set the textcomponent back to the visible text and add each character in the next word.

This is probably not the best, or cleanest solution, but I think it's on the right track:

 IEnumerator DrawText(string text,float time) //Keeps adding more characters to the Text component text
 {
     string[] words = text.Split(' ');
     for (int i = 0; i < words.Length; i++)
     {
         string word = words[i];
         // add a space for words except for the last
         if (i != words.Length - 1) word += " ";    
         
         string previousText = _textComponent.text;
         
         // determine if next word needs to be on new line
         float lastHeight = _textComponent.preferredHeight;
         _textComponent.text += word;
         if (_textComponent.preferredHeight > lastHeight)
         {
             previousText += System.Environment.NewLine;
         }

         for (int j = 0; j < word.Length; j++)
         {
             _textComponent.text = previousText + word.Substring(0, j+1);
             yield return new WaitForSeconds(time);
         }
     }
 }


Comment
Add comment · Show 1 · 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 Barsonax · Apr 15, 2016 at 05:14 PM 0
Share

Hmm checking the height is pretty smart actually. I was first thinking of actually calculating the length of the words and check if they fit or not.

This is of course already done internally to deter$$anonymous$$e if a word should go to the next line and by checking the height you are using the information thats already there.

This a nice idea iam going to use to make a nice little easy to use script for.

Edit: I have slightly modified your script to fix a bug in it. Now when a newline is inserted it will remove the last character which should be always a space. This is to prevent the text from double jumping (the space can jump to the next line and the newline will do this again). Other than that this is working. Nice!

     /// <summary>
     /// This coroutine will draw text over time in a textcomponent
     /// </summary>
     /// <param name="text"></param>The text to draw
     /// <param name="time"></param>The time before the next char should be drawn
     /// <returns></returns>
     IEnumerator DrawText(string text, float time)
     {
         string[] words = text.Split(' ');
         for (int i = 0; i < words.Length; i++)
         {
             string word = words[i];
             // add a space for words except for the last
             if (i != words.Length - 1) word += " ";
 
             string previousText = _textComponent.text;
             
             // deter$$anonymous$$e if next word needs to be on new line
             float lastHeight = _textComponent.preferredHeight;
             _textComponent.text += word;
             if (_textComponent.preferredHeight > lastHeight)
             {
                 previousText = previousText.Remove(previousText.Length - 1); //Removes the last character. This is always a space and thus we dont want this because it can make our text jump 2 lines.
                 previousText += System.Environment.NewLine;
             }
             for (int j = 0; j < word.Length; j++)
             {
                 _textComponent.text = previousText + word.Substring(0, j + 1);
                 yield return new WaitForSeconds(time);
             }
         }
     }

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

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

Related Questions

Unity 5.0.2 UI Text - Best Fit isn't working correctly? 1 Answer

Unity 5 - Does anyone know how to scale the text in the UI? 1 Answer

Resizing font by scale according to screen size, (More complicated than you might think) 1 Answer

MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. 5 Answers

how do i change max size value of text component unity? 1 Answer


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