Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    927
    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:



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

    Peter77

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

    Nigey

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