For some time I have struggled to get Google Play Services working in my Unity game. There is so much to install and configure but I didn't find one single guide which showed how to do it right from the start to the end. In this article I will guide you through the entire process, right from installing tools, configuring Unity and setting up Google Developer Console and Game Services. Hopefully I can save you some pain.

How to add Google Play Services to your Unity game

In this end-to-end guide I will show you how to setup Google Play gameservices. Right from configuring the Google part up to testing the services on a real device. If you follow my advice you would not even to have rewrite your code for iOS Game Center services. For that I will use Easy Mobile asset. I am not affiliated with the company behind the plugin but I have found it much more useful than downloading the free SDK’s or by purchasing Prime31 plugins. I have used those with two of my previous games and even used Gamespark in my latest game but with Easy Mobile Pro Asset things are very easy to accomplish. The Lite version is even for free. So, don’t struggle for too long with you Google SHA1 keys, oAuth settings or whatever issue you are running into. Just fire up Unity, create a new project, switch to the Android target platform and then start reading this guide. Follow it closely and you will not have any issue at all testing Google gameservices, turn based or realtime, leaderboards and achievements in just under an hour on a real device. Enjoy!

Add your game to Google Play Gameservices

• Browse to your Google Play console at: https://play.google.com/apps/publish
• From the sidebar menu choose Gameservices
• Select the button Add new game
• In the dialog make sure that you are on the tab ‘I am not using Google Game API in my game’
• Enter the name of your game
• Select the category of your game
• Choose button Continue

Your game, under Gameservices, will be created. When it is finished you will see a new sidebar menu with various options like Missions, Game details, Linked apps etc.

Prepare your game for development and debugging

The first thing we are going to do now is to create two linked apps. One linked app will be used to connect to gameservices when you build and run your game to a connected device directly from the Unity editor. This could be a physical device you connect through USB or by running a simulator. Simulators can be started through the Android Studio application. When you launch Android Studio you will see a little cogwheel at the buttom of the startup screen. There you can select the AVD manager which you can use to create a simulator and launch it as well. Make sure you select a simulator which has the Google Play gameservices icon next to it else you won’t be able to test the Gameservices.

Create your first linked app

Ok, let’s start creating the first linked app used to debug the game on connected devices.

• From the sidebar select Linked apps
• Click on the Android icon
• At package name enter something like: com.companyname.appname. Of course you select your own country code, company name and game name here. 😉 Important: write this down. You cannot change it later on. Secondly, you don’t need to select an existing app here
• Now you need to enable turn based or realtime services by toggling the switches.
• If you want you can enable anti piracy as well.
• Scroll to the top of the screen and select Save and continue
• The next thing we are going to do is to authorise your app. Click the button Authorise my app now
• At the oAuth dialog you need to paste your SHA1 fingerprint. As we are currently linking the app for debug purposes, we need the SHA1 key from the Android debug keystore. The keystore is a file on your computer. On a Macbook it is in the users folder. To get it follow these steps:
○ On Macbook, use the Finder, Go to folder: ~/.android/
○ There you will see a file called debug.keystore
○ Now, open a terminal window. You can do that on Macbook by choosing Command button + Spacebar. Then type Terminal to open a terminal window.
○ Your terminal window will by default open at the users folder (~)
○ So you still need to browse to the .android folder: cd .android
○ Next you will need to retrieve the SHA1 fingerprint from the debug.keystore file: keytool -list -v -keystore debug.keystore
○ It will ask for the keystore password which is android by default
○ It will then output the SHA1 key. Select it with your mouse and copy it to the clipboard. Then back to our browser Google Play page paste the SHA1 key
○ Finally click Confirm
○ It will now show your client-id. You are probably not going to need it but it would be wise to copy it and store it somewhere safe. Add a note telling that this ID is your linked app for debugging purposes.

Congratulations, you now have created your first linked app.

Prepare your game for testing or releasing through Google Play Store

We need to create another one linked app. The reason is that when you want to upload your game to Google it cannot work with your debug keystore. Instead you have to create your own keystore which will be used for uploading your game to Google. By uploading the game you can test it through Google Play store tracks like internal testing track, alpha track, beta track or your live production track.

So to be able to linked another app (same package name) for upload purposes we need to create a keystore. Luckily this is very easy right from the Unity editor. You can do it through Android Studio or through terminal as well but I have found the Unity way the easiest. Make you sure you are on the latest Unity version (2019.1++). Follow these steps:

Create a keystore

• Start Unity and load your game project
• On Macbook go to the player settings. You can hit Command + Shift + B and select the button Player settings there.
• First of all expand the section called Other settings
• There you will see your package name. Enter the same package name you have used when configuring your first linked app at Google Play gameservice e.g. com.mycompany.appname.
• Next expand the Publishing settings
• There you will see button called Keystore manager
• In the keystore dialog choose the dropdown and select New keystore -> dedicated location
• I prefer to save the keystore to the same location as my project. From a security perspective this might not be the wisest thing to do. However, make sure you create a safe (online) backup of the keystore and never lose it (including the password) because you won’t be able to recover it and hence, publish updates of your game to the store. Trust me, I have been there. 🙂 Select a name. Unity will automatically add the .keystore extension here.
• Now fill in the required fields: password (make a note of it!!!!)
• The Alias could be anything you like. I use the name of my game here.
• Next fill in the remaining fields: First and lastname, Organizational Unit (if you have any), Organization, City, State or Province, and Country Code (e.g. US)
• Click on Save and Unity will create the keystore for you. It will then ask if you want to set this as your project keystore. Choose Yes. And you are done with creating the keystore!

Now that you have your own keystore we are going to link another app for uploading purposes. Follow these steps:

Create your second linked app

• Go back to your browser page Google Play gameservices. If you are still on the last page after you created your first linked app you will see the option to Link another app. If not, browse to the gameservices -> Linked apps section and create a new one.
• Again select Android
• For package name select the same package name as you have used before and also specified in the Unity player settings under Other settings e.g. com.mycompany.appname.
• Again, select the requied turn based or real time feature and anti piracy settings.
• Scroll to the top and select Save and continue
• Next click on Authorise my app
• After that you need to specifiy your SHA1 key. Unlike the previous time we don’t extract it from the Android debug keystore but from the upload keystore you have created before from the Unity editor. Follow these steps:
○ Open a terminal window (on Macbook) or a command prompt on Windows.
○ Browse to the location where you have saved your keystore. In my case it is in the project folder.
○ Then extract the SHA1 key by running the same keytool command as before: keytool -list -v -keystore
○ Copy the SHA1 key to the clipboard and back in your browser window paste it into the SHA1 text field.
○ Click Save

Super! You have now created your second linked application. Make sure you record the second client id as well and safely backup your keystore and password as it is not recoverable.

Did you see the app id on the page as well? You need to write down that one as well. It will be shown after you create your linked app. Did you miss it? No problem, choose gamedetails from the sidebar and you will find your app id beneath your game title.

Add testers

No matter if you are deploying your game through USB connected device or through an uploaded internal test track you will need to add testers else the signing in will fail.

• In Google Play -> Gameservices select Testing
• Click the button Add testers
• Add comma separated email addresses and click Save

Add Gameservices SDK to your game

You will need to add Google Play Gameservices SDK to your game. This can be somewhat difficult based upon the choices you are going to make.
First of all, ask yourself if your game will be released on Android only or on iOS as well. If you are releasing them on both platforms then you have to wire up the Apple Gamecenter services as well. They look very similar but have some differences as well.
On Android you could browse to github and download the Google Play gameservices SDK yourself and follow the included instructions. There is a another Github which contains for instance the turn based features.
Another option would be to buy third party plugins. Prime31 offers plugins for Gamecenter and Google Play gameservices.

But here is my advice!

Easy Mobile Pro vs. Free SDK

You don’t want to create an iOS codebase and Android codebase for features which are nearly identical. There is another third party plugin you can buy directly from the Unity Asset store which adds another layer on top of these SDKs so that you can focus on developing your game instead of figuring out all the of the SDK differences. The plugin is from Easy Mobile and is really a life saver!

I am in no way affiliated with that company but I can highly recommend it. Part of the asset are example scenes you can deploy to a device. Just change the package name to the one you have choosen in the previous steps and already you can test the services through a UI or even trough a real turn based or realtime racing game.

Install Easy Mobile Pro

Buy and install Easy Mobile Pro here: https://assetstore.unity.com/packages/tools/integration/easy-mobile-pro-75476

• Open your Unity project
• Double click the Easy Mobile Pro package from your download location or through the Asset store store window click on My Assets and Import the package from within Unity.
• Unity will show you a dialog with all the files it is about to import. I have selected everything because I want to start with the demo scene first so that I know for sure Gameservices have been configured correctly. Choose Import again.
• Give it a while to compile everything. Watch the spinner at the bottom right of the Unity window.
• The first question it will ask if you want to enable Android auto resolution. Choose Enable

Configure Easy Mobile Pro

Let’s start configuring Easy Mobile Pro. Before we do that I would recommend going to Google Play Gameservices first and add one leaderboard. It enables us to download a certain resource file which we will need later on.

Create a leaderboard first

• Back to your browser. At Google Play gameservices select Leaderboards from the sidebar.
• Then select the button Add Leaderboard.
• Enter a name for your leadboard e.g. Highscores
• The other fields are yet not neccessary to fill out so click Save
• Now look closely to the page where it says something like Download resources. It is a link you can click. It will open a dialog where you can export your resources. This is nothing more than a piece of XML containing your app id and your leaderboard id.
• Make sure the Android tab is selected and copy the contents to the clipboard or notepad. Keep it safe, we are going to need it soon.

Back to our configuration of Easy Mobile Pro in Unity!

Continue the configuration of Easy Mobile Pro in Unity

• From the toolbar select Window -> Easy Mobile -> Settings
• Toggle the Game Services switch to On
• It will say that you need the Google Play Games plugin. Click on the button Download Google Play Games Plugin
• It will open another browser window pointing to GitHub. There you will see a button called Clone or Download. Select Download zip.
• The ZIP file will be downloaded to your computer. Unzip it. On Macbook just double click the file.
• It will create a folder. Inside that folder you browse to current build. There you will find the Unity package. Double click the package to install it. Make sure your Unity project is still open.
• In the import dialog select all file and Import. You will probably need to click on OK onece.
• Again, Unity needs some time to compile the files. Watch the spinner!
• After while it will ask if you want to delete the obsolete files. Choose Yes.
• And again we will need to wait for the spinner to complete. A dialog will pop up which will download Android dependencies. It will close automatically.

If everything goes well you will now see the wonderful Easy Mobile Pro settings inspector window for Game Services. Let us continue!

• Under Easy Mobile settings -> Game Services -> Google Play Games setup section you will see a tekst field called Web Client Id. We are still developing our game so we would like to debug on connected devices. I always paste here the first client id we obtained when we created our first linked app. Later on this would be replaced by the second client id when we start uploading the game to Google Play for internal testing, alpha, beta or production tracks. Some people claim it is not needed at all right now but it doesn’t hurt as well. So paste your first client id. If you don’t remember (shame shame) you can obtain it still from your Google Play Gameservices -> Linked apps section online.
• Next you need to paste the Android XML Resources. Remember? It is the piece of XML you could download after you had created your first leaderboard.
• Nearly there! Finally click the Setup Google Play Games button.

If done correctly you will see two popups. One confirming that Android resolution finished successfully. The second one telling you that Google gameservices has been configured successfully.

Ok, as we would like to build a game with multiplayer features and also a leaderboard we will need to configure those in Unity as well. Easy. Follow these steps:

• Under the same Easy Mobile settings -> Game Services section you will find a section called Multiplayer. Enable the tickbox.
• Under Leaderboards section click Add New Leaderboard.
○ You might wonder why this is needed as you have already created a Google leaderboard and added the resource definition (XML). Remember that Easy Mobile Pro is cross platform? Great! In your code you would like to report a score. But the leaderboard Ids you need to specify differ from iOS and Android. By creating a Easy Mobile leaderboard you can specify both Ids but in your code just refer to one. Easy Mobile will do the mapping for you!
• Name your leaderboard e.g. Highscores.
• Leave the iOS Id empty for now as this guide is for setting up the Google Play Gameservices. Under Android id select the leaderboard from the dropdown. If there is no leaderboard then you probably did not follow the steps where you would have needed to paste the XML snippet into the settings textbox of Easy Mobile.
• And then at the bottom you select the button Generate Constants Class

Pfew. Did you really make it until here! That is awesome. I really wrote this end-to-end guide just for that purpose. Ok, and also for myself as well. I keep forgetting these things. 😉

Let us continue to the fun section and try it out on a real device!

Test multiplayer gameservices on real device

You still have your Unity project open and connected a real device to your computer. We are now going to deploy a test scene included with Easy Mobile Pro to your device so that we can test if we have configured everything correctly.

I assume your Unity project has the Android as target platform.

Connect a real device

Ok, this might sound trivial but some people are still wondering why their device is not showing up in Unity as a valid deployment target.

• Connect your Android device through an USB cable.
• Then enable Options for developers
○ It is a hidden feature on your phone
○ Go to the Settings -> System -> About Phone
○ Find your Build number
○ It might seem strange but you have to tap the build number of couple of times until it tells you that developer options have been enabled. No kidding.
○ On some phones this might be under a different menu setting or you have to tap something different. Google your device to look for ‘enable developer mode’.
• When you connect your phone it might ask what you would like to do with your USB connection. I always select transfer files.
• You can check if everything works by going in Unity to Build settings (Command + Shift + B on Macbook).
• If your target platform is Android you will see a line ‘ Run device’ with a Refresh button next to it. Click the Refresh button
• After a while your device should appear in de drop down list of Run device

Build and run Easy Mobile test scene

• In select the folder Unity -> project view -> demo -> scenes
• The first scene you will see is the DemoHome
• Again, open the Unity Build settings and drag the DemoHome scene to the list of scenes. Make sure it is at the top.
• Next, open the Modules folder
• Select all scenes and drag those to the list of scenes in build settings as well

You have now defined which scenes to build and run. The first one in the list will be loaded … first. 🙂

From the DemoHome scene it will be possible to load all of the other scenes.

• Select your device from the Run Device drop down
• Select the option Development build. This determines whether you are using the android debug.keystore with the default android password or your own custom keystore created in the previous steps. If you have created the custom keystore in the previous steps then this has already been selected under Player settings -> Publishing settings as custom keystore meaning that your build will be using the second linked App in Google Play Gameservices called the upload or release build. If you watch closely during building you will see a validateRelease task appear. If you select the development build option then it will use your first linked app and you will have to configure the android debug.keystore under Publishing settings. During building your will see a validateDebug task appear. Got it? So when selecting Development build you will need to:
○ Go to Player settings -> Publishing settings -> Deselect custom keystore. It
• Finally, click the Build and run option from the Build settings but make sure your connected device is selected in the Run device dropdown.
• Unity will ask you where to store the build files and APK. I alway choose a different folder outside of my project folder.

Validate game services

The game will launch the DemoHome scene from Easy Mobile.

• As soon as the game launches you will see default Google UI popup which will initialize game services and automatically signs you in if you have Google Play app on your phone.
• Next, if you choose Game Services button you will see a screen where you can verify if you are logged in. If not, something is probably wrongly configured. After the Google Play services popup you should see your avatar popup appearing. If not you are not signed in.
• When the user is signed in you can select on of the various scenes to test your setup. We didn’t setup Achievements yet but that is similar to Leaderboards setup. Whenever you make changes to your Leaderboard or Achievements, download the resources snippet (XML) and paste it into the Easy Mobile Settings Android Resources field, Setup again and Generate constants again and you are all set.
• The kitchen sink scenes are very useful for testing the various API calls. But if you really want to see how a game is setup I would advice to look at the Tic Tac Toe game or the realtime Racing game. The racing game shows you how to call the Google API to do the match making, after that it will listen to messages. It will start with sharing the initial game state. When everyone has received the same state and initialized the game they will send ready events. When all are ready the game starts. Every movement is send as another message. When someone leaves the game a peer disconnected message is send. When someone finishes another message is send. Basically that is the whole concept: authentication, match making and sending and listening to messages.

Troubleshooting

If you follow this guide really closely, it should work right away. How do I know? Because I have followed this guide to configure a game from scratch to verify if it works.

Still things may go wrong. I know. Check out the following to see if anything might help:

Validate that the account you are using on your device is added as tester on Google Play gameservices.
• Open Play Games app on your device
• Click on the avatar at the top
• Write down the email address you see on the screen
• Browse to Google play gameservices -> Testing and verify that this email address has been added to the list of testers

Validate that you have selected the debug keystore when using Development build in Unity
• In Unity -> Build Settings -> Player Settings -> Publishing options -> make sure the custom keystore has been deselected when building and running your game using Development build option checked.

Thank you so far for reading this How to guide. If you feel something is wrong please let me know and I will update it. Thanks and good luck with your game!