Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
6
Question by wlad_s · Dec 01, 2011 at 03:08 PM · meshtexttextmeshwrapwrapping

Wrapping a Textmesh text

Hi,

How do I wrap a Textmesh text? For example, I want it not to be longer than 3 meters. Than I have a script which picks some text and I need it to automatically break it in more lines if it's longer than 3 meters.

Thanx!

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

5 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by jindave · Mar 28, 2012 at 06:24 PM

Hi here is my code:

 // Wrap text by line height
 private string ResolveTextSize(string input, int lineLength){
 
   // Split string by char " "         
   string[] words = input.Split(" "[0]);
 
   // Prepare result
   string result = "";
 
   // Temp line string
   string line = "";
 
   // for each all words        
   foreach(string s in words){
     // Append current word into line
     string temp = line + " " + s;
 
     // If line length is bigger than lineLength
     if(temp.Length > lineLength){
 
       // Append current line into result
       result += line + "\n";
       // Remain word append into new line
       line = s;
     }
     // Append current word into current line
     else {
       line = temp;
     }
   }
 
    // Append last line into result        
    result += line;
 
    // Remove first " " char
    return result.Substring(1,result.Length-1);
 }
Comment
Add comment · Show 2 · 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 Zaapp · May 24, 2012 at 04:32 PM 0
Share

This works well for unispace fonts, but does anyone have a similar solution for other fonts? I'm thinking about trying to use the bounds of the mesh and maybe splitting each line into a separate mesh at runtime, assu$$anonymous$$g that's not too expensive.

avatar image srmojuze · Apr 08, 2013 at 08:31 PM 0
Share

Excellent, thanks a lot.

avatar image
7

Answer by thienhaflash · Jun 18, 2012 at 04:54 PM

Basically there are no way to get an abitrary TextMesh's size without rendering it. I wrap the code that render and cache the characters dynamically to improve performance, so each character will be render only once when it is first used. One good thing is this approach will provide very accuracy result (100% correct). You can use my class below :

https://dl.dropbox.com/u/10796188/wp/unity/TextSize.cs

I don't have enough time to make a sample package so i put a sample inside the file itself, hope that it's clear enough for you (a bit too long to post the whole file here).

You can use the above method to get the size of any string, it's really cheap because all those chars' size are already cached.

Comment
Add comment · Show 7 · 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 nargus · Dec 13, 2012 at 09:55 PM 0
Share

That's...ingenius! I'm adding yoru code to my common stock! :D

avatar image Essential · Apr 25, 2013 at 09:13 PM 0
Share

Thanks for posting your script. Works nicely!

avatar image masha · Dec 16, 2013 at 02:54 PM 0
Share

This was incredibly useful, thanks a lot =] Added two functions to help wrap text.

avatar image Alturis2 · May 04, 2014 at 12:23 PM 0
Share

First of all I cannot believe there is not built in less hacky way to do this as part of the Text$$anonymous$$esh features.

@thienhaflash I plugged in your script and it seems to work reasonably well thanks.

However I am running into problems with consistency and I think it has to do with the fact that this only works if the Text$$anonymous$$esh is not scaled or rotated. Oddly, though, the only change in scale or rotation that I use should not be affecting the x axis size in the world.

avatar image ashjack · Jul 24, 2016 at 04:24 PM 5
Share

Sorry for necro-posting, but if I needed this answer, then others will too. I found what I believe to be the file that is linked above: https://github.com/$$anonymous$$ystfit/TeslaRift/blob/master/TeslaRift-Unity/Assets/Scripts/Visuals/TextSize.cs

Please correct me if this is the incorrect file.

Show more comments
avatar image
6
Wiki

Answer by masha · Dec 16, 2013 at 03:00 PM

Used thienhaflash's solution which works great and added the following two functions to the class, in order to make wrapping even easier.

 public void FitToWidth(float wantedWidth) {
     
     if(width <= wantedWidth) return;
     
     string oldText = textMesh.text;
     textMesh.text = "";
     
     string[] lines = oldText.Split('\n');
     
     foreach(string line in lines){
         textMesh.text += wrapLine(line, wantedWidth);
         textMesh.text += "\n";
     }
 }

 private string wrapLine(string s, float w)
 {
     // need to check if smaller than maximum character length, really...
     if(w == 0 || s.Length <= 0) return s;
     
     char c;
     char[] charList = s.ToCharArray();
     
     float charWidth = 0;
     float wordWidth = 0;
     float currentWidth = 0;
     
     string word = "";
     string newText = "";
     string oldText = textMesh.text;
     
     for (int i=0; i<charList.Length; i++){
         c = charList[i];
             
         if (dict.ContainsKey(c)){
             charWidth = (float)dict[c];
         } else {
             textMesh.text = ""+c;
             charWidth = renderer.bounds.size.x;
             dict.Add(c, charWidth);
             //here check if max char length
         }
     
         if(c == ' ' || i == charList.Length - 1){
             if(c != ' '){
                 word += c.ToString();
                 wordWidth += charWidth;
             }
             
             if(currentWidth + wordWidth < w){
                 currentWidth += wordWidth;
                 newText += word;
             } else {
                 currentWidth = wordWidth;
                 newText += word.Replace(" ", "\n");
             }
             
             word = "";
             wordWidth = 0;
         } 
         
         word += c.ToString();
         wordWidth += charWidth;
     }
     
     textMesh.text = oldText;
     return newText;
 }
Comment
Add comment · Show 1 · 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 ramonthepsycho · Sep 23, 2016 at 09:27 AM 0
Share

Does it work when the text is from database?

avatar image
0
Wiki

Answer by AlfredDuler · Oct 08, 2013 at 07:58 AM

What happen if you're importing formated text from let's say a database, and there are already some line break in the text?

You got unwanted line break because de nbr of characs doesn't increase with line break.

This was my problem, so I used the function gave by jindave and came with this one.

note: I'm not a proffessional dev so it might be ugly code, but I think it could help some novices.

It's obviously in Javascript:

 function ResolveTextSize( input : String, lineLength : int): String
 {
     var linesRetrieved : String[]  = input.Split('\n'[0]);
            
     var  result : String= "";
     
     for ( var actualLine : String in linesRetrieved )
     {
         var words : String[] = actualLine.Split(" "[0]);
         
         var  line : String = "";
         
         for(var s : String in words)
         {
             
             var  temp  : String = line + " " + s;
             
             if(temp.Length > lineLength)
             {
                 result += line + "\n ";
                 line = s;
             }
             else 
             {
                 line = temp;
             }
         }
 
         result += line + "\n";
         
     }
 
     return result;
 }
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 Sun-Pengfei · Mar 02, 2017 at 06:40 AM

 TextGenerator textGen = new TextGenerator();
 TextGenerationSettings generationSettings = textCompo.GetGenerationSettings(textCompo.rectTransform.rect.size);
 float width = textGen.GetPreferredWidth(text, generationSettings);
 if (width > preferedWidthMax)
 {
     layoutElement.preferredWidth = preferedWidthMax;
 }

This is the most elegant way I've found to this problem. Note that the text's parent has a content size fitter and a layout group components, and the text itself has a layout element.

Check the other related question:

https://forum.unity3d.com/threads/wrapping-child-text-with-content-size-fitter.315630/

http://answers.unity3d.com/questions/921726/how-to-get-the-size-of-a-unityengineuitext-for-whi.html

Comment
Add comment · Show 1 · 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 idbrii · Apr 30, 2019 at 05:30 PM 0
Share

Your answer uses textCompo.rectTransform, but Text$$anonymous$$esh doesn't have a rectTransform.

The linked answers are for UnityEngine.UI.Text, but the question was for UnityEngine.Text$$anonymous$$esh.

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.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

How to calculate a TextMesh width without rendering it ? 2 Answers

Wrap text textmesh 0 Answers

I can't change the text mesh 1 Answer

Unity crashes at writing TextMesh? 0 Answers

Text Mesh to Mesh ? 2 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