In Unity’s Authentication Service, checking the online status after signing in anonymously can be a bit tricky, as IsSignedIn, IsAuthorized, and IsExpired don’t automatically update with changes in network connectivity. Here’s how you can approach this:
1. Checking Connection Status
Since IsSignedIn and IsAuthorized don’t update with network status changes, you can add a separate check to determine if the device is online. Here are a few methods to do that:
a) Use Application.internetReachability
Unity provides Application.internetReachability to check the network state:
if(Application.internetReachability == NetworkReachability.NotReachable)
{
// Show "Not Connected" message.
}
else
{
// Show "Connected" message.
}
While Application.internetReachability can help in detecting a lack of network, it’s not foolproof for all internet connectivity states. It will tell you if the device has an active internet connection but not if the connection is working with Unity’s services specifically.
b) Make a Simple Request to Unity Services
If you want to confirm if Unity’s Authentication Service can actually reach the network, consider making a lightweight request (like checking the leaderboard or getting player information). If the request fails, you can assume the service is unreachable and show a “Not Connected” message.
For example:
try
{
var leaderboards = await LeaderboardsService.Instance.GetPlayerLeaderboardAsync("example_leaderboard_id");
// Connection is good if we get here.
}
catch(Exception ex)
{
// Handle offline status or network error.
Debug.Log("No connection to Unity services.");
}
This way, you know specifically whether Unity’s backend is reachable.
2. Re-Signing In After Connection is Restored
When the internet connection returns, IsSignedIn should still be true for the current session. As long as IsAuthorized is still true and the session hasn’t expired (IsExpired is false), you don’t need to re-authenticate. However, if IsExpired eventually turns true, then you will need to re-sign in.
If you want to proactively refresh the session after reconnection, you could trigger SignInAnonymouslyAsync() again. However, usually, this isn’t necessary unless IsExpired is true.
Putting It Together
Here’s an example of how you might structure this in Unity:
private async void CheckConnectivityAndSignInIfNeeded()
{
// Check if there's an internet connection.
if(Application.internetReachability == NetworkReachability.NotReachable)
{
Debug.Log("Not connected to internet.");
// Show "Not Connected" message in UI.
return;
}
// Optionally, check if Unity's service is reachable.
try
{
// Example lightweight request.
var leaderboards = await LeaderboardsService.Instance.GetPlayerLeaderboardAsync("example_leaderboard_id");
Debug.Log("Connected to Unity services.");
}
catch
{
Debug.Log("Cannot reach Unity services.");
// Show "Not Connected" message in UI.
return;
}
// Check if sign-in is required based on expiration status.
if(!AuthenticationService.Instance.IsSignedIn || AuthenticationService.Instance.IsExpired)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
Debug.Log("Re-signed in to Unity Authentication Service.");
}
}
This method checks connectivity, ensures Unity services are reachable, and only re-authenticates if necessary. You can run this function on a timer or set it up to be called whenever you detect a network state change.