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

SearchProvider.active

建议更改

成功!

感谢您帮助我们改善 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每一条建议更改,并在适用时进行更新。

关闭

提交失败

由于某种原因,无法提交您建议的更改。请在几分钟后 </a>重试。感谢您花时间帮助我们改善 Unity 文档的质量。

关闭

取消

public bool active;

描述

指示搜索提供程序是否处于活动状态。搜索服务将忽略处于非活动状态的搜索提供程序。可在搜索设置中切换活动状态。

using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.Search;
using UnityEngine;

static class SearchProvider_active
{
    static string typeColors = "example_colors_active";
    static string displayNameColors = "example_Colors_active";
    static string typeFruits = "example_fruits_active";
    static string displayNameFruits = "example_Fruits_active";

    static List<string> colors = new List<string> { "orange", "red", "green", "blue" };
    static List<string> fruits = new List<string> { "orange", "apple", "banana", "strawberry" };

    [SearchItemProvider]
    internal static SearchProvider CreateProviderColors()
    {
        return new SearchProvider(typeColors, displayNameColors)
        {
            filterId = "c:",
            priority = 99999, // put example provider at a low priority
            isExplicitProvider = false,
            fetchItems = (context, items, provider) =>
            {
                var expression = context.searchQuery;
                if (colors.Contains(expression))
                {
                    var id = expression + "(color)";
                    items.Add(provider.CreateItem(context, id, id, id, null, null));
                }
                return null;
            }
        };
    }

    [SearchItemProvider]
    internal static SearchProvider CreateProviderFruits()
    {
        return new SearchProvider(typeFruits, displayNameFruits)
        {
            filterId = "f:",
            priority = 99999, // put example provider at a low priority
            isExplicitProvider = false,
            fetchItems = (context, items, provider) =>
            {
                var expression = context.searchQuery;
                if (fruits.Contains(expression))
                {
                    var id = expression + "(fruit)";
                    items.Add(provider.CreateItem(context, id, id, id, null, null));
                }
                return null;
            }
        };
    }

    [MenuItem("Examples/SearchProvider/active")]
    public static void Run()
    {
        var colorProvider = SearchService.GetProvider(typeColors);
        colorProvider.active = false;

        var context = SearchService.CreateContext("orange");

        // The providers used in the context should not contain the inactive provider
        var sb = new StringBuilder();
        foreach (var provider in context.providers)
            sb.AppendLine(provider.name);
        Debug.Log(sb.ToString());
    }
}