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

ParsedQuery<T0>.returnPayloadIfEmpty

建议更改

成功!

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

关闭

提交失败

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

关闭

取消

public bool returnPayloadIfEmpty;

说明

布尔值。指明当查询为空时是否应返回原始有效负载。

如果设置为 true,则会在查询为空时返回原始有效负载。如果设置为 false,则返回一个空数组。

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

static class Example_ParsedQuery_returnPayloadIfEmpty
{
    static List<MyObjectType> s_Data;

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

        // When setting "returnPayloadIfEmpty" to true, empty queries
        // will return the same data set that was passed as input. Otherwise,
        // it will return an empty set.
        var query = queryEngine.ParseQuery("");
        var filteredData = query.Apply(s_Data).ToList();
        Debug.Assert(filteredData.Count == s_Data.Count, "Filtered data should have the same number of items as the input to the query.");
        for (var i = 0; i < filteredData.Count; ++i)
        {
            Debug.Assert(filteredData[i] == s_Data[i], $"Filtered data at index {i} should be the same as the input.");
        }
    }

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