Version: 2021.3
LanguageEnglish
  • C#

QueryEngine<T0>.Parse

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 Query<TData> Parse(string text, bool useFastYieldingQueryHandler);

Parameters

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.

Returns

Query<TData> Query operation of type TData.

Description

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


Declaration

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

Parameters

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

Returns

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

Description

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

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961