Matchplay: A Matchmaker and Multiplay sample
The Matchplay sample demonstrates how to create a Matchmake button: a basic networked client-server game with a matchmaking feature from end-to-end using the Unity Engine and Cloud Services SDK.
- Matchmaking Ticket Config: Players set their preferences for the map and game mode they want.
- Matchmaking: Players click the Matchmake button to begin looking for a match.
- Matchmaker Allocation Payload: The server gets information about the match and configures the server accordingly.
- Multiplay Server Allocation: Spins up a dedicated cloud server and passes its information to the player.
- Client Server Netcode: A lightweight server that can be hosted on Game Server Hosting.
The project runs in client or server modes and in local service modes. The server will attempt to find services for 5 seconds, and if it can’t, it starts the server with some default settings for local testing. The client can directly connect to this server using the local host IP address.
- bootStrap: You must play from this scene for the sample to work.
- mainMenu: Displays the matchmake and local connection buttons to users.
- game_lab: A game scene with a table.
- game_space: A game scene which features a sphere.
Run as a client
The project runs as a client in the following scenarios:
- You play the Matchplay project in-editor from the bootStrap scene.
- You run a non-server build on your platform of choice.
- You play a ParrelSync clone project with “client” in the arguments field from the bootStrap scene.
The client can either connect using the Matchmaker or through a local connection with the UI.
Matchmaker won't run without services being set up; follow the Sample Setup Guide to set them up.
Run as a server
The project will run as a server in the following scenarios:
- You run a server build on your platform of choice.
- You run a ParrelSync clone project with “server” in the argument field from the bootStrap scene.
- Gamer Server Hosting will run the server automatically when the matchmaker finds a match.
Depending on the context the server is running in, it will either fetch its configuration from the matchmaker or start with the default values.
Test Netcode locally with ParrelSync
To test Netcode locally with ParrelSync:
- Go to the top bar and select ParrelSync > Clones Manager > Add new Clone. This duplicates your project folder and synchronizes the contents to allow for iteration.
- Input "server" in the arguments field so the clone plays in local server mode.
- Once the server starts, return to the base project and select Play.
- Once the mainMenu scene loads, select local and Play to connect to the local server.
Cloud project and organization
To use Unity’s Multiplayer services, you need a cloud organization ID for your project. Follow the How do I create a new Organization guide to set up your org.
To learn how to connect your project with services, follow the Setting up Project Services guide.
Matchmaker and Multiplay depend on Unity Authentication 2.0 for credentials. This sample uses Unity Authentication’s anonymous login feature to create semi-permanent credentials unique to each player but do not require developers to maintain a persistent account for them.
The Matchmaker service allows players to search for other players with the same preferences as them and puts them in a match together.
The Matchmaker documentation contains code samples and additional information about the service. It includes comprehensive details for using the Matchmaker along with additional code samples, and it might help you better understand the Matchplay sample.
Manage the Matchmaker service in the Unity Cloud Dashboard.
Game Server Hosting (Multiplay)
The Game Server Hosting service hosts game servers in the cloud to allow for easy connection between players from around the world with the best ping performance possible.
The Game Server Hosting documentation contains code samples and additional information about the service. It includes comprehensive details for using Game Server Hosting along with additional code samples to help you better understand the Matchplay sample.
Manage the Game Server Hosting service in the Unity Cloud Dashboard.
Sample setup guide
Link your Editor project to the Cloud Project as described in Cloud Project and Organization.
To build the server, go to your Matchplay project, and select BuildTools > Linux Server.
It should automatically build your project as a server build, and output it to:
Upload the server to Multiplay and configure server hosting. Go to your Unity Cloud Dashboard > Multiplay Setup Guide > Create a build.
Fill out the Details fields and move to Upload Files. Drag your Linux Headless Build into the dropbox and select Upload Files.
Continue to set up the build configuration. Complete the fields and enter Matchplay.x86_64 in the game server executable field.
Select SQP as your query type, and fill in the following as the custom launch parameters:
-ip 0.0.0.0 -port $$port$$ -queryPort $$query_port$$ -logFile $$server_log_dir$$/matchplaylog.log
To create a fleet, enter a fleet name, and select the previously created build configuration. For the scaling settings, select 1 as the minimum available, and 5 as the maximum. The Game Server Hosting fleet is now ready.
Now that we have our server fleet, we can set up the Matchmaker by selecting Matchmaker Setup Guide. You can click through Integrate Matchmaker for this guide, as the sample already has it integrated in the project.
Select Create Queue and enter a name for the queue. The queue name must be between 1 and 36 characters and contain only alphanumeric or hyphen characters.
Name the queue 'casual-queue' and set the maximum players on a ticket to 10.
The exact string queue name defined in the UDash must match the input string in the following SDK sample:
To create a pool, select your previously created Game Server Hosting fleet and build configuration. Set the timeout to 15 seconds.
The match rules are the filters that we use in the sample to match the players by their preferences. Within the pools and queues, every player’s settings are evaluated against every other player.
To set up the match definition rules:
- Set up the region you are playing from. This should be the same as your server fleet region.
- Set up the basic team definitions and skip the advanced rules.
- Select Finish to finalize the configuration of the matchmaker.
Now you should be able to play the Project in the Unity Editor and have the Matchmake button find you a match, connect you to a Game Server Hosting server, and await more players.
You can use ParrelSync or multiple builds to connect several people to the same server using the Matchmake button.