Search Unity

Question Property Drawer,TextField and Height

Discussion in 'Editor & General Support' started by Gyratos, Mar 14, 2023.

  1. Gyratos

    Gyratos

    Joined:
    Oct 7, 2021
    Posts:
    1
    Greetings,
    I know many threads exist on the same subject. However none managed to help me fix my problem even though it's the same as everyone else. My custom property drawer overlaps with other properties and it's beyond annoying. I have overriden GetPropertyHeight but it's still go beyond what I want. Worse if I give it a hard height, the textfield will just extend itself until it overlaps again. Damn textfield x)

    I tried to get all children properties but it only detects one so height remains unchanged if I multiply it by the number of children...
    I don't know what to try anymore seriously. Even hardcoding space does not fix it. Ideally it should extend the field depending on the amount of text (wrapped) the user inputs in. It's supposed to be used in a behaviorTree Editor Window to allow the user to input a description of its nodes. The textfield can then be hidden/displayed with a toggle.
    Here is my property :
    Code (CSharp):
    1. [Serializable]
    2. public class NodeDescriptionProperty
    3. {
    4.     [SerializeField] bool displayDescription = false;
    5.     [SerializeField, TextArea] string description= "";
    6.  
    7.     public bool DisplayDescription => displayDescription;
    8.     public string Description => description;
    9.  
    10.     public NodeDescriptionProperty(bool _displayDescritpion) => displayDescription = _displayDescritpion;
    11. }
    Then my Drawer :
    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(NodeDescriptionProperty))]
    2. public class NodeDescriptionPropertyDrawer : PropertyDrawer
    3. {
    4.     [SerializeField] GUIStyle style;
    5.     [SerializeField] bool initStyleDone = false;
    6.     [SerializeField] SerializedProperty displayDesc = null;
    7.     [SerializeField] SerializedProperty desc = null;
    8.     [SerializeField] float height = 0;
    9.  
    10.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    11.     {
    12.         //base.OnGUI(position, property, label);
    13.         displayDesc = property.FindPropertyRelative("displayDescription");
    14.         desc = property.FindPropertyRelative("description");
    15.         if(!initStyleDone)
    16.             InitStyle();
    17.    
    18.         position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
    19.         EditorGUI.BeginProperty(position, label, property);
    20.         Rect _toggleRect = new Rect(position.x, position.y, position.width/5, position.height);
    21.         Rect _descRect = new Rect(position.x + _toggleRect.width, position.y, position.width/2, position.height*3);
    22.         EditorGUI.BeginChangeCheck();
    23.         displayDesc.boolValue = EditorGUI.Toggle(_toggleRect, displayDesc.boolValue);
    24.         if (displayDesc.boolValue)
    25.             desc.stringValue = EditorGUI.TextField(_descRect, desc.stringValue, style);
    26.         EditorGUI.EndChangeCheck();          
    27.         EditorGUI.EndProperty();
    28.     }
    29.  
    30.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    31.     {
    32.  
    33.         //return base.GetPropertyHeight(property, label) + height;
    34.         SerializedProperty _desc = property.FindPropertyRelative("description");
    35.         float _totalHeight = EditorGUI.GetPropertyHeight(_desc, label);
    36.         return _totalHeight;
    37.  
    38.  
    39.     }
    40.  
    41.     void InitStyle()
    42.     {
    43.         initStyleDone = true;
    44.         style = new GUIStyle(GUI.skin.textField);
    45.         style.fontSize = 11;
    46.         style.fontStyle = FontStyle.Bold;
    47.         style.wordWrap = true;
    48.         style.clipping = TextClipping.Clip;
    49.    
    50.     }
     

    Attached Files: