执行搜索
Add a search and fetch function to the search provider, and use a fuzzy search.
阅读时间4 分钟最后更新于 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
当搜索提供程序需要很长时间来计算其结果或依赖于 WebRequest 等异步搜索引擎时,可以使用异步 API。
fetchItems要使用异步 API,请让 函数返回 。 应该是一个产生结果的函数,以便 API 可以一次获取一项。
fetchItemsIEnumerable<SearchItem>IEnumerable<SearchItem>返回 时,枚举器将在应用程序更新期间存储和迭代。枚举在多个应用程序更新期间继续,直到完成。
IEnumerable<SearchItem>迭代时间受约束,以确保 UI 不被阻止。但是,由于调用位于主线程中,因此如果结果尚未准备就绪,应确保尽快生成。
下面的示例演示了如何使用异步 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 查询资源商店。
AssetStoreProvider.cs - :创建一个启动 Entrian Source Search 索引器的进程,该索引器为项目中的资源提供全文搜索。
ESS.cs