- Home /
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?
Answer by Chronos-L · Mar 26, 2013 at 05:01 AM
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. $$anonymous$$aybe 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.
Actually when the question was asked the $$anonymous$$ultiline / TextArea attribute and the corresponding built in property drawer did not exist yet. So this answer was the answer back then and still answers the question that the OP had asked back then. $$anonymous$$eep in $$anonymous$$d that it's the duty of the OP to pick an answer and he did back then. This answer is still valid, even there might be a simpler / built-in solution now.
You should not bump such ancient and you should save your downvotes for actual wrong information. Creating a custom inspector (or nowadays a custom property drawer) actually gives you much more possibilities than the built-in textarea property drawer.
Answer by andsee · Aug 18, 2014 at 10:53 AM
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";
http://docs.unity3d.com/ScriptReference/TextAreaAttribute.html
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.
Answer by menelaus · Apr 22, 2014 at 01:42 PM
For C#;
[Multiline]
public string Note = "this is multiline string \n 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.