docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Weakly reference a scene

    Weak references to scenes work in the same way as weak references to objects. For more information, refer to Weakly reference an object.

    Unity uses an UntypedWeakReferenceId to weakly reference scenes so, to weakly reference a scene from a C# script, you can use the same workflow as described in Weakly reference an object from a C# script.

    Weakly reference a scene from the Inspector

    The RuntimeContentManager has a specific set of APIs to manage weakly-referenced scenes at runtime. This means that theWeakObjectReference wrapper doesn't work for scenes. The scene equivalent to this wrapper is WeakObjectSceneReference which provides the same runtime and editor workflow benefits as WeakObjectReference, but for scenes.

    To weakly reference a scene from the Inspector, substitute WeakObjectSceneReference for WeakObjectReference in the Weakly reference an object from the Inspector workflow. The following code sample shows how to do this.

    using Unity.Entities;
    using Unity.Entities.Content;
    using UnityEngine;
    
    public class SceneRefSample : MonoBehaviour
    {
        public WeakObjectSceneReference scene;
        class SceneRefSampleBaker : Baker<SceneRefSample>
        {
            public override void Bake(SceneRefSample authoring)
            {
                var entity = GetEntity(TransformUsageFlags.Dynamic);
                AddComponent(entity, new SceneComponentData { scene = authoring.scene });
            }
        }
    }
    
    public struct SceneComponentData : IComponentData
    {
        public WeakObjectSceneReference scene;
    }
    

    Additional resources

    • Weakly reference an object
    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)