token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and returns an object of type TFilter. |
stringComparison | String comparison option. |
supportedOperatorType | List of supported operator tokens. Null for all operators. |
Adds a new custom filter.
<TData>: The type of the data that is filtered by the QueryEngine.
<TFilter>: The type of the data that is compared by the filter.
Use this overload to register basic filters if you don't need to handle the operators yourself.
// Add a filter for MyObjectType.id that supports all operators queryEngine.AddFilter("id", myObj => myObj.id); // Add a filter for MyObjectType.name that supports only contains (:), equal (=) and not equal (!=) queryEngine.AddFilter("n", myObj => myObj.name, new[] { ":", "=", "!=" }); // Add a filter for MyObjectType.active that supports only equal and not equal queryEngine.AddFilter("a", myObj => myObj.active, new[] { "=", "!=" }); // Add a filter for the computed property magnitude that supports equal, not equal, lesser, greater, lesser or equal and greater or equal. queryEngine.AddFilter("m", myObj => myObj.position.magnitude, new[] { "=", "!=", "<", ">", "<=", ">=" });
For a complete example see QueryEngine.
token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and TParam, and returns an object of type TFilter. |
parameterTransformer | Callback used to convert a string to type TParam. Used when parsing the query to convert what is passed to the function into the correct format. |
stringComparison | String comparison option. |
supportedOperatorType | List of supported operator tokens. Null for all operators. |
Adds a new custom filter with parameters.
<TData>: The type of the data that is filtered by the QueryEngine.
<TFilter>: The type of the data that is compared by the filter.
<TParam>: The type of the data that is passed to the filter.
Use this overload to register filters that accept parameters if you don't need to handle the operators yourself.
using System.Collections.Generic; using System.Globalization; using System.Linq; using UnityEditor; using UnityEditor.Search; using UnityEngine; static class Example_QueryEngine_AddFilter_Param { static List<MyObjectType> s_Data; [MenuItem("Examples/QueryEngine/AddFilter_Param")] public static void RunExample() { // Set up the query engine var queryEngine = new QueryEngine<MyObjectType>(); queryEngine.SetSearchDataCallback(myObj => new[] { myObj.id.ToString(), myObj.name }); // Add a "dist" filter to filter items based on a specified distance from a position or from another item queryEngine.AddFilter("dist", (myObj, paramPosition) => { // Compute the distance from the position that was retrieved in the parameter transformer var vec = myObj.position - paramPosition; return vec.magnitude; }, paramStringValue => { // Transform the parameter from a string to a usable Vector2 // If the user specified a vector if (paramStringValue.StartsWith("[") && paramStringValue.EndsWith("]")) { paramStringValue = paramStringValue.Trim('[', ']'); var vectorTokens = paramStringValue.Split(','); var vectorValues = vectorTokens.Select(token => float.Parse(token, CultureInfo.InvariantCulture.NumberFormat)).ToList(); return new Vector2(vectorValues[0], vectorValues[1]); } // Treat the value as the name of an object var myObj = s_Data.Find(obj => obj.name == paramStringValue); return myObj.position; }, new[] { "=", "!=", "<", ">", "<=", ">=" }); s_Data = new List<MyObjectType>() { new MyObjectType { id = 0, name = "Test 1", position = new Vector2(0, 0), active = false }, new MyObjectType { id = 1, name = "Test 2", position = new Vector2(0, 1), active = true }, new MyObjectType { id = 2, name = "Test 3", position = new Vector2(1, 0), active = false }, new MyObjectType { id = 3, name = "Test 4", position = new Vector2(1.2f, 0), active = false }, }; // Find all items that are at a distance of 1 or greater to [0, 0] var query = queryEngine.Parse("dist([0, 0])>=1"); var filteredData = query.Apply(s_Data).ToList(); Debug.Assert(filteredData.Count == 3, $"There should be 3 items in the filtered list but found {filteredData.Count} items."); Debug.Assert(!filteredData.Contains(s_Data[0]), "Test 1 should not be in the list as its distance to [0, 0] is < 1."); // Find all items that are at a distance smaller than 1 to "Test 3", but not "Test 3" itself query = queryEngine.Parse("dist(Test 3)<1 -\"Test 3\""); 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[3]), "Test 4 should be in the list as its distance to Test 3 is < 1."); } 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 override string ToString() { return $"({id}, {name}, ({position.x}, {position.y}), {active})"; } } }
token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
filterResolver | Callback used to handle any operators for this filter. Takes an object of type TData, the operator token and the filter value, and returns a boolean indicating if the filter passed or not. |
supportedOperatorType | List of supported operator tokens. Null for all operators. |
Adds a new custom filter with a complete resolver.
<TData>: The type of the data that is filtered by the QueryEngine.
<TFilter>: The type of the data that is compared by the filter.
Use this overload to register basic filters if you need to handle the operators yourself.
using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEditor.Search; using UnityEngine; static class Example_QueryEngine_AddFilter_Resolver { static List<MyObjectType> s_Data; [MenuItem("Examples/QueryEngine/AddFilter_Resolver")] public static void RunExample() { // Set up the query engine var queryEngine = new QueryEngine<MyObjectType>(); queryEngine.SetSearchDataCallback(myObj => new[] { myObj.id.ToString(), myObj.name }); // Add a custom filter "is", with a resolver that will handle all operators queryEngine.AddFilter<string>("is", (myObj, operatorToken, filterValue) => { if (operatorToken != ":") return false; switch (filterValue) { case "active": return myObj.active; case "root": return myObj.id == 0; case "center": return myObj.position == Vector2.zero; default: return false; } }, new[] {":"}); s_Data = new List<MyObjectType>() { new MyObjectType { id = 0, name = "Test 1", position = new Vector2(0, 0), active = false }, new MyObjectType { id = 1, name = "Test 2", position = new Vector2(0, 1), active = true }, new MyObjectType { id = 2, name = "Test 3", position = new Vector2(1, 0), active = false }, new MyObjectType { id = 3, name = "Test 4", position = new Vector2(1.2f, 0), active = false }, }; // Find all items that are of type "active" var query = queryEngine.Parse("is:active"); 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[1]), "Test 2 should be in the list as it is active."); // Find all items that are root query = queryEngine.Parse("is:root"); 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]), "Test 1 should be in the list as it is a root."); } 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 override string ToString() { return $"({id}, {name}, ({position.x}, {position.y}), {active})"; } } }
token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
filterResolver | Callback used to handle any operators for this filter. Takes an object of type TData, an object of type TParam, the operator token and the filter value, and returns a boolean indicating if the filter passed or not. |
parameterTransformer | Callback used to convert a string to type TParam. Used when parsing the query to convert what is passed to the function into the correct format. |
supportedOperatorType | List of supported operator tokens. Null for all operators. |
Adds a new custom filter with parameters and a complete resolver.
<TData>: The type of the data that is filtered by the QueryEngine.
<TFilter>: The type of the data that is compared by the filter.
<TParam>: The type of the data that is passed to the filter.
Use this overload to register filters that accept parameters if you need to handle the operators yourself.
using System.Collections.Generic; using System.Globalization; using System.Linq; using UnityEditor; using UnityEditor.Search; using UnityEngine; static class Example_QueryEngine_AddFilter_Param_Resolver { static List<MyObjectType> s_Data; [MenuItem("Examples/QueryEngine/AddFilter_Param_Resolver")] public static void RunExample() { // Set up the query engine var queryEngine = new QueryEngine<MyObjectType>(); queryEngine.SetSearchDataCallback(myObj => new[] { myObj.id.ToString(), myObj.name }); // Add a "dist" filter to filter items based on a specified distance from a position or from another item, and handle all operators queryEngine.AddFilter<Vector2, string>("dist", (myObj, paramPosition, operatorToken, filterValue) => { var distance = (myObj.position - paramPosition).magnitude; if (operatorToken == ":") { switch (filterValue) { case "near": return distance <= 10.0f; case "far": return distance > 10.0f; } return false; } var floatValue = float.Parse(filterValue, CultureInfo.InvariantCulture.NumberFormat); switch (operatorToken) { case "=": return distance == floatValue; case "!=": return distance != floatValue; case "<": return distance < floatValue; case "<=": return distance <= floatValue; case ">": return distance > floatValue; case ">=": return distance >= floatValue; } return false; }, paramStringValue => { // Transform the parameter from a string to a usable Vector2 // If the user specified a vector if (paramStringValue.StartsWith("[") && paramStringValue.EndsWith("]")) { paramStringValue = paramStringValue.Trim('[', ']'); var vectorTokens = paramStringValue.Split(','); var vectorValues = vectorTokens.Select(token => float.Parse(token, CultureInfo.InvariantCulture.NumberFormat)).ToList(); return new Vector2(vectorValues[0], vectorValues[1]); } // Treat the value as the name of an object var myObj = s_Data.Find(obj => obj.name == paramStringValue); return myObj.position; }); s_Data = new List<MyObjectType>() { new MyObjectType { id = 0, name = "Test 1", position = new Vector2(0, 0), active = false }, new MyObjectType { id = 1, name = "Test 2", position = new Vector2(0, 10), active = true }, new MyObjectType { id = 2, name = "Test 3", position = new Vector2(10, 0), active = false }, new MyObjectType { id = 3, name = "Test 4", position = new Vector2(12f, 0), active = false }, }; // Find all items that are "near" "Test 1", including "Test 1" var query = queryEngine.Parse("dist(Test 1):near"); var filteredData = query.Apply(s_Data).ToList(); Debug.Assert(filteredData.Count == 3, $"There should be 3 items in the filtered list but found {filteredData.Count} items."); Debug.Assert(!filteredData.Contains(s_Data[3]), "Test 4 should not be in the list as it is far of Test 1."); // Find all items that are far of [0, 0] query = queryEngine.Parse("dist([0, 0]):far"); 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[3]), "Test 4 should be in the list as it is far of [0, 0]."); } 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 override string ToString() { return $"({id}, {name}, ({position.x}, {position.y}), {active})"; } } }