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?
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.
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!