publicvoidSubscribeToLoginEvents(){_events.LoggingIn+= OnLoggingIn;_events.LoggedIn+= OnLoggedIn;_events.LoggingOut+= OnLoggingOut;_events.LoggedOut+= OnLoggedOut;_events.LoginAdded+= OnLoginAdded;_events.LoginRemoved+= OnLoginRemoved;_events.LoginUpdated+= OnLoginUpdated;}publicvoidUnsubscribeToLoginEvents(){_events.LoggingIn-= OnLoggingIn;_events.LoggedIn-= OnLoggedIn;_events.LoggingOut-= OnLoggingOut;_events.LoggedOut-= OnLoggedOut;_events.LoginAdded-= OnLoginAdded;_events.LoginRemoved-= OnLoginRemoved;_events.LoginUpdated-= OnLoginUpdated;}#regionLogin / Logout CallbacksprotectedvirtualvoidOnLoggingIn(ILoginSession loginSession){Debug.Log($"Logging In : {loginSession.LoginSessionId.DisplayName}");}protectedvirtualvoidOnLoggedIn(ILoginSession loginSession){Debug.Log($"Logged in : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");}protectedvirtualvoidOnLoggingOut(ILoginSession loginSession){ Debug.Log($"Logging out : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");
}protectedvirtualvoidOnLoggedOut(ILoginSession loginSession){ Debug.Log($"Logged out : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");
}protectedvirtualvoidOnLoginAdded(AccountId accountId){Debug.Log($"LoginSession Added : For user {accountId.Name}");}protectedvirtualvoidOnLoginRemoved(AccountId accountId){Debug.Log($"Login Removed : For user {accountId}");}protectedvirtualvoidOnLoginUpdated(ILoginSession loginSession){ Debug.Log($"LoginSession has been Updated : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");
}#endregion
Dynamic Events
Make sure the parameter in your method matches the event type. See what parameter is required for each Login Event here. Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this there is no need to Subscribe/Unsubscribe from events with the usual +=/-=
[LoginEvent(LoginStatus.LoggingIn)]privatevoidOnPlayerLoggingIn(ILoginSession loginSession){Debug.Log($"Logging In : {loginSession.LoginSessionId.DisplayName}");}[LoginEvent(LoginStatus.LoggedIn)]privatevoidOnPlayerLoggedIn(ILoginSession loginSession){Debug.Log($"Logged in : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");}[LoginEvent(LoginStatus.LoggingOut)]privatevoidOnPlayerLoggingOut(ILoginSession loginSession){Debug.Log($"Logging out : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");}[LoginEvent(LoginStatus.LoggedOut)]privatevoidOnPlayerLoggedOut(ILoginSession loginSession){Debug.Log($"Logged out : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");}[LoginEvent(LoginStatus.LoginAdded)]privatevoidOnLoginSessionAdded(AccountId accountId){Debug.Log($"A new Login was added for player {accountId.DisplayName}");}[LoginEvent(LoginStatus.LoginRemoved)]privatevoidOnLoginSessionRemoved(AccountId accountId){Debug.Log($"A new Login was removed for player {accountId.DisplayName}");}[LoginEvent(LoginStatus.LoginValuesUpdated)]privatevoidOnLoginSessionValuesUpdated(ILoginSession loginSession){Debug.Log($"LoginSession has been updated for player {loginSession.LoginSessionId.DisplayName}");}
Dynamic Async Events
Make sure the parameter in your method matches the event type. See what parameter is required for each Login Event here. Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this there is no need to Subscribe/Unsubscribe from events with the usual +=/-=
Remember to use async void or async Task or else the event may run synchronously
More information on the methods called in any async method can be found here. Unity Gaming Services Examples. They are direct copies from Unity's docs. These are just examples and don't mimic real world use cases
[LoginEventAsync(LoginStatus.LoggingIn)]privateasyncvoidOnPlayerLoggingInAsync(ILoginSession loginSession){Debug.Log($"Logging In : {loginSession.LoginSessionId.DisplayName}");awaitGetJoinedLobbies();}[LoginEventAsync(LoginStatus.LoggedIn)]privateasyncvoidOnPlayerLoggedInAsync(ILoginSession loginSession){Debug.Log($"Logged in : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");awaitLoadPlayerData();}[LoginEventAsync(LoginStatus.LoggingOut)]privateasyncvoidOnPlayerLoggingOutAsync(ILoginSession loginSession){Debug.Log($"Logging out : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");awaitRemovePlayerFromLobby();}[LoginEventAsync(LoginStatus.LoggedOut)]privateasyncvoidOnPlayerLoggedOutAsync(ILoginSession loginSession){Debug.Log($"Logged out : {loginSession.LoginSessionId.DisplayName} : Presence = {loginSession.Presence.Status}");awaitSavePlayerData();}[LoginEventAsync(LoginStatus.LoginAdded)]privateasyncvoidOnLoginSessionAddedAsync(AccountId accountId){Debug.Log($"A new Login was added for player {accountId.DisplayName}");awaitGetJoinedLobbies();}[LoginEventAsync(LoginStatus.LoginRemoved)]privateasyncvoidOnLoginSessionRemovedAsync(AccountId accountId){Debug.Log($"A new Login was removed for player {accountId.DisplayName}");awaitRemovePlayerFromLobby();}[LoginEventAsync(LoginStatus.LoginValuesUpdated)]privateasyncvoidOnLoginSessionValuesUpdatedAsync(ILoginSession loginSession){Debug.Log($"LoginSession has been updated for player {loginSession.LoginSessionId.DisplayName}");awaitUpdatePlayerData();}