|
| 1 | +--- |
| 2 | +id: distributed-authority-quick-start |
| 3 | +title: Distributed authority quickstart for Netcode for GameObjects |
| 4 | +--- |
| 5 | + |
| 6 | +Use this guide to learn how to create your first [distributed authority](../terms-concepts/distributed-authority.md) Netcode for GameObjects project. It walks you through the connection setup, including connecting to the distributed authority service, and adding basic gameplay. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +Before you begin, you need the following: |
| 11 | + |
| 12 | +- An active Unity account with a valid license. |
| 13 | +- The [Unity Hub](https://unity.com/download). |
| 14 | +- A supported version of the Unity Editor. Refer to the [Netcode for GameObjects requirements](https://docs-multiplayer.unity3d.com/netcode/current/installation). |
| 15 | + |
| 16 | +## Install packages |
| 17 | + |
| 18 | +- Install the latest `com.unity.services.multiplayer` Multiplayer Services package. |
| 19 | +- Install the latest `com.unity.netcode.gameobjects` Netcode for GameObjects package. |
| 20 | + |
| 21 | +## Project setup |
| 22 | + |
| 23 | +1. Create a new Unity Project using the 3D template. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +2. Ensure that the project is connected to a Unity Cloud project. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +:::note Access during alpha and beta |
| 32 | +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. |
| 33 | +::: |
| 34 | + |
| 35 | +## Connection setup |
| 36 | + |
| 37 | +1. Create a new script called *ConnectionManager.cs*. This script provides a user interface and connects to a distributed authority service session: |
| 38 | + |
| 39 | +```cs |
| 40 | +using System; |
| 41 | +using System.Threading.Tasks; |
| 42 | +using Unity.Services.Authentication; |
| 43 | +using Unity.Services.Core; |
| 44 | +using Unity.Services.Multiplayer; |
| 45 | +using UnityEngine; |
| 46 | + |
| 47 | +public class ConnectionManager : MonoBehaviour |
| 48 | +{ |
| 49 | + private string _profileName; |
| 50 | + private string _sessionName; |
| 51 | + private int _maxPlayers = 10; |
| 52 | + private ConnectionState _state = ConnectionState.Disconnected; |
| 53 | + private ISession _session; |
| 54 | + |
| 55 | + private enum ConnectionState |
| 56 | + { |
| 57 | + Disconnected, |
| 58 | + Connecting, |
| 59 | + Connected, |
| 60 | + } |
| 61 | + |
| 62 | + private async void Awake() |
| 63 | + { |
| 64 | + await UnityServices.InitializeAsync(); |
| 65 | + } |
| 66 | + |
| 67 | + private void OnGUI() |
| 68 | + { |
| 69 | + if (_state == ConnectionState.Connected) |
| 70 | + return; |
| 71 | + |
| 72 | + GUI.enabled = _state != ConnectionState.Connecting; |
| 73 | + |
| 74 | + using (new GUILayout.HorizontalScope(GUILayout.Width(250))) |
| 75 | + { |
| 76 | + GUILayout.Label("Profile Name", GUILayout.Width(100)); |
| 77 | + _profileName = GUILayout.TextField(_profileName); |
| 78 | + } |
| 79 | + |
| 80 | + using (new GUILayout.HorizontalScope(GUILayout.Width(250))) |
| 81 | + { |
| 82 | + GUILayout.Label("Session Name", GUILayout.Width(100)); |
| 83 | + _sessionName = GUILayout.TextField(_sessionName); |
| 84 | + } |
| 85 | + |
| 86 | + GUI.enabled = GUI.enabled && !string.IsNullOrEmpty(_profileName) && !string.IsNullOrEmpty(_sessionName); |
| 87 | + |
| 88 | + if (GUILayout.Button("Create or Join Session")) |
| 89 | + { |
| 90 | + CreateOrJoinSessionAsync(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void OnDestroy() |
| 95 | + { |
| 96 | + _session?.LeaveAsync(); |
| 97 | + } |
| 98 | + |
| 99 | + private async Task CreateOrJoinSessionAsync() |
| 100 | + { |
| 101 | + _state = ConnectionState.Connecting; |
| 102 | + |
| 103 | + try |
| 104 | + { |
| 105 | + AuthenticationService.Instance.SwitchProfile(_profileName); |
| 106 | + await AuthenticationService.Instance.SignInAnonymouslyAsync(); |
| 107 | + |
| 108 | + var options = new CreateSessionOptions(_maxPlayers) { |
| 109 | + Name = _sessionName |
| 110 | + }.WithDistributedConnection(); |
| 111 | + |
| 112 | + _session = await MultiplayerService.Instance.CreateOrJoinSessionAsync(_sessionName, options); |
| 113 | + |
| 114 | + _state = ConnectionState.Connected; |
| 115 | + } |
| 116 | + catch (Exception e) |
| 117 | + { |
| 118 | + _state = ConnectionState.Disconnected; |
| 119 | + Debug.LogException(e); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +2. Attach this script to a new object in your scene. |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +## Netcode for GameObjects setup |
| 130 | + |
| 131 | +1. Create a new object in your scene called *NetworkManager*. Attach a Network Manager component to it. |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +2. Set **Session Mode** to **Distributed Authority**. |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +3. Under **Network Transport**, select **UnityTransport** from the list of options to add. |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +4. Save any changes to your objects and scene. |
| 144 | + |
| 145 | +## Adding gameplay |
| 146 | + |
| 147 | +1. Create a new Script called *PlayerCubeController.cs*: |
| 148 | + |
| 149 | +```cs |
| 150 | +using Unity.Netcode; |
| 151 | +using Unity.Netcode.Components; |
| 152 | +using UnityEngine; |
| 153 | + |
| 154 | +public class PlayerCubeController : NetworkBehaviour |
| 155 | +{ |
| 156 | + public float speed = 20; |
| 157 | + |
| 158 | + private NetworkTransform _transform; |
| 159 | + |
| 160 | + private void Start() |
| 161 | + { |
| 162 | + _transform = GetComponent<NetworkTransform>(); |
| 163 | + } |
| 164 | + |
| 165 | + private void Update() |
| 166 | + { |
| 167 | + var movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); |
| 168 | + _transform.transform.position += movement * speed * Time.deltaTime; |
| 169 | + } |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +2. Create a new prefab called *PlayerCube* and open it for editing. |
| 174 | + - Attach the *PlayerCubeController* to the prefab. When prompted to add a NetworkObject, select **Yes**. |
| 175 | + - Attach a Network Transform component as well. Make sure all the **Ownership** flags are unchecked. |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +3. Attach a child object in the prefab. Select the root of the prefab, right-click, and select **3D Object > Cube**. |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +4. Save all changes to the prefab. |
| 184 | + |
| 185 | +5. Open the Network Manager, navigate to **Prefab Settings**, and set the **Default Player Prefab** to be **PlayerCube**. |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +6. Save all changes to the scene. |
| 190 | + |
| 191 | +## Next steps |
| 192 | + |
| 193 | +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. |
| 194 | + |
| 195 | +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. |
| 196 | + |
| 197 | + |
0 commit comments