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})"; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.