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) は、まだ表示されていない場合にはオンデマンドで表示されます。

アイテムが作成された後にこれらのフィールドに値を入れるには、SearchProvider を特定のハンドラー (fetchLabelfetchDescriptionfetchThumbnail) で初期化する必要があります。

トラッキングアイテムの選択

trackSelection でコールバックを登録すると、マウスやキーボードを使って検索結果のアイテムを選択するたびに、Search に何か処理をさせることができます。例えば、AssetScene のプロバイダーは、trackSelection コールバックを使用して、検索で選択されたアイテムへの ping を実行します。

ドラッグアンドドロップの有効化

検索プロバイダーの中には、シーンにドラッグアンドドロップできるアイテムを返すものがあります。ドラッグアンドドロップをサポートするアイテムを持つカスタムプロバイダーを作成する場合は、startDrag を実装します。

例えば、AssetScene プロバイダーは DragAndDrop 構造体に、適切なドラッグアンドドロップ操作を可能にするために関連するアイテムの UID を加えます。

コンテクスト検索にプロバイダーを加える

Alt Shift + C のショートカットで検索ウィンドウを開くと、コンテキスト検索が開始されます。つまり、フォーカスのあるウィンドウが検索されます。

コンテキスト検索を起動すると、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
検索プロバイダーの登録