docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Load a weakly-referenced object at runtime

    Unity doesn't automatically load weakly-referenced objects, so it's your responsibility to load them before you need them. To load an object at runtime, you must have a weak reference to the object stored in an ECS component. The weak reference can be either a WeakObjectReference or an UntypedWeakReferenceId. For information on how to store a weak reference to an object, refer to Weakly reference an object.

    Load an object at runtime with a WeakObjectReference

    The following code example shows how to use WeakObjectReferences to load and render a mesh with a material from an ISystem. For information on how to pass a WeakObjectRefrence to a component, refer to Weakly reference an object from the Inspector.

    using Unity.Entities;
    using Unity.Entities.Content;
    using Unity.Transforms;
    using UnityEngine;
    
    public struct WeakObjectReferenceData : IComponentData
    {
        public bool startedLoad;
        public WeakObjectReference<Mesh> mesh;
        public WeakObjectReference<Material> material;
    }
    
    [WorldSystemFilter(WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor)]
    [UpdateInGroup(typeof(PresentationSystemGroup))]
    public partial struct RenderFromWeakObjectReferenceSystem : ISystem
    {
        public void OnCreate(ref SystemState state) { }
        public void OnDestroy(ref SystemState state) { }
        public void OnUpdate(ref SystemState state)
        {
            foreach (var (transform, dec) in SystemAPI.Query<RefRW<LocalToWorld>, RefRW<WeakObjectReferenceData>>())
            {
                if (!dec.ValueRW.startedLoad)
                {
                    dec.ValueRW.mesh.LoadAsync();
                    dec.ValueRW.material.LoadAsync();
                    dec.ValueRW.startedLoad = true;
                }
                if (dec.ValueRW.mesh.LoadingStatus == ObjectLoadingStatus.Completed &&
                    dec.ValueRW.material.LoadingStatus == ObjectLoadingStatus.Completed)
                {
                    Graphics.DrawMesh(dec.ValueRO.mesh.Result,
                        transform.ValueRO.Value, dec.ValueRO.material.Result, 0);
                }
            }
        }
    }
    

    Load an object at runtime with an UntypedWeakReferenceId

    The following code example shows how to use the RuntimeContentManager APIs and UntypedWeakReferenceIds to load and render a mesh with a material from an ISystem. For information on how to pass a UntypedWeakReferenceId to a component, refer to Weakly reference an object from a C# script.

    using Unity.Entities;
    using Unity.Entities.Content;
    using Unity.Transforms;
    using UnityEngine;
    using Unity.Entities.Serialization;
    
    public struct ObjectUntypedWeakReferenceIdData : IComponentData
    {
        public bool startedLoad;
        public UntypedWeakReferenceId mesh;
        public UntypedWeakReferenceId material;
    }
    
    [WorldSystemFilter(WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor)]
    [UpdateInGroup(typeof(PresentationSystemGroup))]
    public partial struct RenderFromUntypedWeakReferenceIdSystem : ISystem
    {
        public void OnCreate(ref SystemState state) { }
        public void OnDestroy(ref SystemState state) { }
        public void OnUpdate(ref SystemState state)
        {
            foreach (var (transform, dec) in SystemAPI.Query<RefRW<LocalToWorld>, RefRW<ObjectUntypedWeakReferenceIdData>>())
            {
                if (!dec.ValueRO.startedLoad)
                {
                    RuntimeContentManager.LoadObjectAsync(dec.ValueRO.mesh);
                    RuntimeContentManager.LoadObjectAsync(dec.ValueRO.material);
                    dec.ValueRW.startedLoad = true;
                }
                if (RuntimeContentManager.GetObjectLoadingStatus(dec.ValueRO.mesh) == ObjectLoadingStatus.Completed &&
                    RuntimeContentManager.GetObjectLoadingStatus(dec.ValueRO.material) == ObjectLoadingStatus.Completed)
                {
                    Mesh mesh = RuntimeContentManager.GetObjectValue<Mesh>(dec.ValueRO.mesh);
                    Material material = RuntimeContentManager.GetObjectValue<Material>(dec.ValueRO.material);
                    Graphics.DrawMesh(mesh, transform.ValueRO.Value, material, 0);
                }
            }
        }
    }
    

    Additional resources

    • Load a weakly-referenced scene 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)