x


Word Wrapping GUIText

I hear it can't be done: "GUIText does not have word wrap, skins, or anything else from OnGUI. GUIText and GUITextures are separate objects, from the Unity 1.x days, though they are still useful for some things since OnGUI can be problematic depending on what you're doing. --Eric"

However, that was a couple years ago -- is it possible yet? I can't use GUILayout because I need to be able to put the text underneath other object.

Thanks!

more ▼

asked Mar 31, 2013 at 06:06 AM

avatar image

sfbaystudios
696 162 1963 220

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Answered:

I modified the code found here: http://forum.unity3d.com/threads/31351-GUIText-width-and-height

 function FormatString ( text : String ) { 
     words = text.Split(" "[0]); //Split the string into seperate words 
     result = ""; 
 
     for( var index = 0; index < words.length; index++)
     { 
         var word = words[index].Trim(); 
         if (index == 0) {
             result = words[0]; 
             block.text = result; 
         } 
 
         if (index > 0 ) { 
             result += " " + word; 
             block.text = result; 
         } 
         TextSize = block.GetScreenRect(); 
         if (TextSize.width > lineLength)
         { 
             //remover 
             result = result.Substring(0,result.Length-(word.Length)); 
             result += "\n" + word; 
             numberOfLines += 1;
             block.text = result;
         } 
     } 
 }

more ▼

answered Mar 31, 2013 at 06:27 AM

avatar image

sfbaystudios
696 162 1963 220

Would it be possible for you to add the variable declarations to this script? Mostly I'm just not sure what "block" is supposed to be.

Jan 22, 2014 at 04:08 PM reberk

@reberk, I'm not 100% sure, the scripts aren't on hand at the moment. However I think block refers to the "block of text". In the app it was a square/rectangular block of text, a quote, so it was likely a GUI object and the result was the correctly formatted text (with \n placed in where I needed line breaks).

I hope that helps! Of course, Unity is still working on a new, in theory much better, GUI system which should solve all these problems, I'd imagine. Last I heard, as of last night with a Unity presentation, it's still being worked on, so who knows the exact date.

Jan 23, 2014 at 03:18 AM sfbaystudios

Thanks! I'll give it a shot. It will be a good test of my deductive programming skills. And I've definitely got my fingers crossed that the new GUI system is coming sooner rather than later; anything that supersedes the weird combination of OnGUI, GUIObjects, and 3D Text Meshes that I'm relying on now will be a glorious boon.

Jan 23, 2014 at 09:32 PM reberk
(comments are locked)
10|3000 characters needed characters left

Not sure if anyone still needs this but I used the code from sfbaystudios, and modified it in C# so that it also takes in the line width. Heres my code:

     string wrapString(string msg, int width) {
     string[] words = msg.Split (" " [0]);
     string retVal = ""; //returning string 
     string NLstr = "";  //leftover string on new line

     for (int index = 0 ; index < words.Length ; index++ ) {
         string word = words[index].Trim();
         //if word exceeds width
         if (words[index].Length >= width+2) {
             string[] temp = new string[5];
             int i = 0;
             while (words[index].Length > width) { //word exceeds width, cut it at widrh
                 temp[i] = words[index].Substring(0,width) +"\n"; //cut the word at width
                 words[index] = words[index].Substring(width);     //keep remaining word
                 i++;
                 if (words[index].Length <= width) { //the balance is smaller than width
                     temp[i] = words[index];
                     NLstr = temp[i];
                 }
             }
             retVal += "\n";
             for (int x = 0 ; x < i+1 ; x++) { //loops through temp array
                 retVal = retVal+temp[x];
             }
         }
         else if (index == 0) {
             retVal = words[0];
             NLstr = retVal;
         }
         else if (index > 0) {
             if (NLstr.Length + words[index].Length <= width ) {
                 retVal = retVal+" "+words[index];
                 NLstr = NLstr+" "+words[index]; //add the current line length
             }
             else if (NLstr.Length + words[index].Length > width) {
                 retVal = retVal+ "\n" + words[index];
                 NLstr = words[index]; //reset the line length
                 print ("newline! at word "+ words[index]);
             }
         }
     }
     return retVal;
 }

EDIT: Updated code, now it checks for long words that exceed width and splits them. i.e. "BoooooooMz !!" with a width of 6 would give: "Booooo" "ooMz !" "!" in new lines each.

more ▼

answered Feb 12 at 04:57 AM

avatar image

NoXlenS
11 2

Sweet, I forgot about this :) I need to re-do this app with the new unity gui, I think all my problems would be instantly solved!

Feb 12 at 05:04 AM sfbaystudios
(comments are locked)
10|3000 characters needed characters left

I tried using GUIText too but it doesnt have word wrapping feature. The simplest thing to do is use GUIStyle and customize the font in the inspector. Just make sure to check Word Wrap in the inspector. This is my code:

 private var descriptionText : String;
 var descriptionStyle : GUIStyle;
 
 function Start()
 {
     descriptionText = "Multi-line text here";
 }
 
 function OnGUI()
 {
      GUI.Button (Rect (10,70, 100, 20), descriptionText, descriptionStyle);
 }
more ▼

answered Aug 15, 2014 at 10:18 PM

avatar image

thekingofclubs
20 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x23

asked: Mar 31, 2013 at 06:06 AM

Seen: 4019 times

Last Updated: Feb 12 at 06:48 AM