Search Unity

How to make a multi line textfield for editor?

Discussion in 'Scripting' started by pspdude, Oct 7, 2012.

  1. pspdude

    pspdude

    Joined:
    Dec 17, 2011
    Posts:
    43
    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.
     
    SamFernGamer4k likes this.
  2. pspdude

    pspdude

    Joined:
    Dec 17, 2011
    Posts:
    43
    Bump?
     
  3. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    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):

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(Test))] // 'Test' here is your component class name
    6. public class TestUI : Editor {
    7.    
    8.     public override void OnInspectorGUI ()
    9.     {
    10.         Test test = target as Test;
    11.         EditorGUILayout.LabelField("My field");
    12.         test.myField = EditorGUILayout.TextArea(test.myField);
    13.     }
    14. }
    15.  
    If you're talking about using a textarea in the GUI on your game play then take a look at the reference page
     
    harleydk and Grooooo like this.
  4. pspdude

    pspdude

    Joined:
    Dec 17, 2011
    Posts:
    43
    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
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(Note))]
    6. public class Note : MonoBehaviour {
    7.        
    8.     //The Skin/Background of the GUIStyle
    9.      public GUISkin mycustomSkin;
    10.     //The Text Of The Note
    11.     public string Text = "Insert Your Text Here!";
    12.    
    13.     void Start () {
    14.    
    15.         //AutoSet the Name
    16.         transform.name = "Note";
    17.        
    18.         //If there is no collider on the note add one
    19.         if (collider == null) {
    20.            
    21.             Debug.LogError ("No Collider On Note " + name + ". Add A Collider!");
    22.            
    23.         }
    24.        
    25.     }
    26.  
    27.     void OnGUI()
    28.     {
    29.         Text = EditorGUI.TextArea(new Rect(10, 10, 200, 100), Text);
    30.  
    31.  
    32.     }
    33.  
    34.  
    35.        
    36. }
    37.  
     
  5. Deleted User

    Deleted User

    Guest

  6. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    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:

    Code (CSharp):
    1.  
    2.     [TextArea(3, 10)]
    3.     public string messageText;
    4.  
    1st param is min number of lines to show, second param is number of lines from which you start showing scrollbar.
    See doc on https://docs.unity3d.com/ScriptReference/TextAreaAttribute-ctor.html

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

    upload_2021-1-31_14-24-4.png

    It also works on Scriptable Objects.
     
    Taaakase, eggsamurai, xucian and 9 others like this.