Problem
When creating Unity UI for games or applications, we often encounter a situation where there are many similar elements in terms of functionality but different in the types of data they visualize. I want to have a system (hierarchy) where it's easy to create new elements without copy-pasting and generating a lot of redundant code. Below, I've provided an example of elements that share the functionality of an infinite scroll list but display different data. What should I use as a solution?
Solution
Let's abstract the data which we need to populate infinite lists with. We have a base class responsible for handling the scrolling logic of our list. Its primary responsibility is to create the illusion that the user is scrolling through a vast stream of elements (the base class does not know what these elements are). However, during scrolling and element replacement, it should populate them with current data from the abstract data list. The data type should be easily replaceable, and it should still be possible to view/edit the data from the Unity editor. This way, the list script can be used to visualize various types of data: payment history, user activity history, match history, task list, achievements, etc.
PopulateData - Base Data Class.
Thanks to the fact that the DataType property requires overriding in derived classes, we always know which data type we are working with, directly from the base PopulateData.
For populating elements with data, the IPopulateElement interface is used, and all elements that need to be populated with data inherit from it.
Interface IPopulateElement
IPopulateElement serves as a common entry point for populating elements with data. It is designed as a generic interface to define the static data type that a specific element will display.
List base class – InfinityScrollList
During scrolling, elements are populated with current data. To be able to create different types of elements and populate them with different data types, we use the base abstract data class PopulateData.
Example and Usage
HistoryInfinityScrollList - a class for displaying balance changes
Let's create a class that will display a list of player balance changes.
Conditions:
- The class should inherit from the base class for an infinite list (InfiniteScrollList).
- There should be validation and control of the number of elements in the scene.
- Before populating the list, there should be a validation for the availability of data for visualization.
HistoryElement - a class that will be interacted by HistoryInfinityScrollList
Since we need to support two different types of data, let's create HistoryElement as a layer that will determine which final element to display and populate with data.
ActionHistoryElement and GameNightElement - final elements
These classes simply populate the necessary UI components (Image, TextMeshProUGUI, etc.) with the required data.
Hierarchy in the Scene
To control the switching and populating of ActionHistoryElement and GameNightElement, they are child objects of HistoryElement. The hierarchy of all other elements that will inherit from InfinityScrollList will be similar, with the difference being in populating the base element that will be filled with data.

Conclusions
We now have the ability to create UI infinite lists populated with the necessary data types, and we can also expand the base functionality as needed. Here is what we achieved:
- Understanding for creating similar UI elements in the future.
- Ability to prevent chaos in the project.
- Shared hierarchy in infinite lists.
