Skip to content

Game.UI.Editor.PrefabPickerAdapter

Assembly: Assembly-CSharp.dll
Namespace: Game.UI.Editor

Type: class

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

Summary:
Adapter implementation that bridges prefab data (PrefabBase) to the editor UI prefab picker controls. Responsible for: - managing the full prefab list and a filtered subset used by the picker, - handling search query parsing (including tag search using '#' prefix), - maintaining search history and favorites (persisted via SharedSettings.instance.editor), - managing per-prefab favorites and thumbnail/badge lookups, - providing search suggestions and filter lists derived from prefab tags, - notifying when a prefab is selected via EventPrefabSelected, - storing column count and other UI-related settings. This class is used by the editor UI to present, filter and select prefabs in Cities: Skylines 2 mod tooling.


Fields

  • public const int kMaxHistoryLength = 100
    Maximum number of search history entries stored. History is trimmed to this length when new entries are added.

  • public const int kMaxHistoryDisplayLength = 20
    Maximum number of history suggestions displayed in the search popup.

  • private ImageSystem m_ImageSystem
    Cached reference to the image/thumbnail system used to obtain prefab thumbnails (not assigned in the class source; present for potential image lookups).

  • private string m_SearchQuery = string.Empty
    Current search query string entered by the user.

  • private bool m_SearchQueryIsFavorite
    Whether the current query is marked as a favorite.

  • private bool m_SearchQueryChanged
    Flag used to indicate the search query changed and filtered items need updating.

  • private List<PrefabItem> m_Items = new List<PrefabItem>()
    Complete list of all PrefabItem entries constructed from provided prefabs.

  • private bool m_ItemsChanged
    Flag indicating the underlying items changed and the filtered list should be recomputed.

  • private List<string> m_AvailableFilters = new List<string>()
    All available filter tags derived from the prefab set (used to populate filter menu).

  • private List<string> m_ActiveFilters = new List<string>()
    Currently active filters selected by the user.

  • private bool m_ActiveFiltersChanged
    Flag indicating active filters changed and filter update is required.

  • private List<PrefabItem> m_FilteredItems = new List<PrefabItem>()
    Current list of items after applying search and active filters (presented to the UI).

  • private bool m_FilteredItemsChanged
    Flag set when filtered items have changed (used to signal the UI to refresh).

  • private PrefabItem m_SelectedItem
    Selected PrefabItem (UI-level selection).

  • private PrefabBase m_SelectedPrefab
    Selected PrefabBase (data-level selection). kept in sync with m_SelectedItem.

  • private List<string> m_SearchHistory = new List<string>()
    Ordered search history (most recent first), persisted to EditorSettings when loaded/changed.

  • private HashSet<string> m_SearchFavorites = new HashSet<string>()
    Set of favorite search queries (persisted).

  • private List<PopupSearchField.Suggestion> m_SearchSuggestions = new List<PopupSearchField.Suggestion>()
    Current suggestions shown by the popup search field, derived from history and favorites.

  • private HashSet<string> m_FavoriteIds = new HashSet<string>()
    Set of prefab IDs that are marked as favorites (persisted into EditorSettings.prefabPickerFavorites).

  • private int m_ColumnCount
    Number of columns used by the picker grid; persisted to EditorSettings.

  • public Action<PrefabBase> EventPrefabSelected
    Event invoked when a prefab is selected. Subscribe to handle selection actions in the editor UI.

  • public bool displayPrefabTypeTooltip { get; set; }
    When true, a tooltip showing the prefab type name is assigned to PrefabItem.tooltip when SetPrefabs builds items.


Properties

  • [CanBeNull] public PrefabBase selectedPrefab { get; set; }
    Gets or sets the currently selected PrefabBase. Setting updates the internal selected prefab reference (m_SelectedPrefab). When selection changes it will be reflected in the picker via Update() which attempts to set the corresponding PrefabItem.

  • public List<string> availableFilters => m_AvailableFilters
    Read-only accessor for available filters (tags) discovered from the loaded prefabs.

  • public List<string> activeFilters => m_ActiveFilters
    Read-only accessor for the currently active filter tags.

  • public Action onAvailableFiltersChanged { get; set; }
    Callback invoked when available filters are recomputed (for example after SetPrefabs).

  • PrefabItem ItemPicker<PrefabItem>.IAdapter.selectedItem { get; set; }
    Explicit interface implementation for ItemPicker adapter selected item. Setting this also updates m_SelectedPrefab, invokes EventPrefabSelected, and records the search query to history when appropriate (persisted to EditorSettings).

  • List<PrefabItem> ItemPicker<PrefabItem>.IAdapter.items => m_FilteredItems
    Explicit interface accessor returning the current filtered items used by the picker UI.

  • public string searchQuery { get; set; }
    Search query string. Setting this flags the query as changed and updates searchQueryIsFavorite accordingly.

  • public bool searchQueryIsFavorite => m_SearchQueryIsFavorite
    Indicates whether the current search query is marked as a favorite.

  • IEnumerable<PopupSearchField.Suggestion> PopupSearchField.IAdapter.searchSuggestions => m_SearchSuggestions
    Explicit adapter property providing current search suggestions (history and favorites) to the popup search UI.

  • int ItemPickerFooter.IAdapter.length => m_FilteredItems.Count
    Footer adapter property exposing the number of filtered items (used for display counts / paging).

  • public int columnCount { get; set; }
    Number of columns in the picker grid. Setting persists the value to EditorSettings.prefabPickerColumnCount.


Constructors

  • public PrefabPickerAdapter()
    No explicit constructor is defined in the source; the default constructor initializes internal fields to their defaults. Typical usage: construct the adapter, call LoadSettings(), then SetPrefabs(...) to populate.

Methods

  • public void LoadSettings()
    Loads persisted settings from SharedSettings.instance.editor (if available). Restores search history, favorites (both search favorites and prefab favorites), and column count. Clears internal collections before loading. Use this during initialization to restore the user's previous picker state.

  • public void SetPrefabs([ItemCanBeNull] ICollection<PrefabBase> prefabs)
    Populates the adapter from a collection of PrefabBase objects. Builds PrefabItem entries (displayName, tooltip when displayPrefabTypeTooltip is true, tags, badge, image and favorite state). Accumulates available filter tags from prefab tags and notifies onAvailableFiltersChanged. Sorts items and marks items changed so an Update() will refresh filtered list.

  • private bool TryGetDLCBadge(PrefabBase prefab, out string icon)
    Attempts to discover a DLC/pack badge icon path for the prefab by inspecting AssetPackItem or ContentPrerequisite components. Returns true and sets icon if found. Used when building PrefabItem.badge in SetPrefabs.

  • public PrefabBase SelectPrefabByName(string name, StringComparison comparisonType)
    Selects and returns the first PrefabBase whose name matches the provided name using the given StringComparison. Updates m_SelectedPrefab accordingly.

  • public void Update()
    Should be called periodically (for example, each frame or when UI updates). Synchronizes m_SelectedItem with m_SelectedPrefab if needed. If items, search query, or active filters changed, recomputes filtered items by calling UpdateFilteredItems() and clears the corresponding change flags.

  • private void UpdateFilteredItems()
    Core filtering logic. Clears search suggestions and filtered items, parses the search query into words and tag tokens (tags start with '#'), merges active filters with explicit tags, and computes filtered item list. Also populates search suggestions from history and favorites (respecting kMaxHistoryDisplayLength). Performs case-insensitive substring matching on prefab names and supports type-name matching as well as tag matching and incomplete-tag suggestions.

  • [CanBeNull] private static string GetIncompleteTag(string[] searchParts)
    Helper that checks the last token in the search parts and returns the incomplete tag (without the '#' prefix) if the last token begins with '#' and has length > 1; otherwise returns null. Used to provide tag autocompletion behavior.

  • bool ItemPicker<PrefabItem>.IAdapter.Update()
    Explicit interface implementation used by the item picker to determine whether filtered items changed since the last update. Returns true if filtered items changed (and resets the changed flag).

  • void ItemPicker<PrefabItem>.IAdapter.SetFavorite(int index, bool favorite)
    Marks the prefab at the provided index in the filtered items as favorite or not. Updates internal favorite ID set, persists EditorSettings.prefabPickerFavorites, updates item favorite flags, re-sorts lists and marks filtered items changed.

  • void PopupSearchField.IAdapter.SetFavorite(string query, bool favorite)
    Marks a search query as favorite (or removes it). Updates m_SearchFavorites and persisted EditorSettings.prefabPickerSearchFavorites and prefabPickerSearchHistory. If the query equals the current searchQuery, updates the searchQueryIsFavorite flag. Calls UpdateFilteredItems() to refresh suggestions.

  • public void ToggleFilter(string filter, bool active)
    Activates or deactivates a filter tag in m_ActiveFilters. Sets m_ActiveFiltersChanged to request a filter update.

  • public void ClearFilters()
    Clears all active filters and flags that the active filters changed (forces a recompute of filtered items on next Update()).


Usage Example

// Example initialization for an editor window / picker
var adapter = new PrefabPickerAdapter();
adapter.LoadSettings(); // restore persisted user settings

// supply all prefabs (from game content) to populate the picker
adapter.SetPrefabs(prefabCollection); // prefabCollection: ICollection<PrefabBase>

// respond to selection events
adapter.EventPrefabSelected += prefab =>
{
    if (prefab != null)
    {
        Debug.Log("Prefab selected: " + prefab.name);
    }
};

// set search query (updates suggestions and marks for refresh)
adapter.searchQuery = "park #decoration";

// update from your UI loop to recompute filtered items when needed
adapter.Update();

// access filtered items when rendering the picker UI (via explicit interface)
var items = ((ItemPicker<PrefabItem>.IAdapter)adapter).items;
int count = ((ItemPickerFooter.IAdapter)adapter).length;