• 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 YoungDeveloper · Feb 27, 2018 at 10:17 PM · textsizefont

Font.GetCharacterInfo always returns false

Hi, I am trying to get width of a char from unity text component

 Text t = GetComponent<Text>();
 t.text = "abc";
 
 CharacterInfo info;
 
 if(t.font.GetCharacterInfo('a', out info, t.fontSize)){
     //Never returns true
 }

I have tried unity default Arial font and many other but none of them successfully return true. What might be the problem?

Comment
Add comment · Show 2
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 Chik3r · Feb 27, 2018 at 11:00 PM -1
Share

If the character ch with the specified size and style is present in the font texture, then this method will return true, and info will contain the texture placement information for that character. If the character is not present, this method returns false. If size is zero, it will use the default size for the font. Reference: https://docs.unity3d.com/ScriptReference/Font.GetCharacterInfo.html

avatar image YoungDeveloper Chik3r · Feb 27, 2018 at 11:31 PM 1
Share

Did you test my example or just reposted the docs? Character 'a' is clearly in default Arial font. I even debugged font.characterInfo array which in fact prints 'a' as existing character, but GetCharacterInfo still returns false.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ryunigia · Oct 01, 2018 at 01:53 PM

Not sure if still is valid, but you gotta set the font to "unicode" in the import settings or atleast anything other than "dynamic". Atleast that's what i did wrong first

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
1

Answer by GGsparta · Nov 23, 2018 at 10:59 AM

Maybe your font is dynamic: this means you have to add characters in your font, and you should use Font.RequestCharactersInTexture! This happened to me with a single UnityEngine.UI.Text component, no matter the font. Reference: https://docs.unity3d.com/ScriptReference/Font.RequestCharactersInTexture.html

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 Spring_Heng · Jan 29 at 07:42 AM

the unity default font is dynamic // answer: Text t = GetComponent(); t.text = "abc";

      CharacterInfo info;
     font.RequestCharactersInTexture(t.text, t.fontSize,t.fontStyle); //add this
      if(t.font.GetCharacterInfo('a', out info, t.fontSize)){
          //Never returns true
      }
 
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

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

82 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

Related Questions

Is there a way to get the size of text? 1 Answer

Changing the size of a GUI label 1 Answer

Font issues - can someone explain the steps to make a custom font work properly? 0 Answers

Changing a GUILabel text SIZE 3 Answers

How to make on screen text resizable 0 Answers

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