Version: 2021.1

QueryEngine<T0>.AddFiltersFromAttribute

切换到手册
public void AddFiltersFromAttribute ();

描述

Adds all custom filters that are identified with the method attribute TFilterAttribute.

Allows you to register a filter with a specific type.

<TFilterAttribute>: The type of the attribute defined for your custom filters.

<TTransformerAttribute>: The type of the attribute defined for your custom parameter transformers.

For more information about the custom attributes, see QueryEngineFilterAttribute and QueryEngineParameterTransformerAttribute.

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

static class Example_QueryEngine_AddFilterFromAttribute
{
    static List<MyObjectType> s_Data;

    [MenuItem("Examples/QueryEngine/AddFiltersFromAttribute")]
    public static void RunExample()
    {
        // Set up the query engine
        var queryEngine = new QueryEngine<MyObjectType>();
        queryEngine.AddFilter("id", myObj => myObj.id);
        queryEngine.SetSearchDataCallback(myObj => new[] { myObj.id.ToString(), myObj.name });

        // Register filters with our own attributes
        queryEngine.AddFiltersFromAttribute<MyObjectFilterAttribute, MyObjectFilterParameterTransformerAttribute>();

        s_Data = new List<MyObjectType>()
        {
            new MyObjectType { id = 0, name = "Test 1", values = new float[] {2, 20, 42} },
            new MyObjectType { id = 1, name = "Test 2", values = new float[] {16, 64, 128} }
        };

        // Find all items with values at index 1 lower or equal to 32
        var query = queryEngine.Parse("v(1)<=32");
        var filteredData = query.Apply(s_Data).ToList();
        Debug.Assert(filteredData.Count == 1, $"There should be 1 item in the filtered list but found {filteredData.Count} items.");
        Debug.Assert(filteredData.Contains(s_Data[0]), "The first item should be in the list.");
    }

    // Define a filter for a "value" property
    [MyObjectFilter("v", "ParseFilterValuesIndex")]
    static float FilterValues(MyObjectType myObj, int index)
    {
        return myObj.values[index];
    }

    // Define the parameter transformer for the filter "FilterValues"
    [MyObjectFilterParameterTransformer]
    static int ParseFilterValuesIndex(string param)
    {
        if (int.TryParse(param, NumberStyles.Number, new NumberFormatInfo(), out var i))
            return i;
        return 0;
    }

    // Create a new attribute in order to register filters of this type only
    class MyObjectFilterAttribute : QueryEngineFilterAttribute
    {
        public MyObjectFilterAttribute(string token, string[] supportedOperators = null)
            : base(token, supportedOperators) {}

        public MyObjectFilterAttribute(string token, StringComparison options, string[] supportedOperators = null)
            : base(token, options, supportedOperators) {}

        public MyObjectFilterAttribute(string token, string paramTransformerFunction, string[] supportedOperators = null)
            : base(token, paramTransformerFunction, supportedOperators) {}

        public MyObjectFilterAttribute(string token, string paramTransformerFunction, StringComparison options, string[] supportedOperators = null)
            : base(token, paramTransformerFunction, options, supportedOperators) {}
    }

    class MyObjectFilterParameterTransformerAttribute : QueryEngineParameterTransformerAttribute {}

    class MyObjectType
    {
        public int id { get; set; }
        public string name { get; set; } = string.Empty;
        public Vector2 position { get; set; } = Vector2.zero;
        public bool active { get; set; }
        public float[] values { get; set; } = new float[0];

        public override string ToString()
        {
            return $"({id}, {name}, ({position.x}, {position.y}), {active})";
        }
    }
}

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961