Version: 2021.1
언어: 한국어
검색 공급자 등록
행동 핸들러 등록

검색 수행

검색 공급자는 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);

SearchProvideritems 리스트에 새로운 SearchItem을 추가하거나 IEnumerable<SearchItem>을 반환해야 합니다.

참고: 비동기 fetchItems API를 사용하지 않는 경우 fetchItems 함수에서 null을 반환해야 합니다.

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에 따라 필터링할 때 부분 검색을 생성하는 정적 함수 SearchProvider.MatchSearchGroup를 사용합니다.

퍼지 검색 사용

아이템에 퍼지 검색을 사용하려면 다음 예와 같이 FuzzySearch.FuzzyMatch를 사용할 수 있습니다.

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

모든 검색 아이템은 score가 있는 동일한 공급자의 아이템을 기준으로 정렬됩니다.낮은 점수는 아이템 리스트 상단에 나타납니다(오름차순 정렬).

비동기 검색 API

검색 공급자가 결과를 계산하는 데 시간이 오래 걸리거나 WebRequests와 같은 비동기 검색 엔진에 의존하는 경우 비동기 fetchItems API를 사용할 수 있습니다.

비동기 API를 사용하려면 fetchItems 함수가 IEnumerable<SearchItem>을 반환하도록 합니다. IEnumerable<SearchItem>은 API가 한 번에 한 아이템씩 가져올 수 있도록 결과를 산출하는 함수여야 합니다.

IEnumerable<SearchItem>이 반환되면 열거자는 애플리케이션 업데이트 중에 저장하고 반복합니다. 완료될 때까지 여러 애플리케이션 업데이트에 대해 열거가 계속됩니다.

UI가 차단되지 않도록 반복 시간이 제한됩니다. 그러나 메인 스레드에서 호출할 수 있으므로, 결과가 준비되지 않은 경우 최대한 빨리 산출해야 합니다.

다음 예제는 비동기 fetchItems API를 사용하는 방법을 보여줍니다.

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;
        }
    }
}
  • AssetStoreProvider.cs: WebRequest를 사용하여 에셋 스토어를 쿼리합니다.
  • ESS.cs: 프로젝트의 에셋에 대해 전체 텍스트 검색을 제공하는 Entrian Source 검색 인덱서를 시작하는 프로세스를 생성합니다.
검색 공급자 등록
행동 핸들러 등록