• 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
14
Question by Grady · Jul 02, 2011 at 02:24 AM · guilinelabelsplitbreak

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

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

7 Replies

· Add your reply
  • Sort: 
avatar image
53
Best Answer

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
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 Bunny83 · Jul 02, 2011 at 05:14 AM 3
Share

That's the "manual" way. Another way to make text automatically wrap around set wordWrap in your GUIStyle to true.

avatar image Grady · Jul 02, 2011 at 05:24 AM 0
Share

thanks, ive used that exact thing in javascript in web development so i should have known!!!!

thanks

-Grady

avatar image ferraribeng · May 18, 2012 at 06:56 AM 0
Share

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.

avatar image Bunny83 · May 18, 2012 at 07:07 AM 3
Share

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.

avatar image SilverFang180882 Bunny83 · May 18 at 08:31 AM 0
Share

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.

avatar image Bunny83 SilverFang180882 · May 18 at 08:53 AM 0
Share

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.

avatar image Bunny83 · Nov 10, 2021 at 10:07 AM 0
Share

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.

avatar image
9

Answer by SniperED007 · Nov 04, 2014 at 11:06 AM

or "line one" + Environment.NewLine + "line two"

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 LukasTanner · Aug 08, 2018 at 05:22 PM 0
Share

For People Who Does Not Show 'Environment' Then Add 'using System;' At The Top Of The Code

avatar image
5

Answer by _Petroz · Jul 02, 2011 at 02:38 AM

Then new line character is "\n".

"line one\\nline two"

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
5

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.

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
4

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";

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
  • 1
  • 2
  • ›

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

20 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Break Line to GUI.Label 2 Answers

How Do I Center A GUI Label? 5 Answers

GUI.Label positioning for many device resolutions 1 Answer

network GUI not working 1 Answer

GUI, making main page lead into an instructions page 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