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

SearchExpressionEvaluatorAttribute

UnityEditor.Search 类中的类

建议更改

成功!

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

关闭

提交失败

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

关闭

取消

描述

用于注册新的 SearchExpressionEvaluator 的属性。这将允许在 SearchExpression 中使用新函数。作为补充说明,所有内置评估器(count{}、select{} 等)都是使用此属性定义的。

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

public class SearchExpressionEvaluator_Example
{
    [SearchExpressionEvaluator(SearchExpressionType.Iterable | SearchExpressionType.Variadic)]
    [SearchExpressionEvaluatorSignatureOverload(SearchExpressionType.Number, SearchExpressionType.Iterable | SearchExpressionType.Variadic)]
    [Description("Extract and returns the X first results for each expression.")]
    public static IEnumerable<SearchItem> TakeXFirst(SearchExpressionContext c)
    {
        var argIndex = 0;
        var takeNumber = 1;
        if (c.args[0].types.HasFlag(SearchExpressionType.Number))
        {
            ++argIndex;
            takeNumber = Math.Max((int)(c.args[0].GetNumberValue(takeNumber)), 0);
        }

        for ( ; argIndex < c.args.Length; ++argIndex)
        {
            var iterable = c.args[argIndex].Execute(c);
            var taken = 0;
            foreach (var item in iterable)
            {
                if (item == null)
                    yield return null;
                else
                {
                    yield return item;
                    ++taken;
                    if (taken == takeNumber)
                    {
                        c.Break();
                        break;
                    }
                }
            }
        }
    }


    [MenuItem("Examples/ExpressionEvaluator/Take First")]
    static void TestTakeFirst()
    {
        var ctx = SearchService.CreateContext("takexfirst{[1,2,3,4], [34, 45, 66], t:script}");
        SearchService.ShowWindow(ctx);
    }

    [MenuItem("Examples/ExpressionEvaluator/Take 2 First")]
    static void TestTake2First()
    {
        var ctx = SearchService.CreateContext("takexfirst{2, [1,2,3,4], [34, 45, 66], t:script}");
        SearchService.ShowWindow(ctx);
    }

    [Description("Returns ids of current selection")]
    [SearchExpressionEvaluator(SearchExpressionEvaluationHints.ThreadNotSupported)]
    public static IEnumerable<SearchItem> SelectionIds(SearchExpressionContext c)
    {
        var instanceIds = UnityEditor.Selection.instanceIDs;
        foreach (var id in instanceIds)
        {
            yield return SearchExpression.CreateItem(id, c.ResolveAlias("selected id"));
        }
    }

    [MenuItem("Examples/ExpressionEvaluator/Selection ids")]
    static void SelectionIds()
    {
        var ctx = SearchService.CreateContext("selectionids{}");
        SearchService.ShowWindow(ctx);
    }

    [Description("Returns asset paths corresponding to a list of instance ids")]
    [SearchExpressionEvaluator("IdsToPaths", SearchExpressionEvaluationHints.ThreadNotSupported, SearchExpressionType.Iterable)]
    public static IEnumerable<SearchItem> IdsToPath(SearchExpressionContext c)
    {
        foreach (var idItem in c.args[0].Execute(c))
        {
            if (SearchExpression.TryConvertToDouble(idItem, out var idNum))
            {
                var id = (int)idNum;
                var path = AssetDatabase.GetAssetPath(id);
                if (!string.IsNullOrEmpty(path))
                {
                    yield return SearchExpression.CreateItem(path, c.ResolveAlias("asset path"));
                }
            }
        }
    }

    [MenuItem("Examples/ExpressionEvaluator/Selection ids to Paths")]
    static void IdsToPaths()
    {
        var ctx = SearchService.CreateContext("IdsToPaths{selectionids{}}");
        SearchService.ShowWindow(ctx);
    }

    [Description("Convert arguments to a string allowing you to format the result.")]
    [SearchExpressionEvaluator(SearchExpressionType.Selector | SearchExpressionType.Text, SearchExpressionType.Iterable | SearchExpressionType.Literal | SearchExpressionType.Variadic)]
    [SearchExpressionEvaluatorSignatureOverload(SearchExpressionType.Iterable | SearchExpressionType.Literal | SearchExpressionType.Variadic)]
    public static IEnumerable<SearchItem> FormatItems(SearchExpressionContext c)
    {
        var skipCount = 0;
        if (SearchExpression.GetFormatString(c.args[0], out var formatStr))
            skipCount++;
        var items = c.args.Skip(skipCount).SelectMany(e => e.Execute(c));
        var dataSet = SearchExpression.ProcessValues(items, null, item => SearchExpression.FormatItem(c.search, item, formatStr));
        return dataSet;
    }

    [MenuItem("Examples/ExpressionEvaluator/Get Script Paths")]
    static void GetScriptPaths()
    {
        var ctx = SearchService.CreateContext("formatitems{t:script search}");
        SearchService.ShowWindow(ctx);
    }
}

构造函数

SearchExpressionEvaluatorAttribute使用此属性将静态 C# 函数注册为新的评估器。SearchExpressionEvaluator 在 SearchExpression 中使用时,可以具有针对传递参数进行验证的签名。