• 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 Lao2211 · Mar 17, 2017 at 11:24 PM · text

Why CharacterInfo.advance and CharacterInfo.width is always 0?

I'm trying to implement text appearing char by char to get a visual novel style story telling.

The code example in the wiki doesn't work for me because this doesn't take into account word width, and when you reach the end of a Text component width the whole word jump to the beginning of the next line. I need to know the width of the word in advance so I can decide if the first character of that word will start at the next line or the current line.

For this, I'm using the metrics provided by the CharacterInfo structure but I'm getting all zeros even for common ascii characters. I will elaborate, this is a simplified version of the problematic code:

 Text text = mGUIText.GetComponent<Text> (); // mGUIText is a GameObject
 CharacterInfo cinfo;
 Font font = text.font;

And then, inside a loop:

 char c = str[i]; // str is a string containing the dialogue
 font.GetCharacterInfo (c, out cinfo, text.fontSize, text.fontStyle);

I cannot properly calculate the width of words or lines of text because cinfo.advance and cinfo.width are always zero.

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

2 Replies

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

Answer by Lao2211 · Mar 18, 2017 at 12:07 AM

I found a solution. Per this question: http://answers.unity3d.com/questions/1071345/inconsistent-results-with-fontgetcharacterinfo.html

It seems that a font metrics is know by Unity only after some text that uses that font got actually rendered. Maybe Unity generates the texture containing the glyphs on the go, as I don't see these textures on my filesystem, I would bet that how font rendering is implemented.

Game engines, and Unitys seems not to be the exception, package as many glyphs as it can in a single texture, this reduces texture switching during rendering that are considered a slow operation for real time rendering, so the engine can render many characters without requiring a texture switch. Same concept as packing sprites efficiently in as few texture atlases as you can.

So here is how I solved it, I won't post my implementation but I will describe the process:

  • Using the editor, create the main Canvas of your GUI system and then drop the Text that you will use as the dialog box to tell your story in visual novel style. Tweak its bounding rectangle to switch your project.

  • Then on the script: first set the whole text of a single dialogue in a single assignment to the text property of the Text component.

  • Call Canvas.ForceUpdateCanvases(); It's a static function so just copy and paste that code, you don't need a Canvas instance.

  • Then clear all the text of the Text component by setting it to an empty string "".

  • As everything happened in a single frame, the player won't see the Text component filled with the full dialogue, because you set it and cleared it in a single Update(). At least so far I didn't see it so using a hidden Text component like ActualDog suggests seems unnecessary.

  • Finally, proceed to add char by char. The code to know in advance the word width is easy to implement now that CharacterInfo contains what you need.

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 Arunraj · Dec 18, 2017 at 10:08 AM

this issue found in 2017 1.2 also. Resetting the UI.text value and setting the UI.text value -> work... Thanks @Lao2211

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

99 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 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

How to change the text inside UI>Text 1 Answer

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

CachedTextGenerator no longer has any character info in Unity 5.3 0 Answers

How do you prevent canvas text from clipping into 3D models? 1 Answer

Resizing font by scale according to screen size, (More complicated than you might think) 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