Search Unity

ScrollRect - Velocity “sky rockets” during scrolling

Discussion in '2D' started by genaray, Sep 8, 2019.

  1. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    140
    Good evening,

    im working on a leaderboard for my little mobile game. This leaderboard should fetch scores from my server, this requires a modified scroll rect aswell as recycling elements.

    Recycling works fine so far, except one detail... the velocity while scrolling... when the user scrolls trough the list in little "steps / intervalls", my scroll rects repositions elements just fine and the velocity dont cause any problems.

    But once the user drags the list for a longer duration while existing elements are getting recycled and repositioned, the velocity skyrockets... this causes the list to jump to the bottom everytime it recycled existing elements, which loads new scores from the server like a machine gun....

    Heres a link to a visual representation of the velocity skyrocket i encountered : Velocity sky rockets, causing the list to load elements too fast

    I tested this strange behaviour with a normal implementation of a scroll rect... no problems here... but once we actually relocate those elements, this strange behaviour starts to occur.

    Heres my implementation of the scroll rect modifier script :

    Code (CSharp):
    1. public ScrollRect scrollRect;
    2. public RectTransform content;
    3.  
    4. /// Gets called once we received packets from the server.
    5. protected override void received(ISFSObject packet) {
    6.  
    7.        float totalsize = 0;
    8.  
    9.        ISFSArray scores = packet.GetSFSArray(validMessage());
    10.        for (int index = 0; index < scores.Count; index++) {
    11.  
    12.            /// Extracting stuff from packet and initialse the element.
    13.            UIElement element = initialiseElement(ID, username, scoreAmount);
    14.  
    15.            /// Calculating the size for each entry, for getting a total size.
    16.            RectTransform rectTransform = element.GetComponent<RectTransform>();
    17.            totalsize += rectTransform.rect.height;
    18.        }
    19.  
    20.        /// When there more than 50 entrys in the list, start to recycle the top ones.
    21.        if (elements.Count > maximum) {
    22.  
    23.            Canvas.ForceUpdateCanvases();
    24.  
    25.            /// Content is the gameobject-parent of the scroll view elements.
    26.            /// Adjusting the y position of the content, otherwhise it would either be pushed up or down, this works fine.
    27.            content.offsetMax = new Vector2(0, content.offsetMax.y - totalsize);
    28.        }
    29.    }
    30.  
    31.  
    32.    ///////////////////////
    33.    ///  Main methods
    34.    ///////////////////////
    35.  
    36.  
    37.    /// <summary>
    38.    /// Creates a leaderboard-score element in the list to display the progress of a user.
    39.    /// Either recycles it or creates a fully new one.
    40.    /// </summary>
    41.    /// <param name="name"></param>
    42.    /// <param name="score"></param>
    43.    public UIElement initialiseElement(int index, string name, long score) {
    44.  
    45.        /// If there more than 50 elements, recycle one.
    46.        if (elements.Count > maximum) {
    47.  
    48.            int use = scrollingDown ? 0 : content.childCount - 1;
    49.            int oldIndex = 0;
    50.  
    51.            /// Receiving either first or last child for recycling it.
    52.            LeaderboardElement leaderboardElement = content.GetChild(use).GetComponent<LeaderboardElement>();
    53.            oldIndex = leaderboardElement.getIndex()-1;
    54.            /// Setting it to a new position.
    55.            leaderboardElement.transform.SetSiblingIndex(index);
    56.  
    57.            Canvas.ForceUpdateCanvases();
    58.            return leaderboardElement;
    59.        }
    60.        else {
    61.  
    62.            /// Creates a new one.
    63.            GameObject leaderboardEntry = Instantiate(this.leaderboardElement, this.content, false);
    64.            leaderboardEntry.transform.SetSiblingIndex(index);
    65.  
    66.            Canvas.ForceUpdateCanvases();
    67.            return leaderboardElement;
    68.        }
    69.    }
    70.  
    71.  
    72.    /// <summary>
    73.    /// Gets called during scrolling the ledaderboard scores for loading new ones, listener of scrollRect.onValueChanged();
    74.    /// </summary>
    75.    /// <param name="position"></param>
    76.    public void onScroll(Vector2 position) {
    77.  
    78.        scrollingDown = lastPosition <= content.offsetMax.y;
    79.        lastPosition = content.offsetMax.y;
    80.  
    81.        if (bottom || top) return;
    82.        if (elements.Count < maximum && content.childCount <= 0) return;
    83.  
    84.        float totalHeight = content.sizeDelta.y;
    85.        float difference = content.rect.height - totalHeight;
    86.  
    87.        if (content.offsetMax.y+difference >= content.rect.height - ((content.rect.height / 100) * 20)) {
    88.  
    89.            if (!bottom && onEnd != null) {
    90.                /// Load new ones from the server... once those arrive, received() is getting called.
    91.                bottom = true;
    92.            }
    93.        }
    94.    }

    Did anyone ever had a similar problem ? Or does someone know what i actually need to do for fixing this annoying issue ?

    Im glad for everyhelp i can get to solve this issue, thanks !