SearchProvider 类执行对特定类型项目的搜索,并管理缩略图、描述和子过滤器。
它具有以下基本 API
public class SearchProvider
{
    public SearchProvider(string id, string displayName = null);
    // Creates an Item bound to this provider.
    public SearchItem CreateItem(string id, string label = null, string description = null, Texture2D thumbnail = null);
    // Utility functions to check whether the search text matches a string.
    public static bool MatchSearchGroups(string searchContext, string content);
    public static bool MatchSearchGroups(string searchContext, string content,
                                        out int startIndex, out int endIndex);
    // The provider's unique ID.
    public NameId name;
    // Text token to "filter" a provider (for example, "me:", "p:", and "s:").
    public string filterId;
    // This provider is only active when a search explicitly specifies it with
    // its filterId.
    public bool isExplicitProvider;
    // Handler to fetch and format the label of a search item.
    public FetchStringHandler fetchLabel;
    // Handler to provide an async description for an item. Called just before
    // Search displays the item.
    // Allows a plug-in provider to fetch long descriptions only when
    // Search needs them.
    public FetchStringHandler fetchDescription;
    // Handler to provider an async thumbnail for an item. Called just before
    // Search displays the item.
    // Allows a plug-in provider to fetch/generate previews only when
    // Search needs them.
    public PreviewHandler fetchThumbnail;
    // Handler to support drag interactions. It is up to the SearchProvider
    // to properly set up the DragAndDrop manager.
    public StartDragHandler startDrag;
    // Called when the selection changes and Search can track it.
    public TrackSelectionHandler trackSelection;
    // MANDATORY: Handler to get items for a search context.
    public GetItemsHandler fetchItems;
    // A Search Provider can return a list of words that help the user complete
    // their search query.
    public GetKeywordsHandler fetchKeywords;
    // List of sub-filters that are visible in the FilterWindow for a
    // SearchProvider (see AssetProvider for an example).
    public List<NameId> subCategories;
    // Called when the Search window opens. Allows the Provider to perform
    // some caching.
    public Action onEnable;
    // Called when the Search window closes. Allows the Provider to release
    // cached resources.
    public Action onDisable;
    // Int to sort the Provider. Affects the order of search results and the
    // order in which providers are shown in the FilterWindow.
    public int priority;
    // Called when Search opens in "contextual mode". If you return true
    // it means the provider is enabled for this search context.
    public IsEnabledForContextualSearch isEnabledForContextualSearch;
}
当您启动搜索窗口时,它会调用 onEnable,您可以使用它来缓存资源。
当您关闭搜索窗口时,它会调用 onDisable,您可以使用它来释放资源。
由于搜索项目列表使用虚拟滚动算法,因此某些 SearchItem 字段(例如,label、thumbnail 和 description)是在需要时获取的,如果它们尚未提供。
要在创建项目后填充这些字段,您需要使用特定处理程序初始化 SearchProvider(fetchLabel、fetchDescription、fetchThumbnail)。
您可以在 trackSelection 上注册回调,以便 Search 在您使用鼠标或键盘在搜索结果中选择项目时执行某些操作。例如,资源任何可以在游戏中或项目中使用的媒体或数据。资源可能来自在 Unity 之外创建的文件,例如 3D 模型、音频文件或图像。您也可以在 Unity 中创建一些资源类型,例如动画控制器、音频混音器或渲染纹理。 更多信息
请参见 词汇表 和 场景场景包含游戏环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,您放置环境、障碍物和装饰,基本上是逐块设计和构建您的游戏。 更多信息
请参见 词汇表 提供程序使用 trackSelection 回调在 Search 中对选定项目进行 ping 操作。
某些搜索提供程序返回可以拖放到场景中的项目。如果您正在创建一个其项目支持拖放的自定义提供程序,请实现 startDrag。
例如,资源 和 场景 提供程序使用相关的项目 UID 填充 DragAndDrop 结构,以允许适当的拖放交互。
当您使用 Alt Shift + C 快捷键打开搜索窗口时,它将启动上下文搜索,这意味着 Search 会搜索具有焦点的窗口。
当您启动上下文搜索时,覆盖了 isEnabledForContextualSearch 的提供程序会检查它们是否应该启用,如下面的示例所示
// Taken from Scene hierarchy provider:
// Makes the provider part of the contextual search if the Scene view or the
// Hierarchy window has focus.
isEnabledForContextualSearch = () =>
                QuickSearchTool.IsFocusedWindowTypeName("SceneView") ||
                QuickSearchTool.IsFocusedWindowTypeName("SceneHierarchyWindow");