Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to know when the user is at the bottom of a editor scroll view?

Discussion in 'Immediate Mode GUI (IMGUI)' started by sebastian_unity67, Mar 26, 2024.

  1. sebastian_unity67

    sebastian_unity67

    Joined:
    May 9, 2023
    Posts:
    7
    Hi!

    I'm implementing a simple list of tags in a scroll view, however, I fetch those tags from a rest API that works with pagination.

    So I need to know when the user has reached the bottom of the current list of fetched tags to fetch the next page.

    This is my current code, but I don't know how to make the method `UserIsAtTheBottomOfScrollView`

    Code (CSharp):
    1. [CustomEditor(typeof(ObjectTags))]
    2. public class ObjectTagsEditor : Editor
    3.     {
    4.     ...
    5.  
    6.     private void OnEnable()
    7.     {
    8.         tagsProperty = serializedObject.FindProperty("tags");
    9.     }
    10.  
    11.     public override void OnInspectorGUI()
    12.     {
    13.         scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(300));
    14.         List<string> allTags = GetTags().Where(tag => TagContainsText(tag, searchTagText) && !IsTagAlredyPresent(tag)).ToList();
    15.         foreach (string tag in allTags)
    16.         {
    17.             if (GUILayout.Button(tag))
    18.             {
    19.                 AddTagIfNotPresent(tag);
    20.             }
    21.         }
    22.         EditorGUILayout.EndScrollView();
    23.         if (UserIsAtTheBottomOfScrollView())
    24.         {
    25.             FetchNextPage();
    26.         }
    27.     }
    28.  
    29.     private bool UserIsAtTheBottomOfScrollView()
    30.     {
    31.         // how do I know when the user is at the bottom of the scroll view?
    32.     }
    33.  
    34.     ...
    35.  
    36. }
    37.  
    Thank you in advance for your help.
     
  2. sebastian_unity67

    sebastian_unity67

    Joined:
    May 9, 2023
    Posts:
    7
    I ended up solving my problem this way:


    Code (CSharp):
    1. [CustomEditor(typeof(ObjectTags))]
    2. public class ObjectTagsEditor : Editor
    3.     {
    4.     ...
    5.     private int TAG_BUTTON_HEIGHT = 25;
    6.     private int MAX_BUTTONS_IN_SCROLL_VIEW = 9;
    7.     private int UNITY_BUTTON_MARGIN = 2; // This is a variable obtained by trial and error
    8.  
    9.     private void OnEnable()
    10.     {
    11.         tagsProperty = serializedObject.FindProperty("tags");
    12.     }
    13.  
    14.     public override void OnInspectorGUI()
    15.     {
    16.         int scrollHeight = MAX_BUTTONS_IN_SCROLL_VIEW * (TAG_BUTTON_HEIGHT + UNITY_BUTTON_MARGIN);
    17.         scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(scrollHeight));
    18.         List<string> allTags = GetAllTags().Where(tag => TagContainsText(tag, searchTagText) && !IsTagAlredyPresent(tag)).ToList();
    19.         foreach (string tag in allTags)
    20.         {
    21.             if (GUILayout.Button(tag, GUILayout.Height(TAG_BUTTON_HEIGHT)))
    22.             {
    23.                 AddTagIfNotPresent(tag);
    24.             }
    25.         }
    26.         EditorGUILayout.EndScrollView();
    27.         if(UserIsAtTheBottomOfScrollView(allTags.Count(), scrollPosition.y))
    28.         {
    29.             Debug.Log("User is at the bottom of the scroll view");
    30.         }
    31.     }
    32.  
    33.     private bool UserIsAtTheBottomOfScrollView(int numberOfTags, float scrollY)
    34.     {
    35.         int maxScroll =
    36.             (numberOfTags - MAX_BUTTONS_IN_SCROLL_VIEW) * (TAG_BUTTON_HEIGHT + UNITY_BUTTON_MARGIN) +
    37.             UNITY_BUTTON_MARGIN;
    38.         int bottomThresholdInPixels = 100;
    39.         if (scrollY >= maxScroll - bottomThresholdInPixels)
    40.         {
    41.             return true;
    42.         }
    43.         return false;
    44.     }
    45.  
    46.     ...
    47.  
    48. }
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,700
    So you know, this is nothing to do with code-editors & IDEs so I would ask that you please look at the available forums before posting.

    I'll move your post to the correct IMGUI forum.

    Thanks.
     
  4. sebastian_unity67

    sebastian_unity67

    Joined:
    May 9, 2023
    Posts:
    7
    Sorry for the mistake! Thank you!
     
    MelvMay likes this.