Multiplayer games are indeed one of the most involving and addictive gaming experiences you could offer to anyone. Everything is simply more funny if you can challenge your friends! š®
Although nowadays Unity is hugely simplifying game development, when talking about making a real-time multiplayer game there are still some non-trivial concepts and different patterns to deal with āļø
We will explore some of the "quirks and features" that Unity offers to the developers in support of this task, from the ready to use Matchmaker down to bit-a-bit messages serialization š¾
Github repo for the demo: https://github.com/BelkaLab/unity-networking-demo
19. Server + Client 1
State sync: built-in objects
NetworkManager Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Client 2
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Client n
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Scene
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
20. State sync
The Network Identity component
controls a GameObjectās unique
identity on the network, and it
uses that identity to make the
networking system aware of the
GameObject.
ā¢ Unity-speciļ¬c objects (position, rotation,
animation..)
ā¢ Simple variables (int, ļ¬oat, ..)
ā¢ Everything serializable! (custom classes)
21. State sync
Sets the Unique ID of this
object on the network, so that
we know who has changes
23. Server + Client 1
State sync: built-in objects
NetworkManager Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Client 2
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Client n
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Scene
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
24. Server + Client 1
State sync: built-in objects
NetworkManager Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Client 2
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Client n
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player 2
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
Player n
ā¦āØ
ā¢ NetworkIdentityāØ
ā¢ NetworkTransform
NetworkTransformāØ
updates
Scene
25. State sync: built-in objects
This is cool!
How can I replicate it also for more
generic variables?
27. State sync: variables
The [SyncVar] attribute tells the
server that once the variable is
changed on the server, it should also
inform everyone else about it
28. State sync: pretty much
anything serializable!
ā¢ Basic types (integers, strings, ļ¬oats, ā¦)
ā¢ Unity types (Vector3, Color, ā¦)
ā¢ Serializable objects
ā¢ Structs made up by types above
29. State sync: pretty much
anything serializable!
ā¢ Basic types (integers, strings, ļ¬oats, ā¦)
ā¢ Unity types (Vector3, Color, ā¦)
ā¢ Serializable objects
ā¢ Structs made up by types above
Bonus: Lists of the primitive types above!
31. State sync: variables
Server + Client 1
NetworkManager Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 2
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Client 2
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player n
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Client n
Other scene objects
(terrain, camera, light)
Player 1
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 2
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player n
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Color SyncVarāØ
updates
Scene
Player 2
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player n
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 1
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
33. State sync: variables
Server + Client 1
NetworkManager Other scene objects
(terrain, camera, light)
Client 2
Other scene objects
(terrain, camera, light)
Client n
Other scene objects
(terrain, camera, light)
Scene
CmdChangeColor
Player 2
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player n
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 1
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 1
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 1
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 2
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player n
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player 2
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color
Player n
ā¦āØ
ā¢ ColorControllerāØ
-[SyncVar] Color