This is article 5 in our Unity Gaming Services tutorial series. In this post, you’ll learn about the steps for setting up a recoverable method that comes with all iOS phones: Apple Game Center, the recommended method for games on the Apple platform.
You’ll continue building on your simple project from previous tutorials, which consists of simple UI buttons and a LoginManager to handle all the sign-in or linking to providers. And you’ll use the code provided by the UGS documentation.
Where to find all of the content in the Unity Gaming Services tutorial series
The UGS series consists of 10 sets of tutorials, with each set comprising a video and written tutorial.
Video tutorials
The full video tutorial series will be here. We’ll add the video tutorials to this playlist throughout October.
Written tutorials
You’ll find links to all articles in this series as they become available in Part 1, the introduction post.
Demo project and scripts
You can find the Gem Hunter Match – Cloud Edition demo project in the GitHub page.
The scripts used in the tutorials can be downloaded here:
UGSTutorialSeries_Scripts.zip (1.6 MB)
Watch the video version of this tutorial.
Plugin and dashboard setup
Note that these steps are based on iOS. As is the case with other third-party plugins, you need to be a registered developer and create an app in the dashboard; for iOS you do this in the App Store Connect portal.
Make sure that in the app created for the project, you enable Game Center inside the App dashboard, under iOS App > 1.0 Prepare for submission (or the version that corresponds).
Note the Bundle ID of your app, under App Information as you’ll need to add it in Unity.
Add the identity provider in the Unity Dashboard or via Project settings > Authentication, and your app’s Bundle ID – this is usually the company reverse domain and product name at the end, for example, com.company.myapp.
Follow the documentation steps to import the Apple Unity Plug-in.
Once it’s downloaded, unzip it and run the following command on the terminal. Make sure you are executing the command in the root directory of the downloaded plugins folder (you can change directory with the command cd). The first task is to build the GameKit SDK.
python3 build.py -p GameKit
Check the requirements in case you couldn’t build these plugins running Python.
When promoted to continue with the build process click on Y (yes).
Then, run the same script/command on the terminal for the Apple Core plugin.
python3 build.py -p Core
When the process is complete you will have two .tgz files created in the same folder that we ran the script on, inside a Build folder.
Once they are built you can add them in your project via Package Manager > Add Package from tarball.
Now you are ready to sign in the player using Game Center.
Sign in and link with Apple Game Center
Open the LoginManager and use the code from the documentation.
Remember to add the directive at the beginning of the script to start using GameKit.
using Apple.GameKit;
In essence, all you have to do is to initialize GameCenter automatically, which retrieves the user credentials; in this case, there are several tokens to store.
string Signature;
string TeamPlayerID;
string Salt;
string PublicKeyUrl;
ulong Timestamp;
The initialization happens by calling this method from Start().
// Start is called before the first frame update
async void Start()
{
await AuthentificatePlayerWithGameCenter();
}
public async Task AuthentificatePlayerWithGameCenter()
{
if (!GKLocalPlayer.Local.IsAuthenticated)
{
// Perform the authentication.
var player = await GKLocalPlayer.Authenticate();
Debug.Log($"GameKit Authentication: player {player}");
// Grab the display name.
var localPlayer = GKLocalPlayer.Local;
Debug.Log($"Local Player: {localPlayer.DisplayName}");
// Fetch the items.
var fetchItemsResponse = await GKLocalPlayer.Local.FetchItems();
Signature = Convert.ToBase64String(fetchItemsResponse.GetSignature());
TeamPlayerID = localPlayer.TeamPlayerId;
Debug.Log($"Team Player ID: {TeamPlayerID}");
Salt = Convert.ToBase64String(fetchItemsResponse.GetSalt());
PublicKeyUrl = fetchItemsResponse.PublicKeyUrl;
Timestamp = fetchItemsResponse.Timestamp;
Debug.Log($"GameKit Authentication: signature => {Signature}");
Debug.Log($"GameKit Authentication: publickeyurl => {PublicKeyUrl}");
Debug.Log($"GameKit Authentication: salt => {Salt}");
Debug.Log($"GameKit Authentication: Timestamp => {Timestamp}");
}
else
{
Debug.Log("AppleGameCenter player already logged in.");
}
}
}
Notes on the script: Remember to wrap up the GameCenter code with platform-specific compilation if you are targeting other platforms as well.
The public method StartSignInWithAppleGameCenter() will be called from the click of the UI button and will check if the player is signed in, or not, to SingInOrLinkWithAppleGameCenter.
public void StartSignInWithAppleGameCenter()
{
SignInOrlinkWithAppleGameCenter()
}
private async void SignInOrlinkWithAppleGameCenter()
{
if (!AuthenticationService.Instance.IsSignedIn)
{
await SignInWithAppleGameCenterAsync(Signature, TeamPlayerID, PublicKeyUrl, Salt, Timestamp);
}
else
{
await LinkWithAppleGameCenterAsync(Signature, TeamPlayerID, PublicKeyUrl, Salt, Timestamp);
}
}
The sign in and link methods need to be included as well.
async Task SignInWithAppleGameCenterAsync(string signature, string teamPlayerId, string publicKeyURL, string salt, ulong timestamp)
{
try
{
await AuthenticationService.Instance.SignInWithAppleGameCenterAsync(signature, teamPlayerId, publicKeyURL, salt, timestamp);
Debug.Log("SignIn is successful.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
RefreshButtonText();
}
async Task LinkWithAppleGameCenterAsync(string signature, string teamPlayerId, string publicKeyURL, string salt, ulong timestamp)
{
try
{
await AuthenticationService.Instance.LinkWithAppleGameCenterAsync(signature, teamPlayerId, publicKeyURL, salt, timestamp, linkOptions);
Debug.Log("Link is successful.");
}
catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
{
// Prompt the player with an error message.
Debug.LogError("This user is already linked with another account. Log in instead.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
Connect the UI button to the script and build the sample, which will open Xcode and from there run on iPhone or iPad.
Check in the Unity Dashboard to confirm that the player was correctly signed in with the Apple Game Center credentials.
Check out our next article in the series.













