docs.unity3d.com
    Show / Hide Table of Contents

    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:

    1. In your Unity project, go to Window > Package Manager > Unity Cloud Presence.
    2. Expand Samples and select Import beside the Generic Message sample.
      Screenshot of the Package Manager's Presence samples

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

    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:

    1. In your Unity project, go to File > Open Scene.
    2. Go to Assets/Samples/Unity Cloud Presence/<package-version>/GenericMessage/Scenes/GenericMessageSample.unity and run the scene.
    3. 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.
    4. Select and join a room. All participant's names appear in the Participants section.
      Screenshot with 1 participant

    5. 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.
      Screenshot with 2 participants

    6. 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.
      Screenshot with request control button

    7. Click on the Show/Hide button to change the visibility of the cube.

    8. Move the x, y and z sliders to change the orientation of the cube.
    9. The cube movement is replicated in all connected participants including late joiner participants.
      Screenshot with cube control

    10. Click on the "Release Control" button, the cube will return to its original state.
      Screenshot with release control button

    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 IGenericMessageHandler from the INetcodeService
    • Subscribe to NetcodeService.ClientStarted and NetcodeService.ClientStopped through the INetcodeService class
    • 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 IGenericMessageHandler from the INetcodeService
    • Subscribe to NetcodeService.ClientStarted and NetcodeService.ClientStopped through the INetcodeService class
    • 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.

    Screenshot of a successful login

    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.

    Screenshot of the Active Input Handling field

    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023