Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
ClosesearchText | Search query to be executed. |
context | Search context used to track asynchronous requests. You need to dispose of the context yourself. |
options | Options defining how the query is performed. |
ISearchList Asynchronous list of search items.
Executes a search request that will fetch search results asynchronously.
The following example executes a query and print results over many frames using EditorApplication.update.
[MenuItem("Examples/SearchService/Request List")] public static void RequestList() { ISearchList results = SearchService.Request("*.cs"); // It is important to note that when you request some search results, // that you need to enumerate them asynchronously using the returned search list. AsyncResultEnumerator.Fetch(results, item => Debug.Log(item)); } struct AsyncResultEnumerator { private Action<SearchItem> m_OnEnumerate; private IEnumerator<SearchItem> m_Iterator; public static AsyncResultEnumerator Fetch(ISearchList results, Action<SearchItem> onEnumerate) { return new AsyncResultEnumerator(results, onEnumerate); } public AsyncResultEnumerator(ISearchList results, Action<SearchItem> onEnumerate) { m_OnEnumerate = onEnumerate; m_Iterator = results.GetEnumerator(); EditorApplication.update += EnumerateResults; } private void EnumerateResults() { if (m_Iterator == null || !m_Iterator.MoveNext()) { m_Iterator = null; EditorApplication.update -= EnumerateResults; } else if (m_Iterator.Current != null) m_OnEnumerate(m_Iterator.Current); } }
If you are running a coroutine you can yield results like the following:
public static IEnumerable<SearchItem> YieldResults() { ISearchList results = SearchService.Request("*.cs"); foreach (var result in results) yield return result; }
onSearchCompleted | Callback invoked when the search request is completed and all results are available. |
Executes a search request and calls back the specified function when all results are available.
[MenuItem("Examples/SearchService/Request All")] public static void RequestAll() { SearchService.Request("t:texture", (SearchContext context, IList<SearchItem> items) => { Debug.Log("All Textures"); foreach (var item in items) Debug.Log(item); }, SearchFlags.Debug); }
onIncomingItems | Callback invoked everytime a batch of results are found and available. |
onSearchCompleted | Callback invoked when the search request is completed. |
Executes a search request and callbacks for every batch of incoming results. It is possible to get duplicate items, so filter the final list if needed.
[MenuItem("Examples/SearchService/Request Async")] public static void RequestAsync() { var batchCount = 0; var totalItemCount = 0; void OnIncomingResults(SearchContext context, IEnumerable<SearchItem> items) { var batchItemCount = items.Count(); totalItemCount += batchItemCount; Debug.Log($"#{++batchCount} Incoming materials ({batchItemCount}): {string.Join("\n", items)}"); } void OnSearchCompleted(SearchContext context) { Debug.Log($"Query <b>\"{context.searchText}\"</b> completed with a total of {totalItemCount}"); } SearchService.Request("t:material", OnIncomingResults, OnSearchCompleted, SearchFlags.Debug); }
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.