Skip to main content

Distributed authority quickstart for Netcode for GameObjects

Use this guide to learn how to create your first distributed authority Netcode for GameObjects project. It walks you through the connection setup, including connecting to the distributed authority service, and adding basic gameplay.

Prerequisites

Before you begin, you need the following:

Install packages

  • Install the latest com.unity.services.multiplayer Multiplayer Services package.
  • Install the latest com.unity.netcode.gameobjects Netcode for GameObjects package.

Project setup

  1. Create a new Unity Project using the 3D template.

new unity project

  1. Ensure that the project is connected to a Unity Cloud project.

connect unity cloud

Access during alpha and beta

During alpha and beta, you need to request access to the distributed authority service. To do so, provide your Unity contact with the ID of the Unity Cloud project you created.

Connection setup

  1. Create a new script called ConnectionManager.cs. This script provides a user interface and connects to a distributed authority service session:
using System;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Multiplayer;
using UnityEngine;

public class ConnectionManager : MonoBehaviour
{
private string _profileName;
private string _sessionName;
private int _maxPlayers = 10;
private ConnectionState _state = ConnectionState.Disconnected;
private ISession _session;

private enum ConnectionState
{
Disconnected,
Connecting,
Connected,
}

private async void Awake()
{
await UnityServices.InitializeAsync();
}

private void OnGUI()
{
if (_state == ConnectionState.Connected)
return;

GUI.enabled = _state != ConnectionState.Connecting;

using (new GUILayout.HorizontalScope(GUILayout.Width(250)))
{
GUILayout.Label("Profile Name", GUILayout.Width(100));
_profileName = GUILayout.TextField(_profileName);
}

using (new GUILayout.HorizontalScope(GUILayout.Width(250)))
{
GUILayout.Label("Session Name", GUILayout.Width(100));
_sessionName = GUILayout.TextField(_sessionName);
}

GUI.enabled = GUI.enabled && !string.IsNullOrEmpty(_profileName) && !string.IsNullOrEmpty(_sessionName);

if (GUILayout.Button("Create or Join Session"))
{
CreateOrJoinSessionAsync();
}
}

private void OnDestroy()
{
_session?.LeaveAsync();
}

private async Task CreateOrJoinSessionAsync()
{
_state = ConnectionState.Connecting;

try
{
AuthenticationService.Instance.SwitchProfile(_profileName);
await AuthenticationService.Instance.SignInAnonymouslyAsync();

var options = new CreateSessionOptions(_maxPlayers) {
Name = _sessionName
}.WithDistributedConnection();

_session = await MultiplayerService.Instance.CreateOrJoinSessionAsync(_sessionName, options);

_state = ConnectionState.Connected;
}
catch (Exception e)
{
_state = ConnectionState.Disconnected;
Debug.LogException(e);
}
}
}
  1. Attach this script to a new object in your scene.

add connection manager

Netcode for GameObjects setup

  1. Create a new object in your scene called NetworkManager. Attach a Network Manager component to it.

add network manager

  1. Set Session Mode to Distributed Authority.

set session mode

  1. Under Network Transport, select UnityTransport from the list of options to add.

use unity transport

  1. Save any changes to your objects and scene.

Adding gameplay

  1. Create a new Script called PlayerCubeController.cs:
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;

public class PlayerCubeController : NetworkBehaviour
{
public float speed = 20;

private NetworkTransform _transform;

private void Start()
{
_transform = GetComponent<NetworkTransform>();
}

private void Update()
{
var movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
_transform.transform.position += movement * speed * Time.deltaTime;
}
}
  1. Create a new prefab called PlayerCube and open it for editing.
    • Attach the PlayerCubeController to the prefab. When prompted to add a NetworkObject, select Yes.
    • Attach a Network Transform component as well. Make sure all the Ownership flags are unchecked.

setup network transform

  1. Attach a child object in the prefab. Select the root of the prefab, right-click, and select 3D Object > Cube.

add the cube

  1. Save all changes to the prefab.

  2. Open the Network Manager, navigate to Prefab Settings, and set the Default Player Prefab to be PlayerCube.

set the prefab

  1. Save all changes to the scene.

Next steps

Hit play, fill out the Profile Name and Session Name, then click Create or Join Session. The profile name is the name of the player, so ensure this is different on every client. The session name is the identifier of the session you are joining, so this should be the same on every client. If everything has been set up correctly you will see the game create a session, connect to it, and spawn a PlayerCube.

If you create a build and connect a new profile to the same session you will see a second PlayerCube spawn and sync up with the first.

success