2 0 2 3
Getting started building multiplayer games with Unity
Arturo Nereu
Developer Relations
Unity Technologies
x.com/ArturoNereu
A story
Conker's Bad Fur Day - Rare Limited
What's your story?
The opportunity
77%
Steam
Apptopia
->
->
->
Of players play multiplayer games. [1]
Top sellers, usually multiplayer games. [2]
Most downloaded and revenue drivers are multiplayer games.[3]
[1] Unity Multiplayer Report 2022. [2] Us Top Sellers retrieved 08/16/2023. [3] Worldwide Top Free on iOS + Android, by Revenue retrieved 08/16/2023.
What is a Multiplayer game?
Definitions
Multiplayer Games
Asynchronous
Synchronous
->
->
Synchronous Multiplayer Games
My PC - UK
Remote PC - Canada
Synchronous Multiplayer Games
My PC - Denmark
Other Player's PC - USA
Single-player to Multiplayer
Multiplayer 'buckets'
Networking
Service Infrastructure
->
->
What does Unity offer?
Networking
Hosting and Services
->
->
Architecting your multiplayer game.
Moon, Aaron. Saintonge, Kiki. - How to build & design your multiplayer game based on genre. (2023)
Demo
Galactic Kittens
Netcode for GameObjects
using System;
using UnityEngine;
using Unity.Netcode;
public class PlayerShipMovement : NetworkBehaviour
{
// ...
void Update()
{
if (!IsOwner)
return;
HandleKeyboardInput();
MovePlayerShip();
}
// ...
}
Megacity
Demo
Magic 3
Unity Gaming Services
// Entry point requested by the game client
module.exports = async ({ params, context, logger }) =>
{
// Parse the received parameters
let playerMove = params.playerMove;
// Get opponent data (from Cloud Save)
let opponentMove = await cloudSaveAPI.getItems(...);
// Run the combat logic
const gameResult = computeResult(playerMove, opponentMove);
// Return the response with the result
return
{
opponentMove: opponentMopve,
playerMove: params.playerMove,
playStatus: gameResult
};
}
function computeResult(playerMove, opponentMove)
{
// combat logic
return gameResult;
}
CombatLogic.js
// ...
using Unity.Services.Authentication;
using Unity.Services.CloudCode;
public struct CloudCodeResponse
{
public string opponentMove;
public string playerMove;
public string playStatus;
}
public class CloudCodeManager : MonoBehaviour
{
public async void Awake()
{
await UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
public async void PlayMonster(int monsterType)
{
var arguments = new Dictionary<string, object> { { "playerMove", monsterType } };
var response = await CloudCodeService.Instance.CallEndpointAsync<CloudCodeResponse>
("CombatLogic", arguments);
// Show the player the results
_gameManager.InstantiateResults(response);
}
}
CloudCodeManager.cs
Thank you!
Arturo Nereu
x.com/ArturoNereu