Version: 2022.3
LanguageEnglish
  • C#

SearchProvider.id

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

public string id;

Description

Search provider unique ID.

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

static class SearchProvider_id
{
    internal static string id = "example_colors";
    internal static string name = "example_Colors";

    [SearchItemProvider]
    internal static SearchProvider CreateProvider()
    {
        return new SearchProvider(id, name)
        {
            filterId = "hex:",
            priority = 99999, // put example provider at a low priority
            showDetailsOptions = ShowDetailsOptions.Description | ShowDetailsOptions.Preview,
            fetchItems = (context, items, provider) =>
            {
                var expression = context.searchQuery;
                if (expression.Length == 6 && IsHex(expression))
                {
                    expression = "#" + expression;
                    items.Add(provider.CreateItem(context, expression, GetColorName(expression),
                        "Look at this " + GetColorName(expression) + " color!",
                        CreateTextureFromColor(expression, 64, 64), null));
                }
                return null;
            },
            fetchPreview = (item, context, size, options) =>
            {
                return CreateTextureFromColor(item.id, (int)size.x, (int)size.y);
            },
        };
    }


    [MenuItem("Examples/SearchProvider/id")]
    public static void Run()
    {
        var context = SearchService.CreateContext(id);

        Debug.Log(context.providers.First().id);
    }

    private static Texture2D CreateTextureFromColor(string color, int width, int height)
    {
        Color fillColor;
        if (!ColorUtility.TryParseHtmlString(color, out fillColor))
            return null;
        var texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
        var fillColorArray = texture.GetPixels32();

        for (var i = 0; i < fillColorArray.Length; ++i)
            fillColorArray[i] = fillColor;

        texture.SetPixels32(fillColorArray);

        texture.Apply();
        return texture;
    }

    private static bool IsHex(string expression)
    {
        foreach (var c in expression)
        {
            if (!Uri.IsHexDigit(c))
                return false;
        }
        return true;
    }

    private static string GetColorName(string color)
    {
        if (color[1] > color[3] && color[1] > color[5])
            return "reddish";
        else if (color[3] > color[1] && color[3] > color[5])
            return "greenish";
        else if (color[5] > color[1] && color[5] > color[3])
            return "bluish";
        return "undefined";
    }
}
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961