Search Unity

How GUIContent label is generated

Discussion in 'Immediate Mode GUI (IMGUI)' started by MialeeMialee, Mar 14, 2020.

  1. MialeeMialee

    MialeeMialee

    Joined:
    Feb 12, 2019
    Posts:
    8
    I have a custom property drawer and I override the OnGui method with
    Code (CSharp):
    1. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2. {
    3. //do things
    4. }
    My question is how the label is generated when this function is called?
    And can I override it? (I know I can change the label inside the OnGui, but this is not what I am asking.)

    For example "public override float GetPropertyHeight" can be used to change the height of the Rect transform)
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    You can pass a custom label to the EditorGUI.PropertyField method.
    Code (CSharp):
    1. var customLabel = new GUIContent("Custom Label");
    2. EditorGUI.PropertyField(position, property, customLabel);
     
  3. MialeeMialee

    MialeeMialee

    Joined:
    Feb 12, 2019
    Posts:
    8
    As I mentioned, I know I can create a new label but this is not what I am asking.

    Again on a given

    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(myClass))]
    2. public class InspectorOfMyclass : PropertyDrawer
    3. {
    4.  
    5.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    6.     {
    7.           // do things here
    8.     }
    9.  
    10. }
    The OnGui method is called automatically when inspecting this class
    My question is: How/where unity generates the "label"
     
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Oh, I thought you just wanted to override the label shown in the inspector, my mistake.

    I don't think you can actually change the display name of a property within Unity's internal systems without source code access. It is likely generated somewhere in Unity's native code based on the name of the property/field. There's a method called ObjectNames.NicifyVariableName that can be used to generate a display name from a variable name, so that could also be used internally.