Get started
To begin using the Unity Cloud Presence package, you must first set up an app identifier, initialize the service and connect to the PresenceRooms service.
Before you begin, make sure you meet the prerequisites.
Set up an app identifier
To set up an app identifier, follow the steps in the Get Started section of the Identity package documentation.
Initialize the service
To initialize your service, follow these steps:
- In your
PlatformServices
script, update thePlatformServices
class with public references toIRoomProvider<Room>
,ISessionProvider
,IPresenceMessageHandler
,IPresentationService
,IPresenceManager
, and a private reference toPresenceManager
.
static PresenceManager s_PresenceManager;
public static IRoomProvider<Room> RoomProvider => s_PresenceManager;
public static ISessionProvider SessionProvider => s_PresenceManager;
public static IPresenceMessageHandler PresenceMessageHandler => s_PresenceManager.PresenceMessageHandler;
public static IPresentationService PresentationService => s_PresenceManager.PresentationService;
public static IPresenceManager PresenceManager => s_PresenceManager;
- Create the services in the
Create
method. The variables provided in the constructor are defined in the Identity authentication guide.
public static void Create()
{
// ...
s_PresenceManager = new PresenceManager(s_CompositeAuthenticator, serviceHttpClient, ServiceHostResolver);
// ...
}
- Initialize the services in the
InitializeAsync
method.
public static async Task InitializeAsync()
{
// ...
await s_CompositeAuthenticator.InitializeAsync();
// ...
}
- Shut down the services in the
Shutdown
method.
public static void Shutdown()
{
// ...
s_CompositeAuthenticator.Dispose();
s_CompositeAuthenticator = null;
s_PresenceManager.Dispose();
s_PresenceManager = null;
// ...
}
Connect to the PresenceRooms service
To connect and disconnect from the PresenceRooms service, follow these steps:
- From the
PlatformServices
script, retrieve theIPresenceManager
. - Use the
ConnectPresenceRoomsAsync
andDisconnectPresenceRoomsAsync
methods to connect and disconnect from thePresenceRooms
service. Refer to theAuthenticationState
in the Unity Cloud Identity documentation for more information.
IPresenceManager presenceManager = PlatformServices.PresenceManager;
async Task ApplyAuthenticationState(AuthenticationState state)
{
switch (state)
{
case AuthenticationState.LoggedIn:
await presenceManager.ConnectPresenceRoomsAsync(new ExponentialBackoffRetryPolicy(), m_CancellationTokenSource.Token);
break;
case AuthenticationState.LoggedOut:
await presenceManager.DisconnectPresenceRoomsAsync();
break;
}
}
Troubleshooting
Refer to the troubleshooting section if you have trouble getting started with your Unity Editor Project.