Users
Once connected to the multiplayer session, you will receive the list of connected users. This UserList
property is accessibly via the INetworkClient
which is available in a static context at ImmerseMultiplayer.Client
.
An IUser
object contains the user’s name (Name
), multiplayer ID (Id
) and unique ID on the Immerse platform (PlatformId
).
PlatformId
is only exposed for very specific use cases where a user needs to be identified across multiple applications. Please useId
wherever possible.
Debug.Log("Users in session: ");
foreach (var user in ImmerseMultiplayer.Client.Users)
{
Debug.Log(user.Name);
}
The local user object is made readily accessible through the LocalUser
property in INetworkClient
, as well as being present in UserList
.
Debug.Log("The local user is " + ImmerseMultiplayer.Client.LocalUser.Name);
If you have the ID of a user, you can retrieve their IUser
object using the FindUser
method in your INetworkClient
var user = ImmerseMultiplayer.Client.FindUser(userId);
Each time a user connects or disconnects, an event is triggered for that user. These events, OnUserConnected
and OnUserDisconnected
, are accessible via the INetworkClient
.
The example below shows how you can subscribe to the user connection and disconnection events:
using ImmerseSDK.Multiplayer;
using UnityEngine;
ImmerseMultiplayer.Client.OnUserConnected += connectingUser =>
{
Debug.LogFormat("{0} joined the session!", connectingUser.Name);
};
ImmerseMultiplayer.Client.OnUserDisconnected += disconnectingUser =>
{
Debug.LogFormat("{0} has disconnected", disconnectingUser.Name);
};