0
\$\begingroup\$

I'm looking for an algorithm that can clip a text ( GUI Mesh text ) when it's length is over 20 characters. The problem is I have one background image(textured plane) (orthographic), where I want to put a Gui mesh text on it, it happens that sometimes the text is larger in length than the supposed background image, and it gets out of the background mesh boundary.

Is it possible to fit that gui mesh into the boundary of the background plane mesh ?

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Why not just pass a maximum of 20 characters in? \$\endgroup\$
    – Eejin
    Commented Jul 30, 2014 at 17:23
  • \$\begingroup\$ @Eejin tried, but the testers just put some hebrew, chineese charachters which the glyph is so big and it passed the boundary :S \$\endgroup\$
    – andreahmed
    Commented Jul 30, 2014 at 21:22
  • \$\begingroup\$ If you're using unicode, you mostly have to accept that certain strings might break your rendering. Even many browsers and text layout libraries don't get everything exactly right. The problems don't stop at rendering either. Things like surrogate pairs and combining characters make comparing and modifying unicode strings significantly less pleasant. How much you want to support unicode and other locales is up to you, but few games do it very well. \$\endgroup\$
    – bcrist
    Commented Jul 31, 2014 at 7:40

1 Answer 1

0
\$\begingroup\$

Now I am no unity expert (I opened it once two years ago), but I hope this helps.

First you would want to find the width of the string (in pixels). Something along these lines:

var textDimensions = GUI.skin.label.CalcSize(new GUIContent("text"));
//textDimensions.x contains the width in pixels.

Now you are going to want to see if that fits. If it doesn't then we are going to have to truncate the input string. (pseudo C++/C# mix)

var textDimensions = GUI.skin.label.CalcSize(new GUIContent(thestring));

while (textDimensions.x > somesize) {
    cut off one character
    textDimensions = GUI.skin.label.CalcSize(new GUIContent(truncated string));
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .