검색 제공자는 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는 SearchItem를 items 목록에 새로 추가하거나 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에 따라 정렬됩니다. 점수가 낮을수록 항목 목록 상단에 표시됩니다(오름차순 정렬).
검색 제공자가 결과를 계산하는 데 시간이 오래 걸리거나 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 검색 인덱서를 시작하는 프로세스를 생성합니다.