searchText | 要执行的搜索查询。 |
context | 用于跟踪异步请求的搜索上下文。您需要自行释放上下文。 |
options | 定义如何执行查询的选项。 |
ISearchList 搜索项目的异步列表。
执行搜索请求,该请求将异步获取搜索结果。
以下示例执行查询并使用 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); } }
如果您正在运行协程,您可以像下面这样生成结果
public static IEnumerable<SearchItem> YieldResults() { ISearchList results = SearchService.Request("*.cs"); foreach (var result in results) yield return result; }
onSearchCompleted | 搜索请求完成后且所有结果都可用时调用的回调。 |
执行搜索请求,并在所有结果都可用时回调指定的函数。
[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 | 每次找到并可用一批结果时调用的回调。 |
onSearchCompleted | 搜索请求完成后调用的回调。 |
执行搜索请求并为每一批传入的结果回调。可能会获取重复项,因此如果需要,请过滤最终列表。
[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); }