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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prefix label not showing up in custom inspector

Discussion in 'Scripting' started by Aberdyne, Jun 9, 2016.

  1. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Hello, I was doing some editor scripting when I stumbled upon this strange bug when adding two or more prefix label in a row. Here is a code to reproduce the problem and the result I get.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(testscript))]
    6. public class testscriptInspector : Editor
    7. {
    8.     public override void OnInspectorGUI()
    9.     {
    10.         GUILayout.BeginHorizontal();
    11.         EditorGUILayout.PrefixLabel("First label");
    12.         GUILayout.Label("1");
    13.         GUILayout.EndHorizontal();
    14.  
    15.         GUILayout.BeginHorizontal();
    16.         EditorGUILayout.PrefixLabel("Second label");
    17.         GUILayout.Label("2");
    18.         GUILayout.EndHorizontal();
    19.  
    20.         GUILayout.BeginHorizontal();
    21.         EditorGUILayout.PrefixLabel("Third label");
    22.         GUILayout.Label("3");
    23.         GUILayout.EndHorizontal();
    24.     }
    25. }
    26.  
    27. public class testscript : MonoBehaviour {
    28.  
    29. }
    prefixlabel_bug.png

    Does anyone have already had this kind of behaviour? Is this a bug or am I doing something wrong? It does that from 5.0.0 to 5.4b17 as far as I can tell.
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    It appears to be a bug with PrefixLabel. You can however use GUILayout.TextField instead of GUILayout.Label as a workaround until it gets fixed.

    Example:
    Code (csharp):
    1. GUILayout.BeginHorizontal();
    2. EditorGUILayout.PrefixLabel("First label");
    3. GUILayout.TextField("1", "Label");
    4. GUILayout.EndHorizontal();
     
    Last edited: Jun 9, 2016
  3. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Thank you! You put me on the right track... explanation below...

    You previously stated (before you edited your answer) that EditorGUILayout might want another EditorGUILayout control after it. It makes sense. I always assumed this had no importance as documation states we can mix and match functions from GUI and GUILayout so I extrapolated that to EditorGUILayout and GUILayout... that's not the same thing obviously.

    Actually the reason I chose to use EditorGUILayout.PrefixLabel was to let Unity figure out the right size itself instead of having to correct the situation you see below when using GUILayout.Label.

    labelinsteadofprefixlabel.png

    So... I searched a bit more and found EditorGUILayout.LabelField which does seem to appreciate PrefixLabel's company more... (which is a better alternative to TextField by the way ;-))

    fieldlabelinsteadoflabel.png

    Code (CSharp):
    1. ...
    2. GUILayout.BeginHorizontal();
    3. EditorGUILayout.PrefixLabel("First label");
    4. EditorGUILayout.LabelField("1");
    5. GUILayout.EndHorizontal();
    6. ...
     
    Last edited: Jun 9, 2016
  4. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Erm... why not simply
    Code (CSharp):
    1. ...
    2. EditorGUILayout.LabelField("First label", "1");
    3. ...
    Anyway, I still wonder if the strange behavior showed above is a bug...
     
  5. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Quick! Download 5.4 and see if it still doesn't work. Maybe you can get entered into the sweepstakes. http://forum.unity3d.com/threads/unity-5-4-beta-sweepstakes.400686/