- Home /
How to make a line break in a GUI Label
Hey guys,
I was wondering, is it possible to make a simple line break in a GUI Label?
Because I have a fairly long string that is being written to the screen, and I would rather it to be split to a second line rather than stretch across the whole screen...
Thanks
-Grady
Answer by SilverTabby · Jul 02, 2011 at 04:18 AM
In java, javascript, C, C++, C#, and similar languages, you need to use an escape character to display certain characters such as new line or have " show up inside a string literal.
They are called escape characters because they "escape" from their normal useage.
All escape characters start with a \\ to tell the compiler that the next character should be interpreted differently than normal.
Here are some common ones:
\n New line \t Tab \v Vertical Tab \b Backspace \r Carriage return \f Formfeed \\ Backslash \' Single quotation mark \" Double quotation mark \d Octal \xd Hexadecimal \ud Unicode character
That's the "manual" way. Another way to make text automatically wrap around set wordWrap in your GUIStyle to true.
thanks, ive used that exact thing in javascript in web development so i should have known!!!!
thanks
-Grady
When I put this "\n" in a public variable, by keying in from the Inspector window, it doesn't work. It doesn't go to the next line, but ins$$anonymous$$d just display "\n" as it is.
The inspector shows only text. The inspector actually uses Unity's GUI system so text is shown as text. When you enter a \
(the escape character) it get excaped itself (\\
) so it's loosing it's meaning. You can type escape sequences in the inspector.
You can write the text in a text editor with a true new line, select and copy both lines and insert them in the variable. This way you get your line break. Another way is to create a custom inspector for your class and display a TextArea instead of a TextField which allows multiline editing. Unity now has the "Multiline" attribute which you can simply attach to a string field. So there's no need for a custom editor or property drawer anymore.
Another way could be to use System.Text.RegularExpressions.Regex.Unescape
to convert the escape sequences into it's true meaning inside your script when you want to use the string.
Regarding the part of typing the lines into a text editor then pasting them -- it doesn't work if written into and pasted from notepad.
Yes, it seems that doesn't work anymore. I don't know which version removed that feature. However the Multiline attribute is the better choice now anyways.
The inspector shows only text. The inspector actually uses Unity's GUI system so text is shown as text. When you enter a \` (the escape character) it get excaped itself (
\`) so it's loosing it's meaning. You can type escape sequences in the inspector.
You can write the text in a text editor with a true new line, select and copy both lines and insert them in the variable. This way you get your line break. Another way is to create a custom inspector for your class and display a TextArea instead of a TextField which allows multiline editing. Unity now has the "Multiline" attribute which you can simply attach to a string field. So there's no need for a custom editor or property drawer anymore.
Another way could be to use System.Text.RegularExpressions.Regex.Unescape
to convert the escape sequences into it's true meaning inside your script when you want to use the string.
Answer by SniperED007 · Nov 04, 2014 at 11:06 AM
or "line one" + Environment.NewLine + "line two"
For People Who Does Not Show 'Environment' Then Add 'using System;' At The Top Of The Code
Answer by ihsan07 · Feb 05, 2017 at 06:34 AM
There is another solution for this: string a = "String you want to get from somewhere with \n" a.Replace("\\n", "\n"); This way Unity gets "\n" as a line break. Not my solution but I can't remember where I have seen it.
Answer by DigitalCowboy · Oct 03, 2015 at 08:13 PM
If you are not getting what you want with \n and want to avoid referencing to System.Environment you can use \r\n e.g. MyText.text = "There will be a carriage return and newline at the end \r\n";