Version: 2021.2
言語: 日本語
public static object GetManagedReference (Object obj, long id);

説明

Retrieves an object based on its managed reference Id.

This method makes it possible to retrieve a specific referenced object efficiently, for example when its current location inside an array or graph of references isn't known. This method returns null when the Id isn't assigned to any object. See Also: GetManagedReferenceIdForObject, SerializeReference.

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

public class GetManagedReferenceExample : ScriptableObject { [Serializable] public class Item { public char m_data; }

[SerializeReference] public List<Item> m_items = new List<Item>();

private void InsertNewItem(long id, char data) { var newItem = new Item() {m_data = data}; if (SerializationUtility.SetManagedReferenceIdForObject(this, newItem, id)) m_items.Add(newItem); }

[MenuItem("Example/SerializationUtility GetManagedReference Example")] static void TestMethod() { var scriptableObject = ScriptableObject.CreateInstance<GetManagedReferenceExample>();

scriptableObject.InsertNewItem(1000, 'a'); scriptableObject.InsertNewItem(1001, 'b'); scriptableObject.InsertNewItem(1002, 'c'); scriptableObject.InsertNewItem(1003, 'd');

// Because 1002 is registered in an earlier call this will log an error and not change the state scriptableObject.InsertNewItem(1002, 'e');

// The array may get reordered over time. One way to find an specific item again would // be to look it up based on a known managed reference id var item = SerializationUtility.GetManagedReference(scriptableObject, 1002) as Item; Debug.LogFormat("Data on object 1002 is {0}", item.m_data);

var willBeNull = SerializationUtility.GetManagedReference(scriptableObject, 9999) as Item; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961