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

SerializedProperty.DataEquals

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法采纳所有提交,但我们确实会阅读用户提出的每项修改建议,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,您的修改建议无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static bool DataEquals(SerializedProperty x, SerializedProperty y);

描述

比较两个 SerializedProperty 的数据。此方法会忽略路径和 SerializedObject。

using System;
using UnityEngine;
using UnityEditor;

public class SerializedPropertyDataEqualsExample : ScriptableObject { [Serializable] public struct SomeData { public string aStringValue; public float aFloatValue; }

[Serializable] public struct SomeOtherData { public string anotherStringValue; public float anotherFloatValue; }

[SerializeField] SomeData someData; [SerializeField] SomeOtherData[] otherDataArray;

static bool AreBothPropertiesEquals() { // Create an instance of the ScriptableObject var testObject = ScriptableObject.CreateInstance<SerializedPropertyDataEqualsExample>(); // Set the first class to some values testObject.someData.aStringValue = "ThisValue"; testObject.someData.aFloatValue = 5.1f; // Set the first array entry of the second class to the exact same values testObject.otherDataArray = new SomeOtherData[1]; testObject.otherDataArray[0].anotherStringValue = "ThisValue"; testObject.otherDataArray[0].anotherFloatValue = 5.1f;

var serializedObject = new SerializedObject(testObject); // Serialized property that refers to data from the first class var propertyOne = serializedObject.FindProperty("someData"); // SerializedProperty that refers to data from the first entry of the second class array var propertyTwo = serializedObject.FindProperty("otherDataArray.Array.data[0]"); // Compare data from each SerializedProperty. bool result = SerializedProperty.DataEquals(propertyOne, propertyTwo);

serializedObject.Dispose(); DestroyImmediate(testObject); return result; }

[MenuItem("Example/SerializedPropertyDataEqualsExample")] static void TestMethod() { Debug.Log("Are properties equals ? " + AreBothPropertiesEquals()); } }