검색 공급자는 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
는 items
리스트에 새로운 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
가 있는 동일한 공급자의 아이템을 기준으로 정렬됩니다.낮은 점수는 아이템 리스트 상단에 나타납니다(오름차순 정렬).
검색 공급자가 결과를 계산하는 데 시간이 오래 걸리거나 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 검색 인덱서를 시작하는 프로세스를 생성합니다.Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.