検索の実行
Add a search and fetch function to the search provider, and use a fuzzy search.
読み終わるまでの所要時間 5 分最終更新 1ヶ月前
検索プロバイダーは、 関数を使用して、アイテムの検索と結果のフィルタリングを行います。 関数は、以下のようなシグネチャを持っています。
fetchItemsfetchItems// context: the necessary search context (for example, tokenized search and// sub-filters).// items: list of items to populate (if not using the asynchronous api)// provider: the Search Provider itselfpublic delegate IEnumerable<SearchItem> GetItemsHandler(SearchContext context, List<SearchItem> items, SearchProvider provider);
注意非同期API を使用しない場合、fetchItems関数でfetchItemsを返さなければなりません。null
SearchItempublic struct SearchItem{ public readonly string id; // The item score affects how Search sorts the item within the results from the Search Provider. public int score; // Optional: Display name of the item. If the item does not have one, // SearchProvider.fetchLabel is called). public string label; // If the item does not have a description SearchProvider.fetchDescription // is called when Search first displays the item. public string description; // If true, the description already has rich text formatting. public SearchItemDescriptionFormat descriptionFormat; // If the item does not have a thumbnail, SearchProvider.fetchThumbnail // is called when Search first displays the item. public Texture2D thumbnail; // Search Provider user-customizable content public object data;}
SearchItemidヒント:に従ってフィルタリングする場合、静的関数SearchContext.searchTextで部分検索を行うことができます。SearchProvider.MatchSearchGroup
ファジーサーチの使用
アイテムに対してファジーサーチを使うには、以下の例のように を使用します。
FuzzySearch.FuzzyMatchif (FuzzySearch.FuzzyMatch(sq, CleanString(item.label), ref score, matches)) item.label = RichTextFormatter.FormatSuggestionTitle(item.label, matches);
すべての検索アイテムは、同じプロバイダーのアイテムに対して でソートされます。スコアが低いアイテムがリストの一番上に表示されます (昇順ソート)。
score非同期検索 API
非同期 API は、検索プロバイダーの結果計算に時間がかかる場合や、WebRequests などの非同期検索エンジンに依存する場合に使用できます。
fetchItems非同期 API を使用するには、 関数が を返すようにします。 は、結果を返す関数である必要があります。API が一度に 1 つのアイテムをフェッチできるからです。
fetchItemsIEnumerable<SearchItem>IEnumerable<SearchItem>IEnumerable<SearchItem>UI がブロックされないように反復処理時間は制限されます。ただし、この呼び出しはメインスレッドで行われるため、結果が準備できない場合は、できるだけ早く yield する必要があります。
以下の例は、非同期の API を使用する方法を示しています。
fetchItemspublic class AsyncSearchProvider : SearchProvider{ public AsyncSearchProvider(string id, string displayName = null) : base(id, displayName) { fetchItems = (context, items, provider) => FetchItems(context, provider); } private IEnumerable<SearchItem> FetchItems(SearchContext context, SearchProvider provider) { while(ResultsNotReady()) { yield return null; } var oneItem = // Get an item yield return oneItem; var anotherItem = // Get another item yield return anotherItem; if(SomeConditionThatBreaksTheSearch()) { // Search must be terminated yield break; } // You can iterate over an enumerable. The enumeration // continues where it left. foreach(var item in someItems) { yield return item; } }}
- : WebRequest を使って Asset Store にクエリを行います。
AssetStoreProvider.cs - : Entrian Source 検索インデックスを起動するプロセスを作成し、プロジェクト内のアセットに対する完全なテキスト検索を提供します。
ESS.cs