Search Providers use the fetchItems
function to search for items and filter the results. The fetchItems
function has the following signature:
// 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);
The SearchProvider
must add new SearchItem
s to the items
list or return an IEnumerable<SearchItem>
.
Note: If you do not use the asynchronous
fetchItems
API, you must returnnull
in yourfetchItems
function.
A SearchItem
is a simple struct:
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;
}
A SearchItem
only requires the id
.
Tip: When you filter according to
SearchContext.searchText
use the static functionSearchProvider.MatchSearchGroup
which makes a partial search.
To use fuzzy search on an item, you can use FuzzySearch.FuzzyMatch
, as in the following example:
if (FuzzySearch.FuzzyMatch(sq, CleanString(item.label), ref score, matches))
item.label = RichTextFormatter.FormatSuggestionTitle(item.label, matches);
All search items are sorted against items of the same provider with their score
. The lower score appears at the top of the item list (ascending sorting).
You can use the asynchronous fetchItems
API when a Search Provider takes a long time to compute its results, or relies on an asynchronous search engine such as WebRequests.
To use the asynchronous API, have the fetchItems
function return an IEnumerable<SearchItem>
. The IEnumerable<SearchItem>
should be a function that yields results, so that the API can fetch one item at a time.
When an IEnumerable<SearchItem>
is returned, the enumerator is stored and iterated over during an application update. Enumeration continues over multiple application updates until it is finished.
The iterating time is constrained to ensure the UI is not blocked. However, because the call is in the main thread, you should make sure to yield as soon as possible if the results are not ready.
The following example demonstrates how to use the asynchronous 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
: queries the Asset Store using WebRequest.ESS.cs
: creates a process to start the Entrian Source search indexer, which provides full text search for Assets in your Project.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.