The Unity Pooling Slider System introduces a design pattern and implementation focused on enhancing the performance and efficiency of UI sliders.
To get started, follow these steps:
- Create a Scroll Rect and populate the View and Content fields.
- Add the PoolingSlider class to the Scroll Rect.
Next, create a class model from which the UI sliders will retrieve information:
//Model layer
public class Example
{
public Color Color { private set; get; }
public Sprite Sprite { private set; get; }
public Example(Color color, Sprite sprite)
{
Color = color;
Sprite = sprite;
}
}Fills up your UI slides, use PoolingSlide:
//View layer
public class ExampleSlide : PoolingSlide<Example>
{
[SerializeField] private Image _background;
[SerializeField] private Image _image;
public override void Constructor(Example value)
{
_background.color = value.Color;
_image.sprite = value.Sprite;
}
}Initialize pooling slider with your data in a setup class:
//View layer
public class ExampleSetup : MonoBehaviour
{
[SerializeField] private PoolingSlider _poolingSlider;
// Your pooling slide prefab to use
[SerializeField] private ExampleSlide _prefab;
[Space]
[SerializeField] private Sprite _example1;
[SerializeField] private Sprite _example2;
private void Start()
{
// Fill up List/Array
List<Example> examples = new List<Example>();
for (int i = 0; i < 128; i++)
{
examples.Add(new Example(Color.red, _example1));
// ...
}
_poolingSlider.Constructor(_prefab, examples, clickedExample =>
{
// Action to perform when a slide is clicked
});
}
}This project uses code from the ScrollList repository by boonyifei.
