unity3d - Unity C# , get text width / font character width -


i want width of text in unity using c# .

here trying .

int getwidthofmessage(string message) {     int totallength = 0;      font font = text.font; //text ui text     characterinfo characterinfo = new characterinfo();      char[] arr = message.tochararray();      foreach (char c in arr)     {         font.getcharacterinfo(c, out characterinfo, text.fontsize);         totallength += characterinfo.advance;     }      return totallength; } 

but font.getcharacterinfo(...) returns false , characterinfo.advance 0 character .

apart original question. following reason doing (expanding text box according text content).

you can use content size fitter component on text object , set horizontal fit property preferred size. , solve problem.

update:

add layout element component , set preferred width value 500 example , set horizontal overflow property of text wrap. work fo sure.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -