Creating a Cross-Platform Multiplayer Game in Unity — Part 1
Learn how to create a cross-platform multiplayer game in Unity in the first of a fourth part tutorial. By Todd Kerpelman.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Creating a Cross-Platform Multiplayer Game in Unity — Part 1
40 mins
- Getting Started
- Running on a Real Device
- Understanding Circuit Racer
- Introducing Google Play Game Services
- Setting Up Google Play Game Services
- Registering Your Game
- Turn-Based vs. Real-Time: A Quick Primer
- Adding Testers
- Installing the Unity Plugin
- Adding Missing Frameworks
- Adding More Frameworks
- Adding the -ObjC Flag
- Signing in the Player
- Improving the Sign-In Process
- Adding a Sign-Out Button
- Where to Go From Here?
Improving the Sign-In Process
Stop the application and start it again in Xcode. Notice that you’re no longer signed in, but when you click the Multiplayer button this time around, you’ll see the “welcome back” message without having to go through the sign-in process. What’s going on?
If you’ve signed in previously, haven’t explicitly signed out, and the game isn’t asking for any new permissions, then Google Play Games assumes it’s okay to sign you in again without showing you another sign-in dialog, which would get pretty annoying over time.
You can take advantage of this fact by “silently” signing the user in when the application starts. Go back to your MultiplayerController.cs
script and add the following new method:
public void TrySilentSignIn() {
if (! PlayGamesPlatform.Instance.localUser.authenticated) {
PlayGamesPlatform.Instance.Authenticate ((bool success) => {
if (success) {
Debug.Log ("Silently signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
} else {
Debug.Log ("Oh... we're not signed in.");
}
}, true);
} else {
Debug.Log("We're already signed in");
}
}
This looks awfully similar to the code in SignInAndStartMPGame
; the difference is that you have a true
argument at the end of your Authenticate
call, which instructs the Play service to try to sign the user in silently. That is, Google Play will check to see, “Is this a case where I can sign the user in without having to show them a dialog?” If so, it will sign the user in silently.
You’ll need to call this from somewhere. Add the following line to the end of Start()
in MainMenuScript
:
MultiplayerController.Instance.TrySilentSignIn();
Build and run your project from within Unity; this time, the application should start up and, after a moment or two, you’ll see the welcome back notification and the console text indicating that you’re signed in. Tap the Multiplayer button and you should see a line in your console log noting that you’re already signed in.
Adding a Sign-Out Button
Since you’re knee-deep in the authentication code, you may as well add a sign-out button too. You wouldn’t typically put such a button on the main screen, as people tend to sign out infrequently enough that you could put this in a settings screen somewhere. However, I find it useful to quickly sign in and out of the app while testing during development.
Add the following public variable to the top of your MainMenuScript
class:
public Texture2D signOutButton;
This public variable will hold a reference to your button image.
Next, add the following code to the end of OnGUI()
, outside of the for
loop:
if (MultiplayerController.Instance.IsAuthenticated()) {
if (GUI.Button(new Rect(Screen.width - (buttonWidth * 0.75f),
Screen.height - (buttonHeight * 0.75f),
buttonWidth * 0.75f,
buttonHeight * 0.75f), signOutButton)) {
MultiplayerController.Instance.SignOut();
}
}
This code checks if there is currently a signed-in user; if so, it displays a button to sign the user out. If the player taps that button, the app calls SignOut()
on your MultiplayerController
.
Next, you need to implement those two new methods that you’re calling in your MultiplayerController
; fortunately, they’re pretty straightforward. Go back to MultiplayerController.cs
and add the following two methods:
public void SignOut() {
PlayGamesPlatform.Instance.SignOut ();
}
public bool IsAuthenticated() {
return PlayGamesPlatform.Instance.localUser.authenticated;
}
Note: Are you wondering why you’re going through the trouble of calling SignOut()
in MultiplayerController
, which then calls SignOut()
on the Play Games platform — instead of simply calling PlayGamesPlatform.Instance.SignOut()
directly in your MainMenuScript
?
This is because you’re trying to follow the general principle of encapsulation (or “information hiding”) and keep all related logic together in one class, rather than spread it out over many classes. If you wanted to swap out Play Games for a completely different multiplayer service, you’d only have to modify MultiplayerController
; you wouldn’t have to change any of your other code.
Note: Are you wondering why you’re going through the trouble of calling SignOut()
in MultiplayerController
, which then calls SignOut()
on the Play Games platform — instead of simply calling PlayGamesPlatform.Instance.SignOut()
directly in your MainMenuScript
?
This is because you’re trying to follow the general principle of encapsulation (or “information hiding”) and keep all related logic together in one class, rather than spread it out over many classes. If you wanted to swap out Play Games for a completely different multiplayer service, you’d only have to modify MultiplayerController
; you wouldn’t have to change any of your other code.
Go back to Unity and open up the MainMenu scene; click on MainMenuGameObject
in the hierarchy panel, and you’ll see that the Main Menu Script now has a Sign Out Button entry that is currently undefined. Click on the circle next to the input text field and select btn-signout from the game’s assets as shown below:
Build and run your app; test the sign-in and sign-out process a few times to make sure it works consistently. The sign-out option will come in quite handy as you test your app with multiple players down the road.
Where to Go From Here?
The end result of all your hard work is that a lot of the setup work is done and you can finally get around to the fun parts of building a multiplayer game.
In Part 2 of this tutorial series, you’ll tackle the actual multiplayer aspects of the game. You’ll add matchmaking capabilities so you can connect your games client with other players across the room — or across the world. You’ll add the opponent’s car to the racetrack, and you’ll begin to send and receive gameplay messages so that you can get these cars racing!
You can download the completed project for this part over here. Just make sure add your own Google API Client ID.
In the meantime, if you have any questions or comments, please feel free to join in the discussion below!