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

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

[CustomEditor(typeof(ObjectTags))]
public class ObjectTagsEditor : Editor
    {
    ...

    private void OnEnable()
    {
        tagsProperty = serializedObject.FindProperty("tags");
    }

    public override void OnInspectorGUI()
    {
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(300));
        List<string> allTags = GetTags().Where(tag => TagContainsText(tag, searchTagText) && !IsTagAlredyPresent(tag)).ToList();
        foreach (string tag in allTags)
        {
            if (GUILayout.Button(tag))
            {
                AddTagIfNotPresent(tag);
            }
        }
        EditorGUILayout.EndScrollView();
        if (UserIsAtTheBottomOfScrollView())
        {
            FetchNextPage();
        }
    }

    private bool UserIsAtTheBottomOfScrollView()
    {
        // how do I know when the user is at the bottom of the scroll view?
    }

    ...

}

Thank you in advance for your help.

I ended up solving my problem this way:

[CustomEditor(typeof(ObjectTags))]
public class ObjectTagsEditor : Editor
    {
    ...
    private int TAG_BUTTON_HEIGHT = 25;
    private int MAX_BUTTONS_IN_SCROLL_VIEW = 9;
    private int UNITY_BUTTON_MARGIN = 2; // This is a variable obtained by trial and error

    private void OnEnable()
    {
        tagsProperty = serializedObject.FindProperty("tags");
    }

    public override void OnInspectorGUI()
    {
        int scrollHeight = MAX_BUTTONS_IN_SCROLL_VIEW * (TAG_BUTTON_HEIGHT + UNITY_BUTTON_MARGIN);
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(scrollHeight));
        List<string> allTags = GetAllTags().Where(tag => TagContainsText(tag, searchTagText) && !IsTagAlredyPresent(tag)).ToList();
        foreach (string tag in allTags)
        {
            if (GUILayout.Button(tag, GUILayout.Height(TAG_BUTTON_HEIGHT)))
            {
                AddTagIfNotPresent(tag);
            }
        }
        EditorGUILayout.EndScrollView();
        if(UserIsAtTheBottomOfScrollView(allTags.Count(), scrollPosition.y))
        {
            Debug.Log("User is at the bottom of the scroll view");
        }
    }

    private bool UserIsAtTheBottomOfScrollView(int numberOfTags, float scrollY)
    {
        int maxScroll =
            (numberOfTags - MAX_BUTTONS_IN_SCROLL_VIEW) * (TAG_BUTTON_HEIGHT + UNITY_BUTTON_MARGIN) +
            UNITY_BUTTON_MARGIN;
        int bottomThresholdInPixels = 100;
        if (scrollY >= maxScroll - bottomThresholdInPixels)
        {
            return true;
        }
        return false;
    }

    ...

}

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.

Sorry for the mistake! Thank you!

1 Like