Search Unity

GUI.BeginScrollView clipping and update delay issues

Discussion in 'Immediate Mode GUI (IMGUI)' started by derClef, Mar 13, 2019.

  1. derClef

    derClef

    Joined:
    Jun 20, 2017
    Posts:
    3
    This problem arises when using GUI.BeginScrollView in a PropertyDrawer for a custom class (tested using Unity 2018.3.2f1).
    I've read the documentation, but there is nothing there that suggests I've been doing something wrong.

    The problem:
    The content is not correctly clipped to the ScrollView's position-Rect.
    Instead, it is sometimes rendered relative to the Inspector window.
    When scrolling, the content vanishes or scrolls out of the ScrollView's position-Rect and takes about 1 to 2 seconds before being displayed correctly inside the ScrollView.

    Demonstration:
    Let's try rendering all the values of the KeyCode enum as labels inside a ScrollRect.



    Example Code:
    The example above was created using a MonoBehaviour, a SerializableClass and a PropertyDrawer for that class like so:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. #endif
    6.  
    7. public class KeyCodeBehaviour : MonoBehaviour
    8. {
    9.     public KeyCodeWrapper wrapper;
    10. }
    11.  
    12. // --------------------------------------------------------
    13.  
    14. [Serializable]
    15. public class KeyCodeWrapper
    16. { }
    17.  
    18. // --------------------------------------------------------
    19.  
    20. #if UNITY_EDITOR
    21. [CustomPropertyDrawer(typeof(KeyCodeWrapper))]
    22. public class KeyCodeWrapperDrawer : PropertyDrawer
    23. {
    24.     private Vector2 _scrollPos = Vector2.up;
    25.     private const float SCROLL_VIEW_HEIGHT = 150f;
    26.  
    27.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    28.     {
    29.         string[] keyCodes = Enum.GetNames(typeof(KeyCode));
    30.         position.height = SCROLL_VIEW_HEIGHT;
    31.  
    32.         float elementHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
    33.         Rect viewRect = new Rect(
    34.             x: 0, y: 0,
    35.             width: position.width,
    36.             height: keyCodes.Length * elementHeight);
    37.  
    38.         _scrollPos = GUI.BeginScrollView(position, _scrollPos, viewRect, false, true, GUIStyle.none, GUI.skin.verticalScrollbar);
    39.         {
    40.             Rect labelRect = new Rect(0, 0, viewRect.width, EditorGUIUtility.singleLineHeight);
    41.  
    42.             foreach(string code in keyCodes)
    43.             {
    44.                 EditorGUI.LabelField(labelRect, code);
    45.                 labelRect.y += elementHeight;
    46.             }
    47.         }
    48.         GUI.EndScrollView(true);
    49.     }
    50.  
    51.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    52.     {
    53.         return SCROLL_VIEW_HEIGHT;
    54.     }
    55. }
    56. #endif
    Research:
    I don't seem to be the first person to have encountered this issue.
    This thread seems to deal with the same problem, but no solution was reached and the bug report was shut down because and older Unity version was used.
    As far as I can tell, this is a bug and it still exists in 2018.3.

    Solution?
    So, what can be done as long as this bug persists?
    I want to be able to use a ScrollView for my PropertyDrawer and not a CustomInspector, so I can't use the auto-layouted version.
    Has someone written their own scroll view using GUI.BeginArea or GUI.BeginClip and gotten around this issue?
     
  2. SunSailor

    SunSailor

    Joined:
    Aug 22, 2012
    Posts:
    26
    Not sure, what your problem is exactly, but I can't confirm this bug, at least not for 2018.3.10f1. What I'm doing different is:
    - I don't specify the sliders explicitely, means, I only use the three parameter version if BeginScrollView
    - I don't reuse the rect struct (Which shouldn't make a difference, just saying)
    - I'm positioning the scroll view inside of position, not using position itself (As there are several more values in my case)
    - I'm encapsulating the whole property drawing within a pair of EditorGUI.BeginPropers/EndProperty

    Maybe you want to check for these to spot the difference?
     
  3. derClef

    derClef

    Joined:
    Jun 20, 2017
    Posts:
    3
    So, I came across this isse again today.
    @SunSailor Thanks for your suggestions, but sadly, non of these make a difference for me. I'm using Unity 2018.3.2f1 and haven't tested any newer versions yet.

    Even the example given in the Unity docs produces behaviour, which can can be seen in the GIF above, meaning the content of the Scroll View (in this case 4 buttons) are displayed outside of the Scroll View and only occasionally jump into their correct position.
     
    Last edited: May 10, 2019