qiankanglai / LoopScrollRect

These scripts will make your UGUI ScrollRect reusing cells, to improve performance, loading time and draw calls.
http://qiankanglai.me/2015/08/15/LoopScrollRect/
MIT License
2.29k stars 475 forks source link
ugui unity3d

Loop Scroll Rect

openupm License: MIT

These scripts help make your ScrollRect Reusable, because it will only build cells when needed. If you have a large number of cells in a scroll rect, you absolutely need it! It will save a lot of time loading and draw call, along with memory in use, while still working smoothly.

中文说明请看这里

Installation

Open Package Manager and Add package from git URL https://github.com/qiankanglai/LoopScrollRect.git.

With OpenUPM, just one command openupm add me.qiankanglai.loopscrollrect.

With older Unity version, just clone the repo and put into Assets/.

Demo

Demo for Loop Scroll Rect. Each cell knows its own index, and it is able to modify its content/size/color easily.

Also ScrollBar is supported now! It supports both vertical & horizontal directions, back and forth.

Demo1

Demo2

Demo without mask. As you can see, the cells are only instantiated when needed and recycled.

Demo3

New: Scroll to Index

ScrollToIndex

Introduction

The original idea comes from @ivomarel's InfinityScroll. After serveral refactorisations, I almost rewrite all the codes:

My scripts copies ScrollRect from UGUI rather than inherit ScrollRect like InfinityScroll. I need to modify some private variants to make dragging smooth. All my codes is wrapped with comments like ==========LoopScrollRect==========, making maintaining a little easier.

Example

Please refer to InitOnStart.cs for quick example implmentation. It's high recommended for implmentating your own cache pool.

Infinite Version

If you need scroll infinitely, you can simply set totalCount to a negative number.

Quick Jump

I've implemented a simple version with Coroutine. You can use the following API:

public void SrollToCell(int index, float speed)

Here is a corner case unsolved yet: You can't jump to the last cells which cannot be pulled to the start.

Example: Loop Vertical Scroll Rect

These steps may be confusing, so you can just open the demo scene and copy & paste :D

You can also remove EasyObjPool and use your pool instead.

ScrollCell

LoopVerticalScrollRect

If you need scroll from top or left, setting content's pivot to 1 and disable ReverseDirection. Otherwise, you should set 0 to pivot and enable ReverseDirection (I have made VerticalScroll_Reverse in the demo scene as reference).

I highly suggests you trying these parameters by hand. More details can be found in the demo scene.