docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Weakly reference an object

    A weak object reference is a handle to an object that is valid regardless of whether the object is loaded or unloaded. If you create a weak reference to an object, Unity includes that object in a content archive which makes the object available to you at runtime. You can then use the RuntimeContentManager API to load, use, and release the weakly-referenced objects.

    The content management system provides a way to weakly reference an object via the Inspector and via a C# script.

    Weakly reference an object from the Inspector

    The WeakObjectReference struct provides a wrapper around the RuntimeContentManager APIs that are responsible for managing weakly-referenced objects. It also makes it possible to create a weak reference to an object via the Inspector. The Inspector property drawer for a WeakObjectReference is an object field that you can drag objects onto. Internally, Unity generates a weak reference to the object you assign, which you can then pass to ECS components during the baking process.

    The WeakObjectReference wrapper also makes it easier to manage individual weakly-referenced objects at runtime. It provides methods and properties to load, use, and release the object that it weakly references.

    The following code sample shows how to create a Baker that passes a WeakObjectReference of a mesh asset to an unmanaged component. The mesh property appears in the Inspector of the MeshRefSample component as an object field that you can assign a mesh asset to.

    using Unity.Entities;
    using Unity.Entities.Content;
    using UnityEngine;
    
    public class MeshRefSample : MonoBehaviour
    {
        public WeakObjectReference<Mesh> mesh;
        class MeshRefSampleBaker : Baker<MeshRefSample>
        {
            public override void Bake(MeshRefSample authoring)
            {
                var entity = GetEntity(TransformUsageFlags.Dynamic);
                AddComponent(entity, new MeshComponentData { mesh = authoring.mesh });
            }
        }
    }
    
    public struct MeshComponentData : IComponentData
    {
        public WeakObjectReference<Mesh> mesh;
    }
    

    Weakly reference an object from a C# script

    The RuntimeContentManager APIs manage weakly-referenced objects by an UntypedWeakReferenceId.

    The following code sample shows how to get the UntypedWeakReferenceId of the objects currently selected in the Project window. To make Unity include these objects in content archives, you must bake the weak reference IDs into an ECS component. To pass weak reference IDs created in Editor scripts to a baker, you can use a Scriptable Object. An Editor script can write weak reference IDs to a ScriptableObject then later, during the baking process, a baker can read the IDs from the ScriptableObject and write them into an ECS component.

    using UnityEngine;
    using UnityEditor;
    using Unity.Entities.Serialization;
    
    public static class ContentManagementEditorUtility
    {
        [MenuItem("Content Management/Log UntypedWeakReferenceId of Selected")]
        private static void LogWeakReferenceIDs()
        {
            Object[] selectedObjects = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
            for (int i = 0; i < selectedObjects.Length; i++)
            {
                Debug.Log($"{selectedObjects[i].name}: {UntypedWeakReferenceId.CreateFromObjectInstance(selectedObjects[i])}");
            }
        }
    }
    

    Additional resources

    • Load a weakly-referenced object at runtime
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)