Intro to Unity NetCode Multiplayer

Code and workflows to find/create/join/leave LAN games with authoritative scorekeeping

What you'll develop in the Multiplayer section

Github branch link: https://github.com/moetsi/Unity-DOTS-Multiplayer-XR-Sample/tree/GhostRelevancyMode-and-Clean-Up

Functionalities included

  • Using launch GameObjects to configure server and client connections

    • Trigger starting a game as a NetCode host

    • Trigger starting a game but joining as a NetCode client to a specific IP address

  • Sending and receiving broadcast messages

    • Automatically send a broadcast UDP packet of game information including IP address on LAN for players to join

    • Listen and receive broadcast UDP packet information and populating a ListView of available LAN games

  • Run Threads to listen for UDP packets

    • Be able to run non-Unity handled threads in-game

  • Graceful handling of joining and leaving games

    • Add NetworkStreamRequestDisconnect component on both host and client when quitting a game

    • Handling timeouts by querying for NetworkStreamDisconnected components from host and client to clean up entities and return to title screen

  • Server-authoritative scorekeeping

    • Create PlayerScore ghosts and HighestScore ghosts to keep player scores authoritative and in sync

    • Associate scores with NCE NetworkIdComponent values

  • UI Toolkit + DOTS

    • Updating Game UI through a MonoBehaviour by pulling DOTS data from ghosts

  • Using GhostRelevancyMode to be mindful of network transmission

    • We will implement a server-side system that will send only send ghosts within a certain radius to players

    • We will also include the ability to override this behavior to send player scores to all players

  • UI Toolkit's new data binding to keep UI up-to-date with MonoBehavior variables

How we'll handle finding/joining/hosting games

Joining and hosting games

We must pass data between NavigationScene and MainScene in our project. This data includes:

  • Game name

  • Server IP

  • Player name

  • Broadcast port

These are the same values that are currently in our ClientLaunchObject and ServerLaunchObject in MainScene.

We will create ClientLaunchObject and ServerLaunchObject in NavigationScene before we transition to MainScene and use DontDestroyOnLoad() so they "survive" the transition between scenes.

Unity has started to recommend using a "manager scene" by using additive scene loading. This is a solid approach, but it's just a bit overkill for this tutorial so you won't see it in this gitbook.

Finding games

We will be using .NET's UdpClient class to send broadcast packets across the local area network's router. This will not work if the player is not connected to WiFi.

We will also be using .NET's Thread class to create listening threads for broadcast packets so it does not interrupt Unity's threading.

Linking DOTS and MonoBehaviour

NetCode will handle the tracking of scores of each player in the game. We want to take this data from ECS and populate the game UI which runs on MonoBehaviours.

In this section we will demonstrate our approach of pulling data from ECS into MonoBehaviours by using the EntityManager.

In the next AR Foundation section, we will also demonstrate how we provide data from MonoBehaviours (the AR Camera's "pose") to ECS.

With both of these techniques it is possible to use any classic Unity MonoBehaviours with ECS. (big deal!)

They key is to understand that a ECS System cannot "push" values directly into a GameObject; instead, a MonoBehaviour must "pull" data using an EntityManager. <- BIG INSIGHT

Unity resources

Unity documentation for NetCode 0.50.01-preview.19: https://docs.unity3d.com/Packages/com.unity.netcode@0.50/manual/index.html Refer to this for more information on NetCode.

Unity samples for NetCode: https://github.com/Unity-Technologies/multiplayer This is the official Unity Asteroids sample that we based our gitbook off of.

Unity thread for NetCode: https://forum.unity.com/forums/dots-netcode.425/Unity is responsive here.

Unity documentation for UI Builder 1.0.0-preview.18: https://docs.unity3d.com/Packages/com.unity.ui.builder@1.0/manual/index.html Refer to this for more information.

Unity documentation for UI Toolkit: https://docs.unity3d.com/2020.3/Documentation/Manual/UIElements.html Refer to this for more information on UI Toolkit.

Microsoft's documentation for UdpClient: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient?view=net-5.0 Refer to this for more info on broadcasting.

Microsoft's documentation for Thread: https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=net-5.0 Refer to this for more info on multi-threading.

Last updated