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

SerializedProperty.managedReferenceId

建议修改

成功!

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

关闭

提交失败

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

关闭

取消

public long managedReferenceId;

描述

与托管引用关联的 ID。

propertyTypeSerializedPropertyType.ManagedReference 时可用。如果引用为 null,则 ID 为 SerializeUtility.RefIdNull。

其他资源: SerializeReferencemanagedReferenceValueManagedReferenceUtility.GetManagedReferenceIdForObject

using System;
using UnityEditor;
using UnityEngine;

public class SerializedPropertyManagedReferenceIdExample : ScriptableObject { [Serializable] public class Item { public int m_data = 1; }

[SerializeReference] public Item m_Item;

[SerializeReference] public Item m_Item2;

[MenuItem("Example/SerializedProperty ManagedReferenceId Example")] static void TestMethod1() { var scriptableObject = ScriptableObject.CreateInstance<SerializedPropertyManagedReferenceIdExample>(); scriptableObject.m_Item = new Item();

using (var serializedObject = new SerializedObject(scriptableObject)) { var itemProperty = serializedObject.FindProperty("m_Item"); var item2Property = serializedObject.FindProperty("m_Item2");

// Set m_Item2 to point to the same object as m_Item // Note: managedReferenceValue could also be used here, for the same result item2Property.managedReferenceId = itemProperty.managedReferenceId;

serializedObject.ApplyModifiedProperties(); }

// Check the results back on the live object

//Will print "Value of m_Item2.m_data: 1" Debug.Log("Value of m_Item2.m_data: " + scriptableObject.m_Item2.m_data);

// Prove that both fields point to the same object scriptableObject.m_Item.m_data = 2;

//Will print "Value of m_Item2.m_data: 2" Debug.Log("Value of m_Item2.m_data: " + scriptableObject.m_Item2.m_data); } }