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

QueryValidationOptions.skipUnknownFilters

提出更改建议

成功!

感谢您帮助我们提高 Unity 文档的质量。尽管我们无法接受所有提交,但我们确实会阅读用户提出的每条更改建议,并在适用时进行更新。

关闭

提交失败

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

关闭

取消

public bool skipUnknownFilters;

说明

布尔值,指示是否跳过未知过滤器。

如果值为 true,则跳过未知过滤器。如果为 false 并且 validateFilters 为 true,如果未通过 QueryEngine.SetDefaultFilter 提供默认处理程序,则未知过滤器将生成错误。

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

static class Example_QueryValidationOptions_skipUnknownFilters
{
    [MenuItem("Examples/QueryValidationOptions/skipUnknownFilters")]
    public static void RunExample()
    {
        // Set up the query validation options with filter validation and unknown filter skipping.
        var validationOptions = new QueryValidationOptions { validateFilters = true, skipUnknownFilters = true};

        // Set up the query engine
        var queryEngine = new QueryEngine<MyObjectType>(validationOptions);
        queryEngine.AddFilter("id", myObj => myObj.id);
        queryEngine.SetSearchDataCallback(myObj => new[] { myObj.id.ToString(), myObj.name });

        var dataset = new[]
        {
            new MyObjectType() { id = 0, name = "Test 1" },
            new MyObjectType() { id = 10, name = "Test 2" }
        };

        // Parse a query with the filter "id". There shouldn't be any errors
        var query = queryEngine.ParseQuery("id>10");
        Debug.Assert(query.valid, $"Query \"{query.text}\" should be valid.");

        // Because the QueryEngine is skipping unknown filter, there should not be errors for unknown filters
        // but it should still get results for filters that are known. If validateFilters were set
        // to false, this query would yield no results since the generated filter node for the unknown filter
        // would automatically return false for any elements of the filtered data set.
        query = queryEngine.ParseQuery("id<10 name:MyName");
        Debug.Assert(query.valid, $"Query \"{query.text}\" should be valid.");
        var filteredData = query.Apply(dataset).ToList();
        Debug.Assert(filteredData.Count == 1, $"There should be 1 item in the filtered list but found {filteredData.Count} items.");
        Debug.Assert(filteredData.Contains(dataset[0]), "Test 1 should be in the filtered list since its id is < 10");
    }

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