Skip to content
This repository was archived by the owner on Jul 23, 2025. It is now read-only.

Commit d611f1c

Browse files
jabbacakesVic-CooperDSchroerr-machLagowiecDev
authored
Develop into main (#1270)
Co-authored-by: Vic Cooper <[email protected]> Co-authored-by: Dominick <[email protected]> Co-authored-by: Rémi MACH <[email protected]> Co-authored-by: LagowiecDev <[email protected]> Co-authored-by: amanda-butler-unity <[email protected]> Co-authored-by: Noel Stephens <[email protected]> Co-authored-by: Griffin of Innatical <[email protected]> Co-authored-by: Christopher Pope <[email protected]> Co-authored-by: Steve Diniro <[email protected]> Co-authored-by: s-omeilia-unity <[email protected]> Co-authored-by: Alex Martin <[email protected]> Co-authored-by: Monaxys <[email protected]> Co-authored-by: Flap27 <[email protected]> Co-authored-by: NRTnarathip <[email protected]>
1 parent 2411425 commit d611f1c

17 files changed

+217
-7
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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+
![new unity project](/img/learn/distributed-authority-quick-start/new-project.png)
26+
27+
2. Ensure that the project is connected to a Unity Cloud project.
28+
29+
![connect unity cloud](/img/learn/distributed-authority-quick-start/connect-unity-cloud.png)
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+
![add connection manager](/img/learn/distributed-authority-quick-start/create-connection-manager.png)
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+
![add network manager](/img/learn/distributed-authority-quick-start/network-manager.png)
134+
135+
2. Set **Session Mode** to **Distributed Authority**.
136+
137+
![set session mode](/img/learn/distributed-authority-quick-start/session-mode.png)
138+
139+
3. Under **Network Transport**, select **UnityTransport** from the list of options to add.
140+
141+
![use unity transport](/img/learn/distributed-authority-quick-start/unity-transport.png)
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+
![setup network transform](/img/learn/distributed-authority-quick-start/network-transform.png)
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+
![add the cube](/img/learn/distributed-authority-quick-start/create-cube.png)
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+
![set the prefab](/img/learn/distributed-authority-quick-start/player-cube.png)
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+
![success](/img/learn/distributed-authority-quick-start/gameplay.png)

docs/terms-concepts/distributed-authority.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,19 @@ The traditional [client-server](client-server.md) model typically includes a ser
3030

3131
With a distributed authority topology, a client's state change takes a single client-relative [round trip period of time (RTT)](../lagandpacketloss.md#round-trip-time-and-pings) (½ per client) to be updated on all connected clients. The distributed authority service is more efficient: messages are routed and then processed, whereas client-server topologies require messages to be processed and then, at a later time, conveyed via new messages to all connected clients.
3232

33-
![Cloud relay service](/img/cloud-relay-service.jpg)
34-
35-
![Distributed authority service](/img/distributed-authority-service.jpg)
33+
<p align="middle">
34+
<img src="/img/cloud-relay-service.jpg" width="50%" />
35+
<img src="/img/distributed-authority-service.jpg" width="50%" />
36+
</p>
3637

3738
The distributed authority service is designed to quickly inspect messages, make any necessary changes (such as updating the destination address), forward the message, and then update any object state changes it's tracking. For certain operations like joining an in-progress session, the distributed authority service completely substitutes for a traditional host or server: it already has an up-to-date understanding of the state and can immediately transmit it to the joining client. Compared to a traditional client-server topology, this results in faster delivery of state changes, more balanced bandwidth utilization, and a smoother experience for all players.
39+
40+
## More information about distributed authority
41+
42+
For more information about how distributed authority works in Netcode for GameObjects, see the following pages in the documentation:
43+
44+
- [Distributed authority quickstart](../learn/distributed-authority-quick-start.md)
45+
- [Understanding ownership and authority](../basics/ownership.md)
46+
- [Race conditions](../basics/race-conditions.md)
47+
- [Spawning synchronization](../basics/spawning-synchronization.md)
48+
- [Deferred despawning](../basics/deferred-despawning.md)

docs/tutorials/get-started-with-ngo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: get-started-ngo
3-
title: Get started with Netcode for GameObjects
3+
title: Client-server quickstart for Netcode for GameObjects
44
---
55

66
Use this guide to learn how to create your first [client-server](../terms-concepts/client-server.md) Netcode for GameObjects project. It walks you through creating a simple Hello World project that implements the basic features of Netcode for GameObjects.

mppm/about.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ Multiplayer Play Mode has some inherent technical limitations, specifically arou
2323
The Unity Editor and Virtual Players require a lot of system resources, so you shouldn't use Multiplayer Play Mode at scale. Multiplayer Play Mode is designed for small-scale, local testing environments that can only support up to four total players (the Main Editor and three Virtual Players).
2424

2525
### Authoring
26-
27-
Virtual Players have restricted authoring capabilities because they're intended as a vehicle to open multiple project runtimes, not provide a multi-editor authoring workflow. You should use the Main Editor to make changes and the Virtual Players to test multiplayer functionality.
28-
26+
You can't create or change the properties of GameObjects in a Virtual Player. Instead, use the Main Editor to make changes and a Virtual Player to test multiplayer functionality. Any changes you make in Play Mode in the Main Editor reset when you exit Play Mode.
2927
:::note
3028
You can't access any Main Editor functionality from Virtual Players.
3129
:::

sidebars.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ module.exports = {
4444
"type": "doc",
4545
"id": "tutorials/get-started-ngo"
4646
},
47+
{
48+
"type": "doc",
49+
"id": "learn/distributed-authority-quick-start"
50+
},
4751
{
4852
"collapsed": true,
4953
"type": "category",
12 KB
Loading
37.3 KB
Loading
78.2 KB
Loading
267 KB
Loading
174 KB
Loading

0 commit comments

Comments
 (0)