• 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 EntertainmentForge · Oct 12, 2017 at 02:43 PM · textdialogue

Animated Dialogue Text - Problem With Line Break

Hello!

I am making a game with animated dialogue and I run into a problem when one word is being written in one line and then when it takes too much space it jumps down to the next line. I demonstrated that in this video (I slowed down text writing so it's obvious what is happening). You can see the word "possible" being written in one line, and then finished down on next line.

https://www.youtube.com/watch?v=ZCu4M2OFGRw&feature=youtu.be

Does anyone have any idea how we could predict that word is going to be too long for that line, and then start writing it in next line right away? I had idea of testing how long the next word is going to be, by checking how many characters there are until the next Space in string. But even if I do this, I don't know how to test if that word is going to be too long to fit in current line or will it be pushed into the next line.

Here is simplified version of function that I run in Update() to animate text:

 if (currentlyAnimatedText.Length > 0)
                 {
                     char letter = char.Parse(currentlyAnimatedText.Substring(0, 1));
 
                     currentlyUsedDialogueBox.text += letter;
                     currentlyAnimatedText = currentlyAnimatedText.Remove(0, 1);
                 }

Any ideas? Thanks!

Comment
Add comment · Show 1
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 EntertainmentForge · Oct 12, 2017 at 08:24 PM 0
Share

Btw I know I can go thought text and add \n manually. But that would be super inefficient way of solving this issue.

2 Replies

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

Answer by EntertainmentForge · Oct 13, 2017 at 11:09 AM

I ended up solving this by instead of writing char by char, I wrote whole dialogue text but then changed color of the text to alpha 0. And then instead of adding char's I just moved this tag "") thought the text, making it look like it's writing text, even thought whole text is already there.

Here is the simplified code:

     string currentlyAnimatedText = "";//assign dialogue text here
         string tempAnimatedText = "";
         int currentlyAnimatedIndex;
     
     void Update()
         {
                         if (currentlyAnimatedIndex < currentlyAnimatedText.Length)
                         {
                             currentlyAnimatedIndex++;

                              currentlyUsedDialogueBox.text = currentlyAnimatedText;
                              tempAnimatedText = currentlyUsedDialogueBox.text.Insert(currentlyAnimatedIndex, "<color=#00000000>");
                         currentlyUsedDialogueBox.text = tempAnimatedText;
                         }
                     
                 
         }


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 ghostmode · Oct 13, 2017 at 11:35 AM 0
Share

Creative solution!

avatar image eyeballTickler · Aug 29, 2018 at 10:47 PM 0
Share

Thank you for this! Very clever solution.

For anyone else confused by the formatting, this tag "") is referring to the color tag with a color of #00000000 (black with an alpha of zero) used with Text$$anonymous$$eshPro.

avatar image
1

Answer by ghostmode · Oct 13, 2017 at 06:41 AM

You'll need to figure out how best to incorporate this into your dialog system, but as a start you can determine the width required to display a string as follows:

 var text = GetComponent<Text>();
 var textGenerator = text.cachedTextGenerator;
 var textSettings = text.GetGenerationSettings(Vector2.zero);
 
 string dialog = "Hello, world!";
 float width = textGenerator.GetPreferredWidth(dialog, textSettings);
 Debug.Log("width needed: " + width);

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 EntertainmentForge · Oct 13, 2017 at 11:03 AM 1
Share

I didn't manage to figure out how to use this to fix the problem. But thanks for helping anyway!

avatar image ghostmode EntertainmentForge · Oct 13, 2017 at 11:32 AM 1
Share

Fair enough. Your code for the animated text is short and sweet, but to add this feature it needs to get a bit more complicated unfortunately. The right answer to this may be "You can, but it's probably not worth the effort", but I'll provide some more details.

In order to know when to move to a new line, you'll need to keep track of:

  • max line width

  • current line width

  • word width

You'll need to check if each word fits before you start writing it.

Check if the 'current line width' + 'word width' > 'max line width'. If it is then write a "\n" and set 'current line width' = 0, then write the word.

When you write a word, update 'current line width' += 'word width'.

avatar image EntertainmentForge ghostmode · Oct 13, 2017 at 12:02 PM 0
Share

Yeah your're right. It's possible but very tricky. Plus I am using text mesh pro for text and I think I don't have cachedTextGenerator and GetGenerationSettings features. Unless I missed something.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Inserting specific words into descriptions stored in XML 0 Answers

Check current letter in typewriter textbox? 0 Answers

How to run onstart and fix this error 1 Answer

Scripting text/dialogue onto an object 1 Answer

Display several paragraphs of text 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges