Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. 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
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Inspector custom PropertyDrawers don't support BeginScrollView

Discussion in 'Extensions & OnGUI' started by gfoot, Sep 9, 2013.

  1. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    548
    I've been trying to use scrollbars in PropertyDrawers, and they seem to be broken. There are also clipping problems with selected text in TextArea controls, at least when used in PropertyDrawers.

    TestComponent.cs defines two data types to demonstrate - one whose PropertyDrawer uses a TextArea and one whose PropertyDrawer uses a ScrollView - and a component with members of both types for demonstration:

    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class TextAreaData {}
    4.  
    5. [System.Serializable]
    6. public class ScrollViewData {}
    7.  
    8. public class TestComponent : UnityEngine.MonoBehaviour
    9. {
    10.     public ScrollViewData scrollViewData;
    11.     public TextAreaData textAreaData;
    12. }
    13.  
    Then we have an editor script providing the PropertyDrawers:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomPropertyDrawer(typeof(ScrollViewData))]
    6. public class ScrollViewPropertyDrawer : PropertyDrawer
    7. {
    8.     public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
    9.     {
    10.         return 100;
    11.     }
    12.    
    13.     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
    14.     {
    15.         var viewRect = new Rect(0, 0, 600, 600);
    16.        
    17.         _scrollPosition = GUI.BeginScrollView(position, _scrollPosition, viewRect);
    18.         GUI.Box(viewRect, "hello world");
    19.         GUI.EndScrollView();
    20.     }
    21.    
    22.     private Vector3 _scrollPosition;
    23. }
    24.  
    25. [CustomPropertyDrawer(typeof(TextAreaData))]
    26. public class TextAreaPropertyDrawer : PropertyDrawer
    27. {
    28.     public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
    29.     {
    30.         return 100;
    31.     }
    32.    
    33.     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
    34.     {
    35.         _text = EditorGUI.TextArea(position, _text);
    36.     }
    37.    
    38.     private string _text;
    39. }
    40.  
    Apply TestComponent to any GameObject and observe the results:

    1) Note that the box is not shown correctly in the scroll view - it seems to be drawn relative to the first section in the Inspector instead. Scrolling the scroll bar moves it around in an odd way.
    2) Enter plenty of lines of text in the TextArea, then drag to select them causing the area to scroll - the selection highlight is still rendered for lines that lie outside the clipping area of the control.

    Have I done something wrong, or is this a bug? For now I can get by without scrolling, but it would be nice if it could work better. Scrolling works fine for me in custom Inspector OnGUI methods, just not in PropertyDrawers. I wondered if it was related to the fact that GUILayout doesn't work either.
     
  2. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    613
    Try nesting your property drawer in a custom editor. i.e. write a custom editor for your container class.

    Code (csharp):
    1.  
    2. public Test : MB
    3. {
    4.    public DrawMe instance;
    5. }
    6.  
    7. [CustomEditor(typeof(Test))]
    8. public class TestEditor : Editor { }
    9.  
    10. [CustomPropertyDrawer(typeof(DrawMe))]
    11. public class DrawMeDrawer : PropertyDrawer {...}
    12.  
    That would actually let GUILayout stuff work well in the property drawer's OnGUI - so it might as well fix your issues.
     
  3. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    34
    This is still going. Is there is bug reported about that?
     
  4. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    34