Showing a textarea field for a string variable in inspector?

When I declare a public string variable in my script, the Unity inspector shows a single-line textfield where I can edit text for the variable. Since the string is intended to store a long message, is it possible for me to get the Unity inspector to show up as a multi-line textarea field for the variable instead?

4 Answers

4

You can use the TextArea attribute before the property you want to display:

[TextArea(3,10)]
public string myText = "This text will appear in a text area that automatically expands";

This really ought to be the accepted answer... it's simple, it works, and nobody has to get nailed to anything.

This worked perfectly for me. I agree, it should be the accepted answer.

Agreed. This is by far the preferable solution in my opinion.

Unlike [Multiline] this automatically wraps long lines. It also displays the textarea beneath the label, full width, rather than to the right of the label.

Yes, you're right ^^ Thanks for the hint, i fixed it.

For C#;

[Multiline]
	public string Note = "this is multiline string 

as you can see…";

Only works with linebreaks in the string - will not soft-wrap a string that is too long.

@plasticYoda Note that to my knowledge there are no solutions to making it auto-wrap without writing some robust code that re-calculates wrapping for you automatically.

You can make a custom inspector.

Use custom editor:

Example Script

public class TextAreaScript : MonoBehaviour {

	public string longString;
}

Custom Editor Script (Placed in Assets/Editor)

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(TextAreaScript)), CanEditMultipleObjects]
public class TextAreaEditor : Editor {
	
	public SerializedProperty longStringProp;

	void OnEnable () {
        longStringProp = serializedObject.FindProperty ("longString");
    }
	
	public override void OnInspectorGUI() {
		serializedObject.Update ();
		longStringProp.stringValue = EditorGUILayout.TextArea( longStringProp.stringValue, GUILayout.MaxHeight(75) );
		serializedObject.ApplyModifiedProperties ();
	}
}

I just gave this a test run in Unity 4.5. Maybe I'm missing a step, but it doesn't seem to be working.

Works for me. Did you copy the code exactly? and did you place the second script in a folder named "Editor"?

This method doesn't seem to have word-wrapping, like the method "andsee" came with - which is also much cleaner and simpler to use (one line over the desired string): [TextArea(3, 10)]

This SHOULD NOT be tagged as the best answer. The one right below this one should. [TextArea(3,10)] annotation in front of the string, done.