Search Unity

Custom editor attribute drawer draw header twice

Discussion in 'Getting Started' started by Mister_Pyxel, Jan 15, 2023.

  1. Mister_Pyxel

    Mister_Pyxel

    Joined:
    May 4, 2017
    Posts:
    47
    Hello,

    While making a simple custom read only attributes, I struggle on a problem : if I put a header and a readonly attribute on the same variable, then the header appear twice :
    upload_2023-1-15_17-15-21.png

    Here is the code I have for that :

    Simple custom attribute :
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace CustomAttributes
    5. {
    6.     [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
    7.     public class ReadOnlyAttribute : PropertyAttribute { }
    8. }
    Custom drawer for "ReadOnlyAttribute" :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using CustomAttributes;
    4.  
    5. [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    6. public class ReadOnlyPropertyDrawer : PropertyDrawer
    7. {
    8.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    9.     {
    10.         bool previousGUIState = GUI.enabled;
    11.         GUI.enabled = false;
    12.         EditorGUI.PropertyField(position, property, label);
    13.         GUI.enabled = previousGUIState;
    14.     }
    15. }
    And how it is used in script variables :
    Code (CSharp):
    1.     [Header ("Live Parameters Values")]
    2.     [SerializeField, ReadOnly] private Vector2 lookRotation;
    3.     [SerializeField, ReadOnly] private Vector3 moveVector;
    4.     [SerializeField, ReadOnly] private Vector2 movementInput;
    One peculiar thing is that, if I don't override the OnGUI and let the Drawer empty, the problem is still present.

    New drawer :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using CustomAttributes;
    4.  
    5. [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    6. public class ReadOnlyPropertyDrawer : PropertyDrawer
    7. {
    8.  
    9. }
    Then I have this in the inspector :
    upload_2023-1-15_17-14-52.png

    So it seem that the problem does not come from the override of the OnGUI in the drawer.

    It is like, by adding the ReadOnly attribute, the decorator drawer is called twice (I tryed multiple setups of attributes and it only happend if one or multiple decorator attributes are together with ReadOnly, adding or removing other attributes like SerializedField does not change anything).

    I couldn't find a comprehensive explanation of how the DecoratorDrawer and the PropertyDrawer are linked (if they are linked in any way).

    Any idea of what could cause this, something I did wrong here?

    PS : I did some test before posting this thread, I imported the asset "NaughtyAttributes" that also have a ReadOnly attribute to see how it was done.
    But what I saw is that : when the asset is imported, MY ReadOnly attribute (not the one from "NaughyAttributes", I was carrefull to verify that it was linking to my attribute) does not produce 2 headers, it works fine, but when I delete the "NaughtyAttributes" file, it goes back to 2 headers.
    I guess there is a general settings file coming with the asset that fix this problem...

    PPS : I'm on Unity latest Tech Stream version 2022.2.2f1

    EDIT : I tryed on latest LTS, version 2021.3.16f1, and the problem is not present
     
    Last edited: Jan 15, 2023
    Aldeminor likes this.