- Home /
Making newline ( \n ) in inspector
I am making some localized tips to be displayed when my player enters an area. These tips are displayed over the characters head.
I have a gameObject with an exposed string, so that I can place tips around my scene, and enter the message I want to have displayed. The string is then passed forward to a TextMesh when the character collides with the tip.
The problem I am encountering is entering new line ( \n ) into the string in the inspector. The escape character is shown in inspector but is left out in game ( the '\' is gone, but not the 'n'). I have tried entering \n by using alt + enter ( as suggested in the reference manual ). But it doesn't seem to work.
Has anyone managed to input newlines through the inspector? (how the hell did you do that :o?)
Answer by duck · May 07, 2010 at 01:54 PM
This appears to be a bug in the windows version of Unity. You can't manually type a return character into an inspector text field, and \n doesn't work.
The only workaround for this is to copy and paste a return character (for example, from notepad).
I attempted to type in a Ctrl-J... and poof went the Editor... lol Do not do that! (3.1) Why did they not resolve that with version 3 ?!? Cut & Pasting text from Notepad seems to work, but painful to format to fit the scene.
That worked a treat. Thanks for that.
Just to clarify for others, there isnt any phisical character that appears in the inspector. . . simply copy and paste text (including blank lines) from a text field.
Since you may have the $$anonymous$$ono editor up, just cut and paste 2 partial lines from any of your scripts onto the location in the line you want to split
it solved the bug that \r\n(it kinda works but it shows in the text ; "some text \r\n" ) i had been dealing with.
Answer by michielbos · Feb 16, 2013 at 10:34 PM
Not sure if this question is still alive, but I found another workaround to put a newline into a string in the inspector:
function Start () {
stringname = stringname.Replace("NEWLINE","\n");
}
Just place NEWLINE in your string, or replace the NEWLINE in the script with something else.
Thanks for the suggestion! $$anonymous$$aybe even more intuitive, use stringname.Replace("\\n", "\n");
.
This will replace literal \\n with a new line (useful if you're reading from X$$anonymous$$L for example).
This is the cleanest variation so far, thanks!
Answer by Molix · May 07, 2010 at 03:38 PM
You can also create a custom inspector that uses a TextArea, rather than a TextField, for the string in question.
I am on a tight deadline, but I'll look in to this more! thanks
Answer by fffMalzbier · Jun 11, 2012 at 07:03 PM
Picked up Molix idea :
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(TextMesh))]
public class TextEditor : Editor {
public override void OnInspectorGUI () {
this.DrawDefaultInspector();
TextMesh current_target = target as TextMesh;
EditorGUILayout.LabelField(" Text:", "");
current_target.text = EditorGUILayout.TextArea( current_target.text,GUILayout.MaxHeight(500f));
}
}
Answer by Developer386DX · Mar 25, 2015 at 05:28 PM
In windows newline is "\r\n" so -> "hello \r\n new line"
the empty space matters " ". Or it did in my problem; unity 5.3 my windows (old 7) "\r\n" did not recognized until i added the space such as " \r\n".