Version: 2023.1
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(SearchService.GetProvider(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";
    }
}