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(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary 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 StoreA growing library of free and commercial assets created by Unity and members of the community. Offers a wide variety of assets, from textures, models and animations to whole project examples, tutorials and Editor extensions. More infoESS.cs
: creates a process to start the Entrian Source search indexer, which provides full text search for Assets in your Project.When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.