A code along project to get familiar with Unity multiplayer game setups.
- Unity 6 Beta (6000.0.2f1)
- VContainer - A lightweight Unity dependency injection solution
- SceneReference - An inspector scene reference tool
- Mirror - A Unity network solution continued by the community.
- PlayFab Unity3D SDK - A Microsoft game backend solution.
- PlayFab Unity Editor Extension - A Unity editor extension for setting up PlayFab.
-
It would be nice to look at official tutorial resources
-
Kart models: download from Low Poly Models, use .blend files under
BLEND
folder. Make sure install Blender for Unity to convert them into assets. -
Input settings can be configured as scriptable objects, a good trick to use and define
InputAction
behaviour before creating them as reusable configurations. -
Useful command line helpers and use cases link: Here.
-
For Unity 2023.1.x or later, Multiplayer Play Mode is a better option for testing out server, host and client.
-
For Unity 2022.x.x or earilier, using command lines with log output for debug purposes.
- Log Server
<Path to Project>\<Game>.exe -logfile log-server.txt -mode server
- Log Client
<Path to Project>\<Game>.exe -logfile log-client.txt -mode client
The Debugging logs on NetworkVariableTest
works and outputs server uptime information. A DebugHelper
outputs debugging logs on the screen as well.
- Add
HelloWorldScene
toFile->Build Settings
. - Go to the built executable location. Run console commands for both server and client from
cmd
or Powershell. - If server is started first, it has nothing in the scene. Once a client starts, a player object will spawn in both games.
- Install
Multiplayer Tools
from Unity Package Manager Registry. Not mandatory, but very helpful for debugging and monitoring network traffic.- Profiler can be accessed from
Window
->Analysis
->Profiler
, and scrolldown toNGO Messages
andNGO Objects
sections. - Create an empty object in the scene and attach
RuntimeNetstatsMonitor
component for runtime data monitoring.
- Profiler can be accessed from
NetworkObject
carrier should not be in the scene. Network prefabs handles allNetworkObject
instances.- Any
MonoBehaviour
implementing aNetworkBehaviour
component can override the Netcode methodOnNetworkSpawn()
. TheOnNetworkSpawn()
method fires in response to theNetworkObject
spawning. - Having authority capability mapped with
IsServer
orIsClient
flag will help sync with the rest of the code about which side has authority.
bool HasAuthority => isServer; // can be set for your whole class or even project
// ...
if (HasAuthority)
{
// take an authoritative decision
// ...
}
if (!HasAuthority)
{
// ...
}
NetworkVariable
take Generics and serialize them intelligently. If using a constum data structure asNetworkVariable
type, it should implementINetworkSerializable
and itsNetworkSerialize
method.
public struct CustomData : INetworkSerializable{
public int Id;
public bool IsOwner;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T: IReaderWriter{
serializer.SerializeValue(ref Id);
serializer.SerializeValue(ref IsOwner);
}
}
[ServerRpc]
attribute marks events being sent to the server. The method is forced to haveServerRpc
suffix, otherwise it won't compile.[ClientRpc]
attribute marks events executed by the server and being sent to the client. he method is forced to haveClientRpc
suffix, otherwise it won't compile.
- Multiplayer Kart made by adammyhre