Skip to content

Game.UI.Editor.PrefabPickerPopup

Assembly:
Assembly-CSharp (game code / modding assembly; actual assembly may vary)

Namespace:
Game.UI.Editor

Type:
public class PrefabPickerPopup

Base:
IValueFieldPopup

Summary:
PrefabPickerPopup is a UI popup used by the editor to let users pick a PrefabBase-derived asset (buildings, props, etc.). It composes a search field, an item picker and a footer via a PrefabPickerAdapter and exposes a nullable option. The popup reads available prefabs from the game's PrefabSystem (via the ECS World) and applies an optional runtime filter and type check to build the picker list. When the user selects a prefab the popup writes the selection back to a supplied ITypedValueAccessor.


Fields

  • private ITypedValueAccessor<PrefabBase> m_Accessor
    Holds the accessor to read/write the currently bound value. This is provided via Attach and used to set the selected prefab when the user picks one.

  • private Type m_PrefabType
    The Type used to filter prefabs by type (must be a PrefabBase-derived type). Only prefabs for which m_PrefabType.IsInstanceOfType(prefab) returns true will be shown.

  • private Func<PrefabBase, bool> m_Filter
    Optional additional runtime filter applied to candidate prefabs. If null no extra filtering is done.

  • private PrefabPickerAdapter m_Adapter
    The adapter that powers the popup widgets (search field, item picker, footer). Responsible for holding the prefab list, selection state and persistence of settings.

Properties

  • public bool nullable { get; set; }
    If true, the popup will include a null entry (empty selection) in the prefab list. Useful for optional prefab fields.

  • public IList<IWidget> children { get; }
    The child widgets that constitute the popup UI: a PopupSearchField, an ItemPicker, and an ItemPickerFooter, all wired to the same adapter. These are set up in the constructor and have default widget settings applied.

Constructors

  • public PrefabPickerPopup(Type prefabType, Func<PrefabBase, bool> filter = null)
    Creates a new popup for prefabs of the specified prefabType. Optionally accepts a filter delegate to exclude certain prefabs. The constructor:
  • stores the type and filter,
  • creates and configures a PrefabPickerAdapter,
  • subscribes to adapter.EventPrefabSelected to forward user selections to OnPrefabSelected,
  • creates the three child widgets (search, item picker, footer),
  • applies default widget settings.

Methods

  • public bool Update()
    Called each frame by the UI system. Syncs the adapter.selectedPrefab from the accessor's current value, calls m_Adapter.Update(), and returns false. This keeps the UI selection consistent with the bound value.

  • public void Attach(ITypedValueAccessor<PrefabBase> accessor)
    Binds the popup to an accessor. When attached the popup:

  • stores the accessor,
  • builds a List by querying the game's PrefabSystem (via World.DefaultGameObjectInjectionWorld?.GetExistingSystemManaged()),
  • includes a null entry if nullable is true,
  • filters prefabs by m_PrefabType and m_Filter,
  • passes the resulting list to m_Adapter.SetPrefabs(list) and calls m_Adapter.LoadSettings() so the UI reflects saved settings.

Note: World.DefaultGameObjectInjectionWorld or the PrefabSystem can be null (e.g., in certain initialization contexts); Attach handles that by simply not adding prefabs if the system can't be found.

  • public void Detach()
    Cleans up the adapter state when the popup is no longer used. It clears the adapter.searchQuery and sets the adapter's prefab list to an empty array.

  • public LocalizedString GetDisplayValue(PrefabBase value)
    Returns a localized label for the given prefab (or null) using EditorPrefabUtils.GetPrefabLabel(value). Useful for showing the current selection in the host UI.

  • private void OnPrefabSelected(PrefabBase prefab)
    Event handler invoked by the adapter when the user selects a prefab. Writes the selected prefab into the bound accessor (m_Accessor.SetTypedValue(prefab)).

Usage Example

// Example: create a popup for prefabs (any PrefabBase type) allowing null selection
var popup = new PrefabPickerPopup(typeof(PrefabBase), filter: prefab => prefab != null && !prefab.package?.isInternal == true);
popup.nullable = true;

// Suppose 'accessor' implements ITypedValueAccessor<PrefabBase> and is provided by the editor field binding:
popup.Attach(accessor);

// In your UI update loop:
popup.Update();

// When finished / closed:
popup.Detach();

Notes and tips: - The popup relies on PrefabSystem being available in World.DefaultGameObjectInjectionWorld. In contexts where the ECS World isn't present (early boot or certain unit tests), Attach will not populate the list. - Use the filter parameter to narrow down prefabs (e.g., only buildings, or only assets from a particular package). - The adapter saves/loads settings (like sorting or favorites), so Attach calls LoadSettings to restore previous UI state.