Search Unity

EditorGUI.DisabledScope PropertyField Not Expanding On Disable

Discussion in 'Scripting' started by Nigey, Sep 8, 2017.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi Guys,

    I've said the whole thing in the title. When I use any of the following:

    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    2. public class ReadOnlyDrawer : PropertyDrawer
    3. {
    4.     public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
    5.     {
    6.         using (new EditorGUI.DisabledScope(true))
    7.         {
    8.             EditorGUI.PropertyField(position, prop, true);
    9.         }
    10.     }
    11. }
    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    2. public class ReadOnlyDrawer : PropertyDrawer
    3. {
    4.     public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
    5.     {
    6.         GUI.enabled = false;
    7.         EditorGUI.PropertyField(position, prop, true);
    8.         GUI.enabled = true;
    9.     }
    10. }
    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    2. public class ReadOnlyDrawer : PropertyDrawer
    3. {
    4.     public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
    5.     {
    6.         EditorGUI.BeginDisabledGroup(true);
    7.         EditorGUI.PropertyField(position, prop, true);
    8.         EditorGUI.EndDisabledGroup();
    9.     }
    10. }
    I get this result:



    It works perfectly with fields that cover one single 'line', but with classes that can be expanded it causes that result.

    Any ideas?
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    bump.
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Did you override GetPropertyHeight to return the actual height of the expanded GUI?
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    I haven't yet. So I need to find that. That's great, thanks. I'll try it out and put either an answer, or more questions here.
     
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay, well that theory works, in theory. There doesn't seem to be many instances of people complaining about the same thing when it comes to creating PropertyDrawers of class variables that contain lists/other class vars ect. People tend to be just increasing the height by a flat number. Is there a way to find out the actual height of the property? As mine contains a class which contains several lists and another class, and I want this dynamic. Is there a clean way?
     
  6. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Coming back necro style. This works with GetPropertyHeight, but not with lists. Does anyone know about this?

    Example:

    Code (CSharp):
    1. //class one
    2. public class ReadOnlyAttribute : PropertyAttribute
    3. {
    4.  
    5. }
    6.  
    7. //class two
    8. [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    9. public class ReadOnlyDrawer : PropertyDrawer
    10. {
    11.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    12.     {
    13.         return EditorGUI.GetPropertyHeight(property, label, true);
    14.     }
    15.  
    16.     public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
    17.     {
    18.         using (new EditorGUI.DisabledScope(true))
    19.         {
    20.             EditorGUI.PropertyField(position, prop, true);
    21.         }
    22.     }
    23. }
    24.  
    25. //...another class...
    26.         [SerializeField, ReadOnly, Header(EditorConfig.RUNTIME_HEADER)]
    27.         private List<GameObject> m_Products = new List<GameObject>();
    28.      
    29.         [SerializeField, ReadOnly]
    30.         private ProductList list;
    31.  
    32.         [Serializable]
    33.         public class ProductList
    34.         {
    35.             [SerializeField]
    36.             public List<GameObject> m_Products = new List<GameObject>();
    37.         }
    38.  
    39.  
    ends up looking like this:



    Any ideas?

    Thanks!