- Home /
The question is answered, right answer was accepted
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;
}
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".
I always receive 1 (or 2) as advance property!? "iiii" doesn't have the same length of "mmmm"..what's wrong here!? :(
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.
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?