版本: Unity 6 (6000.0)
语言English
  • C#

SearchProvider.isExplicitProvider

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实阅读了用户提出的每个建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请 <a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

public bool isExplicitProvider;

描述

只有在使用 filterId 显式指定时,此搜索提供程序才会处于活动状态。

using UnityEditor;
using UnityEditor.Search;
using UnityEngine;

static class SearchProvider_isExplicitProvider
{
    internal static string type = "example_uppercase_isExplicitProvider";
    internal static string displayName = "example_UpperCase_isExplicitProvider";

    [SearchItemProvider]
    internal static SearchProvider CreateProvider()
    {
        return new SearchProvider(type, displayName)
        {
            filterId = "+",
            priority = 99999, // put example provider at a low priority
            isExplicitProvider = true,
            fetchItems = (context, items, provider) =>
            {
                var expression = context.searchQuery;
                expression += " -> " + expression.ToUpper();

                items.Add(provider.CreateItem(context, context.searchQuery.ToUpper(), context.searchQuery.ToUpper(), expression, null, null));
                return null;
            }
        };
    }

    [MenuItem("Examples/SearchProvider/isExplicitProvider")]
    public static void Run()
    {
        SearchService.SetActive(type);
        using (var context = SearchService.CreateContext(""))
        {
            // For the context of this example, we set the flag synchronous so we can get the results right away.
            context.options |= SearchFlags.Synchronous;
            // If we don't specify the filterId for an explicit provider, there will be no results.
            context.searchText = "uppercase String";

            var results = SearchService.GetItems(context);
            Debug.Log(results.Count); // 0;

            // Use the filterId for an explicit provider
            context.searchText = "+uppercase String";
            results = SearchService.GetItems(context);
            // There should be only one result with that specific description
            Debug.Log(results.Count); // 1;
            Debug.Log(results[0].description); // "uppercase String -> UPPERCASE STRING";
        }
    }
}