Loop Scroll Rect Documentation

repository·master·Indexed 25 days ago

https://github.com/qiankanglai/loopscrollrect

A Unity utility (me.qiankanglai.loopscrollrect) that makes ScrollRects reusable by building cells only when needed. It reduces loading times, draw calls, and memory usage for large cell counts. Key features include support for variable cell sizes via LoopScrollSizeHelper, data provision through LoopScrollDataSource, and API methods for scrolling to specific cells and refreshing content.

Tokens
682
Snippets
1
Records
6
Agent score
33%

What's inside Loop Scroll Rect

  1. Set up Scroll Cells

    master

    Cells used in the loop scroll rect must meet two requirements:

    1. Layout Properties: Cells need preferred width and height for layout calculation. The easiest way is to attach a Layout Element component and set the preferred width and height.
    2. Index Callback: Each cell must have a script attached that implements a method with the signature void ScrollCellIndex (int idx) to receive its index updates.
  2. Install Loop Scroll Rect

    master

    You can install the package using OpenUPM or via the Unity Package Manager using a Git URL.

    Using OpenUPM:

    openupm add me.qiankanglai.loopscrollrect

    Using Unity Package Manager: Add package from git URL: https://github.com/qiankanglai/LoopScrollRect.git

    openupm add me.qiankanglai.loopscrollrect
  3. Quick Start with Loop Scroll Rect

    master

    To implement a basic loop scroll rect, you should follow these steps:

    1. Provide Cells: Implement the LoopScrollPrefabSource interface to provide different cells. This is highly recommended for using a cache pool.
    2. Provide Data: Implement the LoopScrollDataSource interface to provide data for a specified cell.
    3. Handle Variable Sizes (Optional): If your cells have different sizes, implement LoopScrollSizeHelper to provide precise cell sizes.

    For the simplest implementation reference, see InitOnStart.cs in the repository.

  4. Use Loop Scroll Rect API for Scrolling and Testing

    master

    The following methods are available for controlling scroll position and testing refill logic:

    Scroll Control

    • ScrollToCell: Scrolls to a specified item and offset with a given speed.
    • ScrollToCellWithinTime: Scrolls to a specified item and offset over a specific duration.

    Refill Testing

    • GetFirstItem: Returns the index and offset of the first visible item.
    • Refill (Test mode): Clears and refills items from the start using a specified first item and offset.
    • GetLastItem: Returns the index and offset of the last visible item.
    • RefillFromEnd (Test mode): Clears and refills items from the end using a specified last item and offset.
  5. Use Loop Scroll Rect API for Basic Usage

    master

    Use these methods to manage the content of the scroll rect:

    • Clear: Clears existing items and sets the total count to zero.
    • Refresh: Updates data for existing cells without affecting the layout (optimized for performance).
    • Refill: Clears and refills items from the start; triggers a full layout recalculation.
    • RefillFromEnd: Clears and refills items from the end; triggers a full layout recalculation.
  6. Configure Loop Scroll Rect Inspector Settings

    master

    The following settings are available in the Inspector:

    • Total Count: The total number of cells. Use a negative number to represent infinity.
    • Reverse Direction: Enable this for scrolling from down-to-top (vertical) or right-to-left (horizontal). You must adjust the Content's pivot and anchor accordingly.