Immerse

Scene Object

Step 4 of 13 |10 mins

App Object

Spawn Object

Description

Every scene must have its own Scene object. The Scene component contains a list of objects that can be accessed by ID allowing you to synchronize between users. Any Object in your scene or project can be added to the scene component and will automatically be assigned a unique ID.

Any objects that are intended to use DontDestroyOnLoad should not be added to a Scene object, but instead registered with the DontDestroyOnLoadIndex in the SceneRegistry.

using ImmerseSDK;
using UnityEngine;

public class IndexedDontDestroyOnLoadExample : MonoBehaviour
{
    private void Awake()
    {
        SceneRegistry.DontDestroyOnLoadLoadIndex.AddObject(this);
        DontDestroyOnLoad(gameObject);
    }
}

Unity Editor Component

The Scene component displays all indexed objects in an array, along with two buttons:

Find Indexed Components

This will find all components that inherit the Indexed interface and add them to the Indexed Objects array.

Regenerate Indices

This will clear out any empty entries in the Indexed Objects array. Note: this will change the ID of the objects in your scene.

Extending The Scene Class

It is possible to extend the Scene class. This can be useful for providing references to other assets required by your scene, as a Scene object can be easily retrieved from the SceneRegistry.

Below is an example of a class that inherits the Scene class and adds a prefab as an asset, which can be accessed publicly and a separate class that gains access to the new Scene using the SceneRegistry and instantiates that prefab.

using ImmerseSDK;
using UnityEngine;

public class MyScene : Scene
{
    /// <summary>
    /// Can be set in the inspector and public accessed
    /// </summary>
    public GameObject MyPrefab;
}
using ImmerseSDK;
using UnityEngine;

public class InstantiationExample : MonoBehaviour
{
    /// <summary>
    /// An example usage where we may want to instantiate a prefab that is referenced in the Scene
    /// </summary>
    public GameObject InstantiatePrefab()
    {
        // Get reference to the scene
        const int sceneBuildIndex = 0;
        var scene = (MyScene) SceneRegistry.GetScene(sceneBuildIndex);

        // Instantiate an instance and return the result
        var instance = Instantiate(scene.MyPrefab);
        return instance;
    }
}

📘

Immerse SDK Class Library

More detail available in ImmerseSDK.Scene

App Object

Spawn Object

Updated 3 months ago

Scene Object


Step 4 of 13 |10 mins

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.