Building a Turn-Based Multiplayer Game with GameSparks and Unity : Part 3
Intermediate Multiplayer Tutorial

Building a Turn-Based Multiplayer Game with GameSparks and Unity: Part 3

Welcome to the third part of our GameSparks tutorial series. In parts one and two we have finished GameSparks configuration and this time we will focus on creating our client application in Unity.

Unity

Project Setup

Create a new project in Unity. Establish a folder structure (at this point you should have folders for at least Prefabs, Scripts, Scenes and Sprites). Import our assets package, which contains all needed sprites and fonts.

Reusable Code

We’ll occasionally use the singleton design pattern to easily obtain references to key objects. If you aren’t familiar with this pattern or generic classes in C#, see Singleton wikipedia page and Microsoft Generic Classes documentation page. Create a new Script called Singleton and paste in the following code:

We’ll also create a class that will simplify scene transitions. It’s good to create your own wrappers for this type of behaviour and use them exclusively, so you can later add features (loading screen, fade in/out) without having to change the existing code. Create a new Script called LoadingManager and paste in the following code:

GameSparks SDK

Follow the instructions at the official documentation page to import and configure GameSparks SDK.

There aren’t any Unity settings required specifically for GameSparks, but it’s always a good idea to enable Run In Background checkbox in Player Settings. It’ll ensure that we’ll receive all Messages and Responses, even when the application is minimized or not focused.

Run In Background Checkbox
Fig. 1: Run In Background checkbox in Player Settings.

Initial Scene Configuration

Create a Camera prefab to share its settings between the scenes.

Camera Prefab inspector.
Fig. 2: Camera prefab configuration.

Create three new scenes: Login, MainMenu and Game. Make sure that each one has a Camera and a Canvas object. Add them to the build settings in the following order:

1 - Login, 2 - MainMenu, 3 - Game.
Fig. 3: Build settings scene order.

Create a new prefab called GameSparksManager. Add GameSparksUnity component to it and make sure to set a reference to the settings asset.

GameSparksManager inspector window.
Fig. 4: GameSparksManager prefab configuration.

Then create a new script, called GameSparksSingleton. Add it to the GameSparksManager prefab. This will ensure that only one instance of GameSparksManager is present and it’s not destroyed after loading a new scene.

Add GameSparksManager prefab instance to the Login scene.

Authentication

Add two InputFields (username and password), two Buttons (login and register) and one Text (for error messages) to the Login scene. Make sure they are grouped under some RectTransform parent object and create a prefab.

Login scene game and hierarchy view.
Fig. 5: Login scene layout and hierarchy.

Create a new script called LoginPanel and add a LoginPanel component to the prefab. Add five UI reference fields to the LoginPanel. Assign them in the inspector.

LoginPanel inspector window.
Fig. 6: LoginPanel inspector window.

In Awake subscribe to the onClick events on both Buttons.

Add Login and Register methods.

Each of them creates a Request object, fills in any needed data and sends it to GameSparks. When using Send method you pass two delegates (callbacks), which will respectively be called when the Request succeeds or fails. When registration succeeds, we want the login request to be carried out for us automatically.

Add input blocking and unblocking to make sure additional Requests aren’t sent before we receive a Response. Complete code for LoginPanel looks like this:

Matchmaking

In this tutorial the MainMenu scene has only two purposes – to send a MatchmakingRequest when Play button is clicked and to await a ChallengeStartedMessage, which will be sent after players are matched and a Challenge is created on the backend.

Create a new, empty panel on the MainMenu scene’s Canvas. Call it MainMenuPanel or similarly. Add a Button to it.

MainMenu scene game and hierarchy view.
Fig. 7: MainMenu scene layout and hierarchy.

Create a new script called MainMenuPanel and add it to the previously created object. Save it as a prefab. Inside the script add a reference to the Button and assign it in the inspector.

In Awake subscribe to the onClick event on the button.

Add Play method.

Inside Play we create a MatchmakingRequest object, fill in any needed data and send it to GameSparks.

Now we need to intercept the ChallengeStartedMessage and when it is received, proceed to the Game scene. We also subscribe to the MatchNotFoundMessage, which will be sent when no match is found in available time.

Add input blocking and unblocking to make sure we only queue for the matchmaking once. Complete code for MainMenuPanel looks like this:

Summary

Now we have a fully functional matchmaking in our Unity client. In the final, fourth part of the tutorial we’ll show you how to set up the Game scene, send Move Events and implement the win conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 5 =