(v0.50) DOTS Tutorial - Build a Multiplayer AR app
  • What is the Purpose of this DOTS Tutorial?
  • Important Notes For Using This Gitbook
  • Unity ECS
    • Intro to Unity ECS
    • Create a Unity ECS Project
    • Spawn and Move Prefabs
    • Spawn and Move Players
    • Spawn Bullets and Destroy Player
    • Publish Builds in Unity ECS
  • Unity DOTS Physics
    • Intro to Unity DOTS Physics
    • Use DOTS Physics for Prefabs, Players, and Bullets
    • Use DOTS Physics for Collisions
  • Unity DOTS NetCode
    • Intro to DOTS NetCode
    • Create a Network Connection using DOTS NetCode
    • Load a Game using DOTS NetCode
    • DOTS NetCode and Prefabs
    • DOTS NetCode and Player Prefabs
    • Use DOTS NetCode for Collisions and Destroying Bullet Prefabs
    • Dynamically Changing Ghosts Between Interpolated and Predicted
  • UI Builder and UI Toolkit
    • Intro to UI Toolkit
    • Create a ScreenManager
    • Style a View
    • Create a ListView
    • Responsive Game UI
    • Navigate Between Scenes Using ClientServerBootstrap
  • Multiplayer (NetCode+, UI Toolkit+)
    • Intro to Unity NetCode Multiplayer
    • Host or Join a Multiplayer Session on LAN
    • Broadcast a LAN Multiplayer Game
    • Keep Score and Update Game UI
    • Send Ghosts with NetCode Using Relevancy
  • AR Foundation
    • Intro to AR Foundation
    • Set Up AR Foundation and ARKit
    • Spawn a Player using AR Foundation
    • Update UI using AR Foundation
Powered by GitBook
On this page
  • What you'll develop in the Multiplayer section
  • Functionalities included
  • How we'll handle finding/joining/hosting games
  • Joining and hosting games
  • Finding games
  • Linking DOTS and MonoBehaviour
  • Unity resources
  1. Multiplayer (NetCode+, UI Toolkit+)

Intro to Unity NetCode Multiplayer

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

PreviousNavigate Between Scenes Using ClientServerBootstrapNextHost or Join a Multiplayer Session on LAN

Last updated 2 years ago

What you'll develop in the Multiplayer section

Github branch link:

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.

Finding games

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

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

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

We will be using .NET's 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 class to create listening threads for broadcast packets so it does not interrupt Unity's threading.

Unity documentation for NetCode 0.50.01-preview.19: Refer to this for more information on NetCode.

Unity samples for NetCode: This is the official Unity Asteroids sample that we based our gitbook off of.

Unity thread for NetCode: Unity is responsive here.

Unity documentation for UI Builder 1.0.0-preview.18: Refer to this for more information.

Unity documentation for UI Toolkit: Refer to this for more information on UI Toolkit.

Microsoft's documentation for UdpClient: Refer to this for more info on broadcasting.

Microsoft's documentation for Thread: Refer to this for more info on multi-threading.

Join our Discord for more info
DontDestroyOnLoad()
"manager scene" by using additive scene loading
UdpClient
Thread
https://docs.unity3d.com/Packages/com.unity.netcode@0.50/manual/index.html
https://github.com/Unity-Technologies/multiplayer
https://forum.unity.com/forums/dots-netcode.425/
https://docs.unity3d.com/Packages/com.unity.ui.builder@1.0/manual/index.html
https://docs.unity3d.com/2020.3/Documentation/Manual/UIElements.html
https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=net-5.0
Join our Discord for more info
https://github.com/moetsi/Unity-DOTS-Multiplayer-XR-Sample/tree/GhostRelevancyMode-and-Clean-Up
Ability to join a LAN game through broadcasting and receive ghosts within a certain radius