Version: 6000.3
语言: 中文
注册搜索提供程序
注册作处理程序

执行搜索

搜索提供程序使用fetchItems功能来搜索项目并过滤结果。这fetchItems函数具有以下签名:

// 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 itself
public delegate IEnumerable<SearchItem> GetItemsHandler(SearchContext context,
                                    List<SearchItem> items,
                                    SearchProvider provider);

SearchProvider必须添加新的SearchItems 到itemslist 或返回IEnumerable<SearchItem>.

注意:如果您不使用异步fetchItemsAPI,您必须返回null在你的fetchItems功能。

一个SearchItem是一个简单的结构体:

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

一个SearchItem仅需要id.

提示:当您根据SearchContext.searchText使用 static 函数SearchProvider.MatchSearchGroup进行部分搜索。

使用模糊搜索

要对项目使用模糊搜索,您可以使用FuzzySearch.FuzzyMatch,如以下示例所示:

if (FuzzySearch.FuzzyMatch(sq, CleanString(item.label), ref score, matches))
    item.label = RichTextFormatter.FormatSuggestionTitle(item.label, matches);

所有搜索项都根据同一提供程序的项进行排序,并使用其score.较低的分数显示在项目列表的顶部(升序排序)。

异步搜索 API

您可以使用异步fetchItems当搜索提供程序需要很长时间来计算其结果,或者依赖异步搜索引擎(如 WebRequests)时,API 的请求。

要使用异步 API,请将fetchItems函数返回一个IEnumerable<SearchItem>.这IEnumerable<SearchItem>应该是一个产生结果的函数,以便 API 可以一次获取一个项目。

IEnumerable<SearchItem>返回时,枚举器在应用程序更新期间被存储和循环访问。枚举会继续进行多个应用程序更新,直到完成。

迭代时间受到限制,以确保 UI 不会被阻塞。但是,由于调用在主线程中,因此如果结果尚未准备好,您应该确保尽快让步。

以下示例演示如何使用异步fetchItems应用程序接口:

public 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;
        }
    }
}
注册搜索提供程序
注册作处理程序