Version: Unity 6.0 (6000.0)
语言 : 中文
Create a custom search provider
注册搜索提供程序

SearchProvider 类

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;
}

缓存和释放资源

启动 Search 窗口时,将会调用 onEnable,其可用于缓存资源。

关闭 Search 窗口时,将会调用 onDisable,其可用于释放资源。

初始化

由于搜索项列表使用虚拟滚动算法,因此某些 SearchItem 字段(例如,labelthumbnaildescription)会按需获取(如果尚未提供它们的话)。

要在创建项后填充这些字段,需要使用特定处理程序(fetchLabelfetchDescriptionfetchThumbnail)初始化 SearchProvider

跟踪项选择

您可以在 trackSelection 上注册回调,让 Search 在您每次使用鼠标或键盘在搜索结果中选择一项时执行某些操作。例如,资源场景提供程序使用 trackSelection 回调在 Search 中 ping 所选项。

支持拖放

某些搜索提供程序会返回可以拖放到场景中的项。如果要创建其项支持拖放的自定义提供程序,请实现 startDrag

例如,资源场景提供程序会使用相关项 UID 填充 DragAndDrop 结构,从而允许正确的拖放交互。

在上下文搜索中包含提供程序

使用 Alt + Shift + C 快捷键打开 Search 窗口时,它会启动上下文搜索,这意味着 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");
Create a custom search provider
注册搜索提供程序