Sample | Exchange generic messages between participants in Unity Cloud scenes
You can use this sample to exchange generic messages (user defined) between participants in a Unity Cloud scene.
Prerequisites
To use the sample, you require the following:
- An installed Presence package
- An installed Vivox package
- An installed Identity package
- An installed Storage package
- A valid Unity ID
- A Unity Cloud scene associated with your Unity ID
- Knowledge of how to join rooms
Note: While the Presence package doesn't depend on the Storage and Identity packages, they're used in the sample to control the authentication flow.
Installation
To install the sample, follow these steps:
- In your Unity project, go to Window > Package Manager > Unity Cloud Presence.
- Expand Samples and select Import beside the Generic Message sample.

After the import process completes, you can view the imported assets under the Assets/Samples/Unity Cloud Presence folder.

Run the sample
Note: If you don’t have at least two participants, do one of the following to join a room from multiple clients:
- Run the sample on two machines and join the same room.
- Build the sample scene and run the build while you run the sample in the Unity Editor.
- Run the sample in two instances of the Unity Editor at the same time.
At least two participants must join the room to take control of the scene gameobject (cube) and exchange generic messages. To run the sample, follow these steps:
- In your Unity project, go to File > Open Scene.
- Go to
Assets/Samples/Unity Cloud Presence/<package-version>/GenericMessage/Scenes/GenericMessageSample.unityand run the scene. - In the Game view, select Login if you are logged out. Log into the browser window that launches with your Unity ID account and then return to the sample scene. > Note: If you've previously logged in, the sample automatically logs you in so you can proceed to the next step.
Select and join a room. All participant's names appear in the Participants section.

Once there are two participants or more in the room, the control panel appears. This one allows to control the visibility and orientation of the cube.

In the control panel, click on the "Request Control" button to enable the orientation sliders and the visibility button.
Note: Only one participant can take control at a given time. If nothing happens when clicking on "Request Control" it means another participant took control. When that participant click on "Release Control", it allows others participants to take the control.

Click on the Show/Hide button to change the visibility of the cube.
- Move the x, y and z sliders to change the orientation of the cube.
The cube movement is replicated in all connected participants including late joiner participants.

Click on the "Release Control" button, the cube will return to its original state.

Main components
This section describes the scripts that make up the main components of this sample.
VisibilityMessage and RotationMessage scripts
The VisibilityMessage script defines the data to be exchanged between participants to control the visibility of a gameobject. Its cache type is "ReplaceLastByType" meaning only the last message of that type will be kept in the server cache for a given cache key.
To open the VisibilityMessage sample script, go to the Assets/Samples/Unity Cloud Presence/<package-version>/GenericMessage/Scripts/VisibilityMessage.cs file.
The RotationMessage script defines the data to be exchanged between participants to control the orientation of a gameobject. Its cache type is "ReplaceLastByParameters" meaning each last message of that type with the same parameter will be kept in the server cache for a given cache key.
To open the RotationMessage sample script, go to the Assets/Samples/Unity Cloud Presence/<package-version>/GenericMessage/Scripts/RotationMessage.cs file.
OperationHandler script
The OperationHandler script shows you how to do the following:
- Retrieve the
IGenericMessageHandlerfrom theINetcodeService - Subscribe to
NetcodeService.ClientStartedandNetcodeService.ClientStoppedthrough theINetcodeServiceclass - Subscribe to
IGenericMessageHandler.MessageReceived - Request/Release control using the
Presence.ControlMessage - Receive ControlMessage for granted control
- Send GenericMessage through the
IGenericMessageHandler
To open the OperationHandler sample script, go to the Assets/Samples/Unity Cloud Presence/<package-version>/GenericMessage/Scripts/OperationHandler.cs file.
ObjectSynchronizer script
The ObjectSynchronizer script shows you how to do the following:
- Retrieve the
IGenericMessageHandlerfrom theINetcodeService - Subscribe to
NetcodeService.ClientStartedandNetcodeService.ClientStoppedthrough theINetcodeServiceclass - Subscribe to
IGenericMessageHandler.MessageReceived - Receive VisibilityMessage and RotationMessage and apply change to target gameobject
To open the ObjectSynchronizer sample script, go to the Assets/Samples/Unity Cloud Presence/<package-version>/GenericMessage/Scripts/ObjectSynchronizer.cs file.
Troubleshoot
This section describes issues you might have while using the sample.
Automatic browser redirection doesn't work
If you run the sample in the Unity Editor, you should see the following page after you successfully login through your browser.

If you aren't automatically redirected to the Editor and nothing happens when you select Launch Application, return to the Editor. This should continue the authentication process.
Mouse input isn't registered
This sample isn't created to run with the Input System package. If you're using this package in your project, your mouse selections may not be detected.
To fix this, set your project to support both the built-in input system and the Input System package: go to Edit > Project Settings > Player and set Active Input Handling to Both.
