The GridView<TItem> class is an abstract base class designed for creating paginated and filterable grid views. It displays items in a grid layout, handles pagination, can apply filters, and manage item selection.

Heroes

GridView is a large class with lots of properties so it can be complex, but each property is simple on its own.
public GridViewBase(
List<TItem> itemList,
VisualTreeAsset itemSlotTemplate,
GameObject parent,
VisualElement parentElement,
VisualElement slotContainer,
TooltipController tooltipController = null,
UIStartVisibility startVisibility = UIStartVisibility.Visible,
VisualElement focusElement = null,
float fadeDuration = 0.5f,
EasingMode easingMode = EasingMode.EaseOutCirc,
bool disableOtherViewsOnFadeIn = false,
bool debug = false
)| Parameter | Type | Description |
|---|---|---|
| itemList | List<TItem> | Data source, list of items to display |
| itemSlotTemplate | VisualTreeAsset | Template for each grid item slot |
| parent | GameObject | Parent GameObject |
| parentElement | VisualElement | Parent VisualElement |
| slotContainer | VisualElement | Container for grid items |
| tooltipController | TooltipController | Optional tooltip controller for closing on page change |
| Property | Type | Description |
|---|---|---|
| SlotContainer | VisualElement | The container holding item slots |
| ItemSlots | List<VisualElement> | List of all created item slot elements |
| SelectedItems | HashSet<TItem> | Currently selected items |
Creates the grid slot elements. Must be implemented by derived classes.
public abstract void CreateSlots();Refilters and refreshes all items in the grid.
public void Refresh()Updates the data source and refreshes the grid.
public void SetItemList(List<TItem> itemList)public abstract void Select(int slotIndex);
public abstract void Deselect(int slotIndex);
public void DeselectAll();Sets the filter functions for the grid.
public void SetFilters(List<Func<List<TItem>, int, List<TItem>>> filters)Applies a filter value at the specified filter index.
public void SetFilter(int filterIndex, int filter)Clears the filter at the specified index.
public void ClearFilter(int filterIndex)Gets the current filter value at the specified index.
public int GetFilter(int index)Returns the filtered list based on current filters.
public virtual List<TItem> GetFilteredList(List<int> filters)Sets up filter toggle buttons.
public void SetFilterButtons(List<List<Button>> filterButtons)Sets up filter clear buttons.
public void SetFilterClearButtons(List<List<Button>> filterClearButtons)Enables or disables filter buttons at an index.
public void SetEnabledFilterButtons(int filterIndex, bool enabled)Updates filter button visual states.
public void UpdateFilterButtons(int filterIndex)When true, empty slots are hidden instead of shown.
public void SetHideEmpty(bool hideEmpty)Sets the color applied to selected filter buttons.
public void SetSelectedFilterColor(UIColorName color)Sets the USS class applied to selected filter buttons.
public void SetSelectedFilterClass(string selectedFilterClass)Sets the USS class applied to all filter buttons.
public void SetFilterClass(string filterClass)Sets up input prompts for tab navigation (prev/next filters).
public void SetInputPrompt(InputPrompt filterItemPrevious, InputPrompt filterItemNext)Navigate between filter tabs.
public void PreviousTab()
public void PreviousTab(int filterIndex)
public void NextTab()
public void NextTab(int filterIndex)Manually register or unregister input handlers.
public void RegisterInput()
public void UnregisterInput()Registers a callback to update items per line when container size changes.
public void RegisterDynamicItemPerLineUpdate()
public void UnregisterDynamicItemPerLineUpdate()When extending GridViewBase, implement these methods:
// Create slot visual elements
public abstract void CreateSlots();
// Refresh items in slots
protected abstract void RefreshItems();
// Filter list based on current filters
protected abstract void FilterList();
// Handle filter changes
protected abstract void OnFilterChange(int filterIndex);
// Handle dynamic layout changes
protected abstract void DynamicItemPerLineUpdate(GeometryChangedEvent evt);
// Called when creating each slot
protected abstract void OnItemCreate(VisualElement slot);
// Unbind item from slot
protected abstract void UnbindItem(VisualElement slot);
// Bind item to slot
protected abstract void BindItem(VisualElement slot, TItem item, int slotIndex, bool selected);
// Selection handling
public abstract void Select(int slotIndex);
public abstract void Deselect(int slotIndex);using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using CupkekGames.Luna;
public class InventoryGridView : GridViewBase<InventoryItem>
{
private List<InventoryItem> _filteredList;
public InventoryGridView(
List<InventoryItem> items,
VisualTreeAsset template,
GameObject parent,
VisualElement parentElement,
VisualElement container
) : base(items, template, parent, parentElement, container)
{
_slotPerLine = 4;
_itemWidth = 80;
}
public override void CreateSlots()
{
_slotContainer.Clear();
ItemSlots.Clear();
int lineCount = Mathf.CeilToInt((float)_itemList.Count / _slotPerLine);
for (int i = 0; i < lineCount; i++)
{
VisualElement line = MakeLine();
_slotContainer.Add(line);
}
RefreshItems();
}
protected override void FilterList()
{
_filteredList = GetFilteredList(_currentFilters);
}
protected override void RefreshItems()
{
for (int i = 0; i < ItemSlots.Count; i++)
{
VisualElement slot = ItemSlots[i];
if (i < _filteredList.Count)
{
InventoryItem item = _filteredList[i];
bool selected = _selectedItems.Contains(item);
BindItem(slot, item, i, selected);
slot.style.visibility = Visibility.Visible;
}
else
{
UnbindItem(slot);
slot.style.visibility = _hideEmpty ? Visibility.Hidden : Visibility.Visible;
}
}
}
protected override void OnItemCreate(VisualElement slot)
{
// Initial slot setup
slot.AddToClassList("inventory-slot");
}
protected override void BindItem(VisualElement slot, InventoryItem item, int index, bool selected)
{
var icon = slot.Q<VisualElement>("Icon");
icon.style.backgroundImage = new StyleBackground(item.Icon);
var label = slot.Q<Label>("Name");
label.text = item.Name;
if (selected)
{
slot.AddToClassList("selected");
}
else
{
slot.RemoveFromClassList("selected");
}
}
protected override void UnbindItem(VisualElement slot)
{
var icon = slot.Q<VisualElement>("Icon");
icon.style.backgroundImage = null;
var label = slot.Q<Label>("Name");
label.text = "";
slot.RemoveFromClassList("selected");
}
protected override void OnFilterChange(int filterIndex)
{
// Handle filter change - e.g., update category header
}
protected override void DynamicItemPerLineUpdate(GeometryChangedEvent evt)
{
float width = _slotContainer.resolvedStyle.width;
int newSlotPerLine = Mathf.Max(1, Mathf.FloorToInt(width / _itemWidth));
if (newSlotPerLine != _slotPerLine)
{
_slotPerLine = newSlotPerLine;
CreateSlots();
}
}
public override void Select(int slotIndex)
{
if (slotIndex < _filteredList.Count)
{
_selectedItems.Add(_filteredList[slotIndex]);
RefreshItems();
}
}
public override void Deselect(int slotIndex)
{
if (slotIndex < _filteredList.Count)
{
_selectedItems.Remove(_filteredList[slotIndex]);
RefreshItems();
}
}
}Settings
Theme
Light
Contrast
Material
Dark
Dim
Material Dark
System
Sidebar(Light & Contrast only)
Font Family
DM Sans
Wix
Inclusive Sans
AR One Sans
Direction