Version: 2021.1
LanguageEnglish
  • C#

QueryEngine<T0>.AddFiltersFromAttribute

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

Declaration

public void AddFiltersFromAttribute();

Description

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})";
        }
    }
}