Multiplayer Service
The Multiplayer service allows you to send and receive game invites, as well as access information about the current player's Omni Online subscription and party status.
These features can be accessed via the MultiplayerService
class in the Omni One Unity SDK. For specifics on all available methods, see the Unity Scripting Reference
Note that before accessing these methods, you will need to initialize the Platform SDK as described in the Platform Overview article.
Omni Online
Players require an active Omni Online subscription to use multiplayer features. Without Omni Online, players should not be able to play online, send game invites to friends, or be featured on online leaderboards. From within your game, call MultiplayerService.HasOmniOnlineService()
to check if the current user has access to Omni Online, and use that information to enable or disable access to relevant multiplayer features.
Omni VOIP
The Omni One platform has a party system that supports VOIP voice chat communication between players. If your game also includes VOIP functionality, you will need to disable it when the player is in an Omni VOIP session, and enable it when the player leaves an Omni VOIP session.
To facilitate this, the Multiplayer Service offers the OmniVoipStatus
method to check the player's current VOIP status, and the SetOmniVoipStatusChangedCallback
method to notify you when this status changes.
Game Invites
For multiplayer games, you can allow the user to invite other players to their session using the Omni One Multiplayer Service. The Multiplayer Service also provides events that you can use to listen for invites from other players.
Launch Details
When sending an invite through the Multiplayer Service, you can include launch details as a way of matching the user with the correct session. These launch details are a string containing information of your choice. For example, you might want to send the IP address of the host player.
When receiving an invite through the Multiplayer Service, you will be given an object that contains the profile information of the user who sent the invite, along with a unique invite ID that can be passed into MultiplayerService.AcceptInvite()
. Once this call is made, the Omni One platform will update the current session's launch details to match the information set by the inviting player's game. The new launch details can be accessed using ApplicationService.SetLaunchIntentChangedCallback()
and ApplicationService.GetLaunchDetails()
as described in the example below.
Game Invite Examples
Sending a Game Invite:
Note: There are two ways to send an invite, but both are called in a similar fashion. InvitePlayers
opens up a dialog window in the Omni One environment that allows the user to choose which other users to send invites to. As an alternative, InvitePlayer
takes in the User ID of the desired player along with the launch details, allowing specific players to be invited from within your game.
using Omni.Platform.Multiplayer;
void SendInvites() {
// For this example, assume your game uses IP address to identify multiplayer sessions.
string launchData = GetMyIP();
MultiplayerService.InvitePlayers(launchData).OnComplete(message =>
{
// Handle and log error case if necessary
if (message.IsError)
{
Debug.Log($"Could not invite players! Error: {message.Error.Code} : {message.Error.Message}");
return;
}
bool invitesSent = message.Data;
if (invitesSent)
{
Debug.Log("Sent invites to players!");
}
}
}
Joining a Multiplayer Session on Startup
Your game will be launched when a player accepts a game invite while they are in the Omni One home environment, outside of your game. You can detect this by calling ApplicationService.GetLaunchDetails()
when starting your game.
If the player opened your game through an invite, this will return the information string you defined when the player sends an invite through the method above. If the game was launched without an invite, this will return null.
Receiving and Accepting an Invite While in Your Game
To receive game invites from within a game, you will need to set two callbacks. First, MultiplayerService.SetGameInviteReceivedCallback()
will inform you when another player sends the currently logged in player an invite to a session of your game, and ApplicationService.SetLaunchIntentChangedCallback()
will allow you to detect when the launch details have been changed after accepting a game invite.
The process to set up accepting an invite is as follows:
- Call
MultiplayerService.SetGameInviteReceivedCallback()
, passing in a handler method (for this example, a method calledOnGameInviteReceived
) - Call
ApplicationService.SetLaunchIntentChangedCallback()
, passing in a handler method (for this example, a method calledOnLaunchIntentChanged
) - In
OnGameInviteReceived
, prompt the player with the option to accept or reject the invite - If the player accepts the invite, call
MultiplayerService.AcceptInvite()
, passing in the invite ID provided in the parameter ofOnGameInviteReceived
. - Once
AcceptInvite
returns, the launch details string for the game will be updated asynchronously with whatever information you have decided will be sent alongside an invite (e.g. the IP address from the example above). Once the data has been updated,OnLaunchIntentChanged
will be invoked. - Inside
OnLaunchIntentChanged
, get the new launch details from the parameter and use the information however you need to in order to let the player join the invited session.
using Omni.Platform;
using Omni.Platform.Multiplayer;
using Omni.Platform.App;
// ...
// Assume this is on a script present when the game first starts
void Start()
{
MultiplayerService.SetGameInviteReceivedCallback(OnGameInviteReceived);
ApplicationService.SetLaunchIntentChangedCallback(OnLaunchIntentChanged);
}
void OnGameInviteReceived(Message<GameInvite> message)
{
GameInvite invite = message.Data;
// assume that you have defined this method such that OnPlayerAcceptedInvite() is called
// with the provided invite if the player decides to accept
PromptPlayerToAcceptInvite(invite);
}
void PromptUserToAcceptInvite(GameInvite invite)
{
// Display invite to the user and get whether or not they accept;
// ...
if (inviteAccepted)
{
MultiplayerService.AcceptInvite(invite.Id);
}
}
void OnLaunchIntentChanged(Message<string> message)
{
string launchDetails = message.Data;
// Assume that you have created this method, and that it connects the current
// player to the session identified by the launch details string
ConnectToMultiplayerSession(launchDetails);
}