The following tutorial will go through creating a performant scrollable grid of numbers.
Check Example scenes for full code and watch the full process in video.
Base class for cells that appear in Pooled Grid. Creating your custom cell class, inherit from PooledCell
.
Now we can create cell prefab that then will be used by Pooled Grid Script to instantiate actual cells.
This is a generic class that you need to inherit to create your custom Pooled Grid. Assuming that you already created custom PooledCell
class, let's create custom Pooled Grid Script:
Here we actually just giving a name to a generic class with our custom cell.
This script we then can then attach to a ScrollRect in the scene and choose cell prefab that we created earlier:
Now the only code that is missing is our custom data source.
Pooled Grid Layout Script gets data from the object that implements IPooledGridLayoutDataSource
interface. Through implementing this interface you feed data to your Pooled Grid.
Any script that implements IPooledGridLayoutDataSource
can be a data source for our Pooled Grid.
Let's just create a MonoBehaviour script that will be attached to some gameobject in scene:
Script contains a link to Pooled Grid that we will use to set grid's data source to this script.
First we will initialize data that we want to display in out grid:
Here we also set DataSource
link and reload the grid to query this script for data through IPooledGridLayoutDataSource
interface.
Lets implement this interface:
Now all we need to do is create a game object in scene to hold our Data Source script and attach PooledGrid (that sits on a ScrollRect) to it:
Click Play and you should see a fast scrolling pooled grid: