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

QueryEngine<T0>.GetAllFilters

提出变更建议

成功!

感谢您帮助我们提高 Unity 文档的质量。尽管我们无法接受所有反馈,但我们会阅读用户提出的每项变更建议,并在适当情况下进行更新。

关闭

提交失败

由于某种原因,您提出的变更建议无法提交。请在几分钟后<a>重试</a>。感谢您花时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public IEnumerable<IQueryEngineFilter> GetAllFilters();

返回

IEnumerable<IQueryEngineFilter> 可枚举 IQueryEngineFilter

说明

获取添加在此 QueryEngine 中的所有过滤器。

此方法返回添加到 QueryEngine 中的所有过滤器。

using System.Linq;
using UnityEditor;
using UnityEditor.Search;
using UnityEngine;

static class Example_QueryEngine_GetAllFilters
{
    static QueryEngine<MyObjectType> SetupQueryEngine()
    {
        // Set up the query engine
        var queryEngine = new QueryEngine<MyObjectType>();

        // 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[] { "=", "!=", "<", ">", "<=", ">=" });

        // Set up what data from objects of type MyObjectType will be matched against search words
        queryEngine.SetSearchDataCallback(myObj => new[] { myObj.id.ToString(), myObj.name });

        return queryEngine;
    }

    [MenuItem("Examples/QueryEngine/GetAllFilters")]
    public static void RunExample()
    {
        var queryEngine = SetupQueryEngine();

        var allFilters = queryEngine.GetAllFilters();
        foreach (var filter in allFilters)
        {
            Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, $"Filter: {filter.token} - Supported operators: {(filter.supportedOperators?.Any() ?? false ? "[" + string.Join(", ", filter.supportedOperators) + "]" : "All")}");
        }

        // Get the filter corresponding to the token "id"
        if (!queryEngine.TryGetFilter("id", out var idFilter))
            Debug.LogError("The filter \"id\" should have been found.");

        Debug.Assert(idFilter != null, "Filter \"id\" should not be null.");

        // Remove the filter with token "id"
        var token = "id";
        queryEngine.RemoveFilter("id");

        var found = queryEngine.TryGetFilter(token, out idFilter);
        Debug.Assert(found == false, "Filter \"id\" should not be found.");
        Debug.Assert(idFilter == null, "Filter \"id\" should be null.");
    }

    /// <summary>
    /// Custom type. This is the type of objects that will be searched by the QueryEngine.
    /// </summary>
    class MyObjectType
    {
        public int id { get; set; }
        public string name { get; set; }
        public Vector2 position { get; set; }
        public bool active { get; set; }

        public MyObjectType()
        {
            id = 0;
            name = "";
            position = Vector2.zero;
            active = false;
        }

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