提供属性的哈希值。(只读)
您可以使用它来跟踪属性路径下的值是否发生了任何更改(其他资源:SerializedProperty.propertyPath)。
请注意
-如果属性内容的大小小于或等于 32 位,则将返回内容而不是哈希值。
-如果属性路径指向数组或复杂类型,则哈希将对应于整个内容。
-如果属性是带有 [SerializeReference] 属性的字段,或者包含此类字段的复合类型,则内容哈希不包括引用的对象的内容,而是仅对引用 ID 进行哈希(其他资源:SerializedProperty.managedReferenceId)。
using UnityEngine; using UnityEditor;
public class MyObject : ScriptableObject { public string myString = "answer to life the universe and everything"; public string[] myStringArray = { "answer", "to", "life", "the", "universe", "and", "everything" }; public int[] myIntArray = { 42, 442, 422, 4242 };
[MenuItem("Example/Output contentHash from SerializedProperty")] static void OutputContentHashFromSerializedProperty() { var scriptableObject = ScriptableObject.CreateInstance<MyObject>();
using (var serializedObject = new SerializedObject(scriptableObject)) { SerializedProperty serializedPropertyMyString = serializedObject.FindProperty("myString"); SerializedProperty serializedPropertyMyStringArray = serializedObject.FindProperty("myStringArray"); SerializedProperty serializedPropertyMyIntArray = serializedObject.FindProperty("myIntArray");
uint myStringHash = serializedPropertyMyString.contentHash; uint myStringArrayHash = serializedPropertyMyStringArray.contentHash; uint MyIntArrayHash = serializedPropertyMyIntArray.contentHash;
serializedPropertyMyString.stringValue = "new string"; serializedPropertyMyIntArray.DeleteArrayElementAtIndex(1);
serializedObject.ApplyModifiedPropertiesWithoutUndo();
Debug.Log(string.Format("myString: before={0}, after={1}, changed={2}", myStringHash, serializedPropertyMyString.contentHash, myStringHash == serializedPropertyMyString.contentHash ? "no" : "yes")); Debug.Log(string.Format("myStringArrayHash: before={0}, after={1}, changed={2}", myStringArrayHash, serializedPropertyMyStringArray.contentHash, myStringArrayHash == serializedPropertyMyStringArray.contentHash ? "no" : "yes")); Debug.Log(string.Format("MyIntArrayHash: before={0}, after={1}, changed={2}", MyIntArrayHash, serializedPropertyMyIntArray.contentHash, MyIntArrayHash == serializedPropertyMyIntArray.contentHash ? "no" : "yes")); } } }