Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Custom Editor Wrap Label Text?

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Mar 11, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    We are writing an editor for the next version of Scene Doors, and we have hit a slight snag, we are trying to both set a default size for the window, and make the label(s) wrap if it is too small.
    Code is below:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. public class SceneDoorsWindow : EditorWindow {
    6.  
    7.     string Product = "Scene Doors";
    8.     string Version = "1.0";
    9.  
    10.     bool AboutFoldout = true;
    11.     bool VersionFoldout = true;
    12.  
    13.     [MenuItem("Scene Doors/About %&s")]
    14.     public static void Init() {
    15.         EditorWindow.GetWindow(typeof(SceneDoorsWindow), false, "About");
    16.     }
    17.  
    18.     void OnGUI()
    19.     {
    20.         GUILayout.Label ("About Scene Doors", EditorStyles.boldLabel);
    21.         //GUILayout.Label ("About", EditorStyles.foldout);
    22.         AboutFoldout = EditorGUILayout.Foldout(AboutFoldout, "About");
    23.         if (AboutFoldout)
    24.         {
    25.             GUILayout.Label ("Scene Doors - Created By Kyle Briggs,Labyrith and Don Briggs Ltd", EditorStyles.boldLabel);
    26.             GUILayout.Label ("Scene Doors ");
    27.             if( GUILayout.Button("labyrith.co.uk"))
    28.                 Application.OpenURL("http://www.labyrith.co.uk/");
    29.         }
    30.  
    31.         VersionFoldout = EditorGUILayout.Foldout(VersionFoldout, "Version");
    32.         if (VersionFoldout)
    33.         {
    34.             GUILayout.Label ("Product Name: " + Product, EditorStyles.boldLabel);
    35.             GUILayout.Label ("Version: " + Version, EditorStyles.boldLabel);
    36.             GUILayout.Label ("Changelog: ", EditorStyles.boldLabel);
    37.             GUILayout.Label ("Unity 5 Support Added");
    38.             GUILayout.Label ("Unity 5 GUI Support Added");
    39.             GUILayout.Label ("Unity 5 Support Added");
    40.             GUILayout.Label ("Increased Support for Mobile Devices");
    41.             GUILayout.Label ("C# Versions Introduced");
    42.             GUILayout.Label ("Re-optimised Code");
    43.             if( GUILayout.Button("Contact Us"))
    44.                 Application.OpenURL("mailto:kyle@kylebriggs.co.uk?subject=Scene Doors");
    45.         }
    46.  
    47.     }
    48. }
     
    ROBYER1 likes this.
  2. Tesar

    Tesar

    Joined:
    Feb 18, 2015
    Posts:
    8
    Not sure if this helps, since Im using EditorGUILayout.LabelField insted of GUILayout.label, and only in custom inspectors, not it custom windows. Following line of code makes my labels wrap nicely:
    EditorStyles.label.wordWrap = true;
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    CoughE likes this.
  4. edman3d

    edman3d

    Joined:
    Jun 1, 2016
    Posts:
    5
    worked perfect on 12/8/17 ty
     
    AlexRay and CoughE like this.
  5. 11tomi12

    11tomi12

    Joined:
    Feb 25, 2016
    Posts:
    17
    Doesn't work anymore. Using EditorGUILayout.LabelField aswell.
     
  6. FBWill

    FBWill

    Joined:
    May 15, 2020
    Posts:
    5
    I know this is an old thread, but in case someone is trying to figure out how to do this properly, all you need to do is set the style. e.g.

    GUIStyle myCustomStyle = new GUIStyle(GUI.skin.GetStyle("label"))
    {
    wordWrap = true
    };

    // Note that your rect has to have enough height to allow your text to wrap, otherwise you will not see the wrapped text.
    Rect labelRect = new Rect(0f, 0f, 50f, 36f);
    EditorGUI.LabelField(labelRect, "Some text", myCustomStyle);
     
  7. gshape

    gshape

    Joined:
    Aug 8, 2012
    Posts:
    103
    I'm using unity 2019.4
    Code (CSharp):
    1. GUILayout.Label(text, EditorStyles.wordWrappedLabel);
     
    EJSainz, Xtro, n2oukdnb and 7 others like this.
  8. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,444
    Thanks! was looking everywhere for this!
     
  9. Megalogue1

    Megalogue1

    Joined:
    Dec 17, 2020
    Posts:
    134
    This just helped me too!