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 /
This question was closed Mar 15, 2015 at 05:27 PM by Glurth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Glurth · Jan 10, 2015 at 06:43 PM · fonttextmeshscaling

How do I scale the results of GetCharacterInfo?

So I've got my textMesh code already to the point where, if I click on it, I can compute the local horizontal distance, from the left-middle of the texture to the mouse click location: This yields an offset from the leftside, in the model's local scale. (I use a RayCast hit location, and InverseTransformPoint for this: works well.)

Now I'm trying to figure out which letter was actually clicked on. I've been banging my head against the wall using "GetCharacterInfo(char, out CharacterInfo)". I just cannot seems to get the WIDTH value returned in the CharaterInfo to scale correctly to the textTexture's local scale.

I've tried using the transform.localscale, the characterSize, the FontSize, and various combinations of each to scale the width. I've gotten CLOSE in some cases, but it never works perfectly.

The final goal: Since I may be going about it the wrong way, the main goal is to put a cube on the screen, that completely (but just barely) covers the character clicked on. AND given a starting and ending character index into the text string displayed, draw the cube to cover all the characters between the start and end index(inclusive). In the end, it should look like a cursor, or highlighted text. In my tests, I tried this with the cube is a CHILD of the same parent as the TextTexture, AND as a child of the text texture itself.

Some additional info:

I use the following function to find the Character clicked on (I pass the horizontal value computed above as the width parameter)- this part works OK, unless I'm using a fixed-width font like CourierNew. In this case calls to GetCharacterInfo() fail (I have no idea what do in this case: hard-code a width, I guess). But even for normal dynamic-width fonts, the width parameter that I pass in here, needs to be scaled somehow, and this is whats been failing for me.

 private int CharIndexAtWidth(string str,float width)
     {
         CharacterInfo fontCharInfo=new CharacterInfo();
         float width_counter = 0;
         for(int i=0;i<str.Length;i++)
         {
             if(font.GetCharacterInfo(str[i],out fontCharInfo))
                 width_counter+=fontCharInfo.width;
         //    Debug.Log ("fontCharInfo.width: "+fontCharInfo.width.ToString());
             if(width_counter>width)
                  return i;
         }
         return -1;
     }
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Glurth · Jan 11, 2015 at 03:20 PM

After excluding charSize as one of the variables to consider (by simply NOT changing it for my tests), I have found the following: Rather than use GetCharacterInfo(char,CharInfo), I am now using GetCharacterInfo(char,CharInfo, float FONTSIZE). Using the fontSize in this function is all we need to do to scale for FontSize. Using the InverseTransformPoint, actually introduces the localScale from the very beginning, so no additional scaling is needed for this variable either! Odd that a scale of 1.0f had me so confused, but that might be because it WASN'T actually 1.0f! Looking at my debug logs and debug drawnlines saw I was off by a pretty consistent scale: a factor of 10.0f in fact. When I used THIS for my scale: everything fell into place.

I think finding that other version of GetCharacterInfo, that takes fontSize as a parameter, is what really helped to make this work.

Testing shows that chacterSize scaling is simply a normal multiplication of the GetCharacterInfo results.

Also note, in unity 5, the correct field to use is now "advance", rather than the depreciated "width".

Comment
Add comment · Show 10 · 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 Flag74 · May 15, 2018 at 12:13 PM 0
Share

I always receive 1 (or 2) as advance property!? "iiii" doesn't have the same length of "mmmm"..what's wrong here!? :(

avatar image Glurth Flag74 · May 15, 2018 at 04:26 PM 0
Share

I suspect this is due to the fontSize value you pass to GetCharInfo. If you use 0, it uses a default size of 14 (I think), but if you use say... 1 or 2, this is REALLY tiny, and could very well result in characters that are no more than 1 or 2 pixels wide! Do you get the same results if you pass that function a ZERO, or say.. 20 for fontSize? Note: the result value is an interger, so even if the advance of a given character SHOULD be say.. 0.7f, it will give you 1.

avatar image Flag74 Glurth · May 15, 2018 at 04:35 PM 0
Share

You are right! ..10 $$anonymous$$utes after posting this comment I've found that I made some confusion about font size and text font size. Do you know how to calculate line height?

Show more comments

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Jagged and pixelated fonts in text mesh 3d (iphone) 4 Answers

How to dynamically change a TextMeshProUGUI font in runtime ? 0 Answers

How to Get a Char from his name ? 1 Answer

Changing the font of a textmesh? 1 Answer

Font scale factor of 0.1 when computing TextMesh width 0 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