docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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:

    1. In your PlatformServices script, update the PlatformServices class with public references to IRoomProvider<Room>, ISessionProvider, IPresenceMessageHandler, IPresentationService, IPresenceManager, and a private reference to PresenceManager.
        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;
    
    1. 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);
    
            // ...
        }
    
    1. Initialize the services in the InitializeAsync method.
        public static async Task InitializeAsync()
        {
            // ...
    
            await s_CompositeAuthenticator.InitializeAsync();
    
            // ...
        }
    
    1. 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:

    1. From the PlatformServices script, retrieve the IPresenceManager.
    2. Use the ConnectPresenceRoomsAsync and DisconnectPresenceRoomsAsync methods to connect and disconnect from the PresenceRooms service. Refer to the AuthenticationState 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.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)