Unity-Technologies / com.unity.services.samples.game-lobby

A sample showcasing a minimal implementation of a lobby experience using the Lobby and Relay packages.
Other
198 stars 66 forks source link
billing-7057 owner-multiplayer-services

Game Lobby Sample

Version 1.1a - UGS Events Beta

UGS EVENTS BETA NOTE

When using the Events code in this sample, make sure your Editor Project Settings > Player > Scripting Define Symbols include: UGS_BETA_LOBBY_EVENTS and UGS_LOBBY_EVENTS

Tested with Unity 2021.2 for PC and Mac.

This sample demonstrates how to use the Lobby and Relay packages to create a typical game lobby experience. It also includes Vivox Voice chat. Players can host lobbies that other players can join using a public lobby list or lobby code. The Lobby also synchronizes the lobby and player data Relay allows players to securely connect with each other via Netcode for GameObjects while maintaining connection anonymity. Connecting to the lobby will also connect to Vivox to enable voice chat as long as an audio input device is available.

Note: This is not a “drag-and-drop” solution; the Game Lobby Sample is not a minimal code sample intended to be completely copied into a full-scale project. Rather, it demonstrates how to use multiple services in a vertical slice with some basic game logic and infrastructure. Use it as a reference and starting point to learn how Lobby and Relay work together and how to integrate them into your project.

Features:

Service organization setup

To use Unity’s multiplayer services, you need a cloud organization ID for your project. If you do not currently have one, follow the How do I create a new Organization? article to set up your cloud organization:

https://support.unity.com/hc/en-us/articles/208592876-How-do-I-create-a-new-Organization-

Once you have an ID, link it to your project under Edit > Project Settings > Services and use the Unity Dashboard to manage your project’s services.

Service overview

Lobby

The Lobby service allows developers to create lobbies and share data between players before a real-time network connection is established. It simplifies the first step in connecting users to other services such as Relay and provides tools to allow players to find other lobbies.

The Lobby documentation contains code samples and additional information about the service. It includes comprehensive details for using Lobby along with additional code samples, and it might help you better understand the Game Lobby Sample:

docs.unity.com/lobby

The Lobby service can be managed in the Unity Dashboard:

https://dashboard.unity3d.com/lobby

Relay

The Relay service connects players in a host-client model with an obfuscated host IP. This allows players to host networked experiences as though players connected directly while only sharing private information with Relay itself. \

The Relay documentation contains code samples and additional information about the service. It includes comprehensive details for using Relay along with additional code samples, and it might help you better understand the Game Lobby Sample:

docs.unity.com/relay

The Relay service can be managed in the Unity Dashboard:

https://dashboard.unity3d.com/relay

In this sample, once players are connected to a lobby, they are connected through Relay to set up real-time data transfer over UTP. Lobby and Relay both depend on Auth for credentials. This sample uses Auth’s anonymous login feature to create semi-permanent credentials that are unique to each player but do not require developers to maintain a persistent account for them. \

Vivox The Vivox Service provides player voice and text chat rooms for in-app communication. It also sports useful features like spatial audio and chat transcription.

The Vivox Documentation contains code samples and additional information about the service. It includes comprehensive details for using Vivox along with additional code samples, and it might help you better understand the Game Lobby Sample: https://docs.vivox.com/v5/general/unity/5_15_0/en-us/Default.htm

Vivox can be managed in the Unity Dashboard: https://dashboard.unity3d.com/vivox

Setup

The Lobby and Relay sections of the Unity Dashboard contain their own setup instructions. Select About & Support > Get Started and follow the provided steps to integrate the services into your project.

With those services set up and your project linked to your cloud organization, open the mainScene scene in the Editor and begin using the Game Lobby Sample.

Running the sample

You will need two “players” to demonstrate the full sample functionality. You can either use the included Parrelsync package to create duplicate projects for testing, or create a standalone build to run alongside the Editor in Play mode. Although Auth creates anonymous credentials using your machine’s registry, multiple Editor projects and client builds allow for multiple anonmyous auth tokens.

Lobby Join Menu

alt_text

The Lobby Join menu contains the lobby list UI, which acts as a hub for players to connect to each other using the public lobby list or lobby code.

    A.    Public lobby list: Shows all lobbies not set to private. Lobbies contain developer-defined data which can be set to public and non-public visibility. The Lobby service cleans up any “zombie” rooms so they don’t appear in this list. For this sample, lobby names and player counts are shown, and lobbies in the “in-game” state are not shown. You can select a lobby and then select Join.\     B.    Refresh icon: Refreshes the Lobby List. The Lobby service imposes rate limits on all API calls to prevent spamming. Refresh attempts within the rate limit will do nothing (approximately every 1.5 seconds, see Lobby documentation for details).\     C.    Filters: Sets the Lobby List to only show servers of a certain color. The Lobby service can filter any queries by data set to public visibility. For this sample, players can optionally filter by color, which hosts set for their lobbies.\     D.    Quick Join button :Join the first available lobby in the list that matches your filters.\     E.    Lobby Code field: Enter a lobby code for an existing lobby. In addition to the public lobby list, all lobbies can be joined using their codes. This allows players to privately share access to lobbies.\     F.    Join: Requests to join by public lobby list selection or lobby code. Failed requests are also rate limited to prevent spam, if the player presses the button repeatedly.\     G.    Create: Allows creation of a new lobby. Players select a lobby name and whether to make a private lobby, and they then connect to the new lobby as its host.\     H.    Player name: Displays the player name and allows renaming. By default, players are assigned a name based on their anonymous Auth credentials, but name changes follow their credentials so that all players see the new name.

Lobby View

alt_text

The Lobby View UI displays information from Lobby and Relay for all players in a lobby. When a new player joins, they immediately begin connecting to the host, after which they synchronize emotes and state changes with the other lobby members.

    A.    Lobby name: Set when the lobby was created and cannot be changed.\     B.    Lobby code: Shareable code generated by the Lobby service. This may be provided externally to other players to allow them to join this lobby.\     C.    Lobby user: A player in the lobby. The player’s name, state, and emote are displayed; this data is synchronized through Relay + UTP, so any changes that a player makes will appear immediately for all connected players. Incoming players will be sent the current data once they have connected.\     D.    Emotes: Shows the player’s Emote, as well as controls for voice chat if the user has a mic connected. \     E.    Vivox Voice Controls: Clicking the audio icon will mute/unmute that user.\     F.    Relay IP : No longer used here with the new Lobby-relay integration.\     G.    Relay Code: No longer used here with the new Lobby-relay integration.\     H.    Emote buttons:: Sets the player’s emote and is synchronized using UTP.\     I.    Lobby color: (Host only) Sets the lobby color for filtering in the Lobby List. This is synchronized through Lobby, so changes won’t appear immediately for all players because Lobby queries are rate limited. See Rate Limits.\     J.    Ready button: Sets a ready state on the player. When all players are ready, the host initiates a countdown to an “in-game” state, and the lobby becomes hidden from the public lobby list.\

Architecture

The Game Lobby Sample is designed as a vertical slice of a multiplayer lobby, so it has additional infrastructure that might be expected in full game production, as well as some components to allow multiple services to work together. As such, not all of the codebase will be relevant depending on your needs. Most contents are self-documenting, but some high-level points follow:

Considerations

While the Game Lobby Sample represents more than just a minimal implementation of the Lobby and Relay services, it is not comprehensive and some design decisions were made for faster or more readable development.