Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice

EditorGUI.DisabledScope PropertyField Not Expanding On Disable

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

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    786
    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.     }
    9. }
    I get this result:

    [​IMG]

    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:
    786
    bump.
     
  3. Peter77

    Peter77

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

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    786
    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:
    786
    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:
    786
    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:

    [​IMG]

    Any ideas?

    Thanks!