Immerse SDK
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Scene Registry

Because it is not possible to send an object reference directly through a message in a multi-user application, the Scene Registry was designed to provide an identity for Objects that will be consistent across all clients.

Only one Scene component should be included in each of the scenes for a project. Each Object that is referenced in this component, a unique ID is generated.

Scene component with 3 example items

ISceneIndexed

To aid in the process of listing items that require indexing, such as those in the Interaction package, any MonoBehaviour that inherits the ISceneIndexed interface will be automatically added to the Scene Index (if present) when it is added to the scene.

Using a GUID

The GUID returned by the Scene Index was designed so that it can be easily used as a Message ID when sending a message.

See the example below demonstrating how to retrieve a GUID and how to use it to find the indexed Object. The example implements the ISceneIndexed interface and therefore will be automatically added to the Scene Index.

using System.Collections;
using System.Text;
using ImmerseSDK.PlatformServices;
using ImmerseSDK.PlatformServices.Multiuser;
using ImmerseSDK.PlatformServices.Multiuser.Messaging;
using ImmerseSDK.PlatformServices.SceneManagement;
using UnityEngine;

public class MessageExample : MonoBehaviour, ISceneIndexed
{
    /// <summary>
    /// Message type to be used in this example. Make sure it's defined in the Messages foldout
    /// </summary>
    private const ushort MessageType = 1000;

    private IEnumerator Start()
    {
        // Listen for incoming messages
        MessageRegistry.Register(MessageType, HandleMessage);

        // Wait until we are connected before sending a message
        yield return new WaitForConnectionStatus(ConnectionStatus.Connected);

        // Get the GUID for this component
        var guid = SceneRegistry.GetId(this);

        // Send a message using the GUID as the message ID
        Networking.Client.SendMessage(MessageType, guid, Encoding.UTF8.GetBytes("Hello World!"));
    }

    /// <summary>
    /// Handles the incoming message
    /// </summary>
    private static void HandleMessage(Message message)
    {
        // We used the GUID as the message ID, so use that in the Scene Registry to find our component
        var guid = (GUID)message.Id;
        var component = SceneRegistry.GetObject<MessageExample>(guid);
        if (component == null)
        {
            // Oh no! Something went wrong! Did we index our component correctly?
            Debug.LogErrorFormat("Could not find object with GUID {0}!", guid);
            return;
        }

        Debug.LogFormat(component, "Received a message from {0}: \"{1}\"", component.name,
            message.GetPayloadAsString());
    }
}