Version: 2021.3
public Query<TData> Parse (string text, bool useFastYieldingQueryHandler);

参数

text The query input string.
useFastYieldingQueryHandler Set to true to get a query that yields null results for elements that don't pass the query, instead of only the elements that pass the query.

返回

Query<TData> Query operation of type TData.

描述

Parses a query string into a Query operation. This Query operation can then be used to filter any data set of type TData.

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

static class Example_QueryEngine_Parse
{
    static List<MyObjectType> s_Data;

    [MenuItem("Examples/QueryEngine/Parse")]
    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 });

        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 = true },
        };

        // Call parse to get a query that can be applied on any data set that matches the QueryEngine's type
        var query = queryEngine.Parse("id<2");

        // You can test the validity of the query, and check for any parsing errors
        if (!query.valid)
        {
            foreach (var queryError in query.errors)
            {
                Debug.LogError($"Parsing error at ${queryError.index}: {queryError.reason}.");
            }
        }

        // Use this query to filter a data set.
        var filteredData = query.Apply(s_Data).ToList();

        // FilteredData now contains only the elements that satisfy the query.
        Debug.Assert(filteredData.Count == 2, $"There should be 2 items in the filtered list but found {filteredData.Count} items.");
        Debug.Assert(filteredData.Contains(s_Data[0]), "Test 1 should be in the list as its id is < 2.");
        Debug.Assert(filteredData.Contains(s_Data[1]), "Test 2 should be in the list as its id is < 2.");
    }

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


public Query<TData,TPayload> Parse (string text, IQueryHandlerFactory<TData,TQueryHandler,TPayload> queryHandlerFactory);

参数

text The query input string.
queryHandlerFactory A factory object that creates query handlers of type TQueryHandler. See IQueryHandlerFactory.

返回

Query<TData,TPayload> Query operation of type TData and TPayload.

描述

Parses a query string into a Query operation. This Query operation can then be used to filter any data set of type TData.