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

SearchProvider.priority

建议更改

成功!

感谢帮助我们提升 Unity 文档质量。尽管无法接受所有提交,但我们会阅读用户提出的每条建议,并会在适当情况下进行更新。

关闭

提交失败

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

关闭

取消

public int priority;

说明

用于对搜索提供程序进行排序的提示。会影响搜索结果以及搜索提供程序在 FilterWindow 中显示的顺序。

最低优先级排在最前。

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

static class SearchProvider_priority
{
    static string typeColors = "example_colors_priority";
    static string displayNameColors = "example_Colors_priority";
    static string typeFruits = "example_fruits_priority";
    static string displayNameFruits = "example_Fruits_priority";

    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)
        {
            priority = 99991,
            filterId = "c:",
            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)
        {
            priority = 99992,
            filterId = "f:",
            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/priority")]
    public static void Run()
    {
        SearchService.SetActive(typeColors);
        SearchService.SetActive(typeFruits);

        using (var context = SearchService.CreateContext("orange"))
        {
            // For the purpose if this example, we use the flag synchronous to get the items immediately.
            var results = SearchService.GetItems(context, SearchFlags.Synchronous);
            // Color should be the first one (both items have the same score but color provider has the lowest priority).
            Debug.Log(results.Count);
            Debug.Log(results[0].description); // "orange(color)";
            Debug.Log(results[1].description); // "orange(fruit)";
        }
    }
}