How to make a multi line textfield for editor?

So, I was going through the scripting reference in VS, and I came across textarea in the editor section, and I thought that would be very useful, but as I was trying to write it in C#, I have no clue of how to use it.

1 Like

Bump?

If you’re talking about displaying a textarea in the Inspector panel for a string variable of your component then you can create a script like this (this script must reside in a folder called “Editor” in your project):

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test))] // 'Test' here is your component class name
public class TestUI : Editor {
	
	public override void OnInspectorGUI ()
	{
		Test test = target as Test;
		EditorGUILayout.LabelField("My field");
		test.myField = EditorGUILayout.TextArea(test.myField);
	}
}

If you’re talking about using a textarea in the GUI on your game play then take a look at the reference page

2 Likes

Can you give a little more info on this? Like, how to set it up properly? Cause I have no clue of how to use this :S

This is the script

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(Note))]
public class Note : MonoBehaviour {
		
	//The Skin/Background of the GUIStyle
	 public GUISkin mycustomSkin;
	//The Text Of The Note
	public string Text = "Insert Your Text Here!";
	
	void Start () {
	
		//AutoSet the Name
		transform.name = "Note";
		
		//If there is no collider on the note add one
		if (collider == null) {
			
			Debug.LogError ("No Collider On Note " + name + ". Add A Collider!");
			
		}
		
	}

    void OnGUI()
    {
        Text = EditorGUI.TextArea(new Rect(10, 10, 200, 100), Text);


    }


		
}
1 Like

The link has a nice solution, I’ll rewrite it here just for people who tend to scroll to the bottom quickly ignoring links:

Add the TextArea attribute to your string field:

    [TextArea(3, 10)]
    public string messageText;

1st param is min number of lines to show, second param is number of lines from which you start showing scrollbar.
See doc on Unity - Scripting API: TextAreaAttribute.TextAreaAttribute

I found that the scrollbar appears a little before reaching max number of lines, but anyway.

It also works on Scriptable Objects.

12 Likes