Creating a Cross-Platform Multi-Player Game in Unity — Part 2

In the second part of this tutorial, you’ll write the code to have users connect with each other and then get a race started! By Todd Kerpelman.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 6 of this article. Click here to view the first page.

Running on a Second Device

It’s quite difficult to test a Unity multiplayer game on a real device and the iOS simulator at the same time, so to make your life easy you’ll use two physical devices to test your multiplayer functions.

It’s safe to assume that since you’re reading this on RayWenderlich.com (where the iOS tutorials outnumber the Android ones by about 60 to 1), your second device also runs iOS. In that case, you can skip the section below. For those of you who want (or need) to run this on an Android device, read on!

Running on Android

If you’ve never run an Android app before, I recommend you read through the excellent “Make Your First Android App” series by Matt Luedke, as this section only presents a brief summary of the steps required.

Download and install Android Studio from http://developer.android.com/sdk/installing/index.html?pkg=studio. Click the Check for updates now text at the bottom of the welcome screen to ensure you have the latest version installed.

Next, get the latest version of the SDK: at the welcome dialog, click on Configure\SDK manager. Make sure you have the latest version of:

  • Android SDK Tools
  • Android SDK Platform-tools
  • Android SDK Build-tools
  • The Android API which corresponds to the device you have. Not sure what device you have? Go to Settings\About phone and look for the Android Version number.
  • Android Support Library
  • Google Play Services

If the status of any of these items is Not installed or Update available, simply check the box next to the item and click Install xx packages… as shown below:

Updating Android Things

Accept all the licenses as you’re prompted, and you’re good to go! Now that you’ve installed the requisite SDKs, you can quit Android Studio.

Note: If you used Eclipse in the past and were left a little underwhelmed, give Android Studio a try; it’s turning out to be quite a nice product.

Make sure USB debugging is turned on for your device, as noted in Matt’s tutorial:


If you have a device, you don’t need any silly provisioning profiles. You just need to turn on USB debugging for your device. Sometimes the checkbox option is available just by going to Settings > Developer Options on your device. Check these instructions for more details.

Other times, you have to do some weird shenanigans. I can’t make this up! A direct quote from Android: “On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.”

Head back to Unity and hook up your device to your computer. Select Unity\Preferences then select External Tools from the dialog that appears. Ensure the Android SDK location is pointing to the right place: on my machine, it’s in /Applications/AndroidStudio/sdk/ but your setup might differ.

Select Google Play Games\Android Setup…; in the dialog box that appears, you may already see the Application ID for your app. If it’s not there, re-enter it:

Unity Android Setup Dialog

If your Application ID isn’t in the dialog box, and you don’t remember what is, here’s how to find it again:

Application ID location

  1. Go back to the Play Developer console at https://play.google.com/apps/publish/
  2. Click on Game Services (the little controller icon) on the left
  3. Click on your game
  4. Look at the name of your game at the top of the screen. Next to it should be an 11-or-12 digit number:
  5. Application ID location

  6. Copy-and-paste this value and then continue with the tutorial!

Click Setup and after a moment or two you should see a confirmation dialog that everything has been set up correctly. Next, select File\Build Settings\Android\Switch Platform to make this the default platform.

Click Build and Run, and Unity will prompt you for a filename for your Android apk file; choose whatever you’d like, but CircuitRacerAndroid is a good suggestion. Unity then compiles the app, transfers it to your device, and launches it!

If you followed the above steps, you should see Circuit Racer running on your Android device. Try out the single player version of the game!

It's Alive!!!

Open up a terminal window and type:

adb logcat | grep Unity

This reports back any debug output from your device. You don’t even need to restart your app to view the logs!

I/Unity   ( 5914):  [Play Games Plugin DLL] 11/21/14 11:04:20 -08:00 DEBUG: Starting Auth Transition. Op: SIGN_IN status: ERROR_NOT_AUTHORIZED
I/Unity   ( 5914):
I/Unity   ( 5914): (Filename: ./artifacts/AndroidManagedGenerated/UnityEngineDebug.cpp Line: 49)
I/Unity   ( 5914):
I/Unity   ( 5914):  [Play Games Plugin DLL] 11/21/14 11:04:20 -08:00 DEBUG: Invoking user callback on game thread
I/Unity   ( 5914):

Return to the main menu of the game and tap on the multiplayer option. You’ll likely be prompted to sign in, but the attempt will probably fail in a few moments:

Don’t panic — you simply haven’t set up your Client ID for Android as you did for iOS in Part 1 of this tutorial.

Go to the Play Developer console (http://play.google.com/apps/publish/) and select the Game Services icon. Select your app, then click on Linked Apps. Click Link another app then select Android:
Link Another App

Give this a name such as Circuit Racer Debug. Most developers create two Client IDs: one for debug releases, and one for production. Under Package Name, use the sample BundleID you’re using for the iOS version. Developers in the real world often like to add “.debug” to the end of this, but you’ll just keep things simple for the sake of this tutorial.

Turn on Real-time multiplayer, just as you did for the iOS version. Your screen should look like this:

Finished "Link Another App" screen

Click Save, then Continue, and finally click Authorize your app now.

Next you’re prompted for your package name, which should be filled out for you, and a signing certificate fingerprint. Hmm, that’s new; how do you get that?

Execute the following command in Terminal:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v

When prompted for your password, enter android, which admittedly is a terrible password, but good enough for the purposes of this tutorial. :]

You’ll see the following output:

Certificate fingerprints:
     MD5:  (Lots of hex numbers)
     SHA1: (Even more hex numbers)
     SHA256: (Yet more hex numbers)
     Signature algorithm name: SHA1withRSA
     Version: 3

Copy the hex string next to the SHA1 entry and paste it into the Signing certificate fingerprint field in the dialog box as shown:

Fingerprint added

The above steps tell Google Play’s servers that “I am the owner of com.<mycompany>.CircuitRacer, and here is a giant random number associated with me that basically guarantees it.” At this point, nobody else can claim to own a Client ID for com.<mycompany>.CircuitRacer.

Finalyl, click Create Client and you’re done!

Build and run your game again; note that in the Android world, you can select Replace when saving your apk, since there are no additional post-process steps as there are the Xcode world.

This time around, you should be able to sign in to your multiplayer game; make sure you’re using a different Google account than the one you’re signed into on your iOS device, and that has also been listed as a Tester account. You’ll soon join the multiplayer lobby for your game.

Todd Kerpelman

Contributors

Todd Kerpelman

Author

Over 300 content creators. Join our team.