Skip to content

Game.UI.Editor.AssetPickerAdapter

Assembly: Game
Namespace: Game.UI.Editor

Type: public class

Base: ItemPicker.IAdapter, SearchField.IAdapter, ItemPickerFooter.IAdapter

Summary:
Adapter that provides AssetItem data and behavior to an ItemPicker UI used in the editor. Manages the full list of assets, a filtered list based on a search query, selection, favorites, and column count. Persists favorites and (optionally) column count to SharedSettings.instance.editor. Supports explicit interface implementations required by the item picker, search field and footer components.


Fields

  • private string m_SearchQuery = string.Empty
    Holds the current search query used to filter displayed items.

  • private List<AssetItem> m_Items
    Master list of all AssetItem instances managed by the adapter.

  • private List<AssetItem> m_FilteredItems
    Current filtered list (subset of m_Items) based on m_SearchQuery. This is what the ItemPicker UI consumes.

  • private bool m_FilteredItemsChanged
    Flag set when the filtered list has changed. Returned by the explicit Update() interface method so the UI can refresh.

  • [CanBeNull] private AssetItem m_SelectedItem
    Currently selected asset item (nullable).

  • private int m_ColumnCount
    Number of columns the picker should render.

  • private bool m_UseGlobalColumnCount = true
    When true, columnCount changes are written to SharedSettings.instance.editor.assetPickerColumnCount; when false, columnCount is local to this adapter.

  • private HashSet<string> m_FavoriteIds = new HashSet<string>()
    Set of favorite asset GUID strings persisted to editor settings.

  • public Action<AssetItem> EventItemSelected
    Event invoked when an item is selected via the explicit interface setter.

Properties

  • public AssetItem selectedItem { get; set; }
    Gets or sets the currently selected AssetItem. Setting this directly updates the adapter's selection but does not invoke EventItemSelected (the explicit interface setter does).

  • AssetItem ItemPicker<AssetItem>.IAdapter.selectedItem { get; set; }
    Explicit interface implementation used by the ItemPicker. Getting returns the current selection; setting assigns the selection and invokes EventItemSelected if non-null.

  • List<AssetItem> ItemPicker<AssetItem>.IAdapter.items => m_FilteredItems
    Explicit interface property returning the list of items the picker should display (the filtered list).

  • public string searchQuery { get; set; }
    Public property to get/set the search query. When changed, triggers UpdateFilteredItems() to rebuild m_FilteredItems.

  • int ItemPickerFooter.IAdapter.length => m_FilteredItems.Count
    Explicit interface property used by the footer to obtain the current number of filtered items.

  • public int columnCount { get; set; }
    Number of columns the picker uses. When set and m_UseGlobalColumnCount is true, the value is saved to SharedSettings.instance.editor.assetPickerColumnCount.

Constructors

  • public AssetPickerAdapter(IEnumerable<AssetItem> items, int columnCount = 0)
    Creates a new adapter:
  • Loads favorite IDs from SharedSettings.instance?.editor.assetPickerFavorites (if present) into m_FavoriteIds.
  • Calls SetItems(items) to populate, mark favorites and sort lists.
  • If columnCount <= 0, uses the global editor setting editor.assetPickerColumnCount (defaulting to 4 if missing); otherwise uses the provided columnCount and disables global persistence.

Methods

  • public void SetItems(IEnumerable<AssetItem> items)
    Replaces the adapter's master list with the provided items:
  • Converts to a List, marks each item.favorite based on m_FavoriteIds, sorts m_Items, and initializes m_FilteredItems as a copy of m_Items.

  • public AssetItem SelectItemByName(string name, StringComparison comparisonType)
    Finds the first AssetItem in m_Items whose fileName equals the given name using the specified comparison and sets it as the current selection. Returns the selected item (or null if not found).

  • public AssetItem SelectItemByGuid(Hash128 guid)
    Finds the first AssetItem in m_Items with a matching guid and sets it as the current selection. Returns the selected item (or null if not found).

  • private void UpdateFilteredItems()
    Rebuilds m_FilteredItems based on m_SearchQuery:

  • If the search query is empty, m_FilteredItems becomes a copy of m_Items.
  • Otherwise, filters m_Items to those whose fileName contains the query (case-insensitive).
  • Sets m_FilteredItemsChanged = true.

  • bool ItemPicker<AssetItem>.IAdapter.Update()
    Explicit interface method called by the UI to know if the filtered items changed. Returns the previous value of m_FilteredItemsChanged and clears the flag.

  • void ItemPicker<AssetItem>.IAdapter.SetFavorite(int index, bool favorite)
    Explicit interface method used to toggle favorite state for an item at the given index in m_FilteredItems:

  • Updates m_FavoriteIds and writes the favorites array back to SharedSettings.instance?.editor.assetPickerFavorites (if available).
  • Updates the assetItem.favorite flag, re-sorts m_Items and m_FilteredItems, and sets m_FilteredItemsChanged = true.

Usage Example

// Create the adapter with a list of AssetItem and use global column count:
var adapter = new AssetPickerAdapter(allAssets);

// Subscribe to selection events:
adapter.EventItemSelected = item => {
    if (item != null) {
        Debug.Log($"Selected asset: {item.fileName} ({item.guid})");
    }
};

// Filter items by name:
adapter.searchQuery = "park";

// Programmatically select an item by name:
var selected = adapter.SelectItemByName("Small Park", StringComparison.OrdinalIgnoreCase);

// Toggle favorite via the picker interface (example usage from picker code):
// ((ItemPicker<AssetItem>.IAdapter)adapter).SetFavorite(0, true);