ManagedReferenceUtility.GetManagedReferenceIds
Declaration
public static long[] GetManagedReferenceIds(Object obj);Параметры
| Параметр | Описание |
|---|---|
| hostObj | Хост-объект, поддерживающий SerializeReference. Например, MonoBehaviour или ScriptableObject. |
Возвращаемое значение
long[] Возвращает список самой последней сериализации объекта хоста, включая самый последний вызов ManagedReferenceUtility.SetManagedReferenceIdForObject.
Запись в списке возвращается как ManagedReferenceUtility.RefIdNull, если поле SerialReference имеет значение null.
Возвращаемый список исключает объекты с отсутствующими типами, поскольку они не могут быть десериализированы. Чтобы получить объекты с отсутствующими типами, используйте SerializationUtility.GetManagedReferencesWithMissingTypes.
Описание
Получает список управляемых идентификаторов ссылок, назначенных объектам, на которые делается ссылка с помощью SerializeReference на указанном узле.
using System; using System.Collections.Generic; using System.Text; using UnityEditor; using UnityEngine; using UnityEngine.Serialization;
public class GetManagedReferenceIdsExample : ScriptableObject { [Serializable] public class Item { public char m_data; }
[SerializeReference] public Item m_singleItem;
[SerializeReference] public List<Item> m_items;
[SerializeReference] public List<Item> m_secondaryList;
void PopulateState() { m_singleItem = new Item() {m_data = 'a'};
m_items = new List<Item>() { new Item() {m_data = 'b'}, new Item() {m_data = 'c'}, new Item() {m_data = 'd'} };
m_secondaryList = new List<Item>() { new Item() {m_data = 'e'}, new Item() {m_data = 'f'} };
// Add some duplicates entries that point to the same instances m_items.Add(m_items[0]); m_items.Add(m_items[2]); m_items.Add(m_secondaryList[0]); m_secondaryList.Add(m_items[1]); }
void Serialize() { // Force a serialization of the ScriptableObject by creating a temporary SerializedObject (similar to viewing the object in the inspector) // This ensures that all the managed references get registered and assigned Ids based on the current state. // Serialization also happens when a Scene is saved, Undo.RecordObjects() etc var serializedObject = new SerializedObject(this); }
void FindAllItems() { // Get the list of Ids (accurate to state the last time this ScriptableObject was serialized). // This is a quick way to access all the instances across all fields, without duplicates. var allReferencedObjects = ManagedReferenceUtility.GetManagedReferenceIds(this);
var objectListing = new StringBuilder(); foreach (var id in allReferencedObjects) { if (id == SerializationUtility.RefIdNull) continue;
// Lookup up the object associated with the Id. If the ScriptableObject contained other types of referenced // objects that are not related to "Item" class then null would be expected. var retrievedItem = ManagedReferenceUtility.GetManagedReference(this, id) as Item; if (retrievedItem == null) continue;
objectListing.Append("Found Item object with data " + retrievedItem.m_data + " at Id " + id).AppendLine(); } /* Output will be something like this, with the Ids different each time:
Found Item object with data a at id 1757967572722253824 Found Item object with data b at id 1757967572722253825 Found Item object with data c at id 1757967572722253826 Found Item object with data d at id 1757967572722253827 Found Item object with data e at id 1757967572722253828 Found Item object with data f at id 1757967572722253829 */ Debug.LogFormat(objectListing.ToString()); }
[MenuItem("Example/ManagedReferenceUtility GetManagedReferenceIds Example")] static void TestMethod() { var scriptableObject = ScriptableObject.CreateInstance<GetManagedReferenceIdsExample>(); scriptableObject.PopulateState(); scriptableObject.Serialize(); scriptableObject.FindAllItems(); } }