Skip to content

Commit

Permalink
Develop into main (#1270)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
15 people authored May 22, 2024
1 parent 2411425 commit d611f1c
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 7 deletions.
197 changes: 197 additions & 0 deletions docs/learn/distributed-authority-quick-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
---
id: distributed-authority-quick-start
title: Distributed authority quickstart for Netcode for GameObjects
---

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.

## Prerequisites

Before you begin, you need the following:

- An active Unity account with a valid license.
- The [Unity Hub](https://unity.com/download).
- A supported version of the Unity Editor. Refer to the [Netcode for GameObjects requirements](https://docs-multiplayer.unity3d.com/netcode/current/installation).

## 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](/img/learn/distributed-authority-quick-start/new-project.png)

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

![connect unity cloud](/img/learn/distributed-authority-quick-start/connect-unity-cloud.png)

:::note 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:

```cs
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);
}
}
}
```

2. Attach this script to a new object in your scene.

![add connection manager](/img/learn/distributed-authority-quick-start/create-connection-manager.png)

## Netcode for GameObjects setup

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

![add network manager](/img/learn/distributed-authority-quick-start/network-manager.png)

2. Set **Session Mode** to **Distributed Authority**.

![set session mode](/img/learn/distributed-authority-quick-start/session-mode.png)

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

![use unity transport](/img/learn/distributed-authority-quick-start/unity-transport.png)

4. Save any changes to your objects and scene.

## Adding gameplay

1. Create a new Script called *PlayerCubeController.cs*:

```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;
}
}
```

2. 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](/img/learn/distributed-authority-quick-start/network-transform.png)

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

![add the cube](/img/learn/distributed-authority-quick-start/create-cube.png)

4. Save all changes to the prefab.

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

![set the prefab](/img/learn/distributed-authority-quick-start/player-cube.png)

6. 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](/img/learn/distributed-authority-quick-start/gameplay.png)
17 changes: 14 additions & 3 deletions docs/terms-concepts/distributed-authority.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ The traditional [client-server](client-server.md) model typically includes a ser

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.

![Cloud relay service](/img/cloud-relay-service.jpg)

![Distributed authority service](/img/distributed-authority-service.jpg)
<p align="middle">
<img src="/img/cloud-relay-service.jpg" width="50%" />
<img src="/img/distributed-authority-service.jpg" width="50%" />
</p>

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.

## More information about distributed authority

For more information about how distributed authority works in Netcode for GameObjects, see the following pages in the documentation:

- [Distributed authority quickstart](../learn/distributed-authority-quick-start.md)
- [Understanding ownership and authority](../basics/ownership.md)
- [Race conditions](../basics/race-conditions.md)
- [Spawning synchronization](../basics/spawning-synchronization.md)
- [Deferred despawning](../basics/deferred-despawning.md)
2 changes: 1 addition & 1 deletion docs/tutorials/get-started-with-ngo.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
id: get-started-ngo
title: Get started with Netcode for GameObjects
title: Client-server quickstart for Netcode for GameObjects
---

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.
Expand Down
4 changes: 1 addition & 3 deletions mppm/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ Multiplayer Play Mode has some inherent technical limitations, specifically arou
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).

### Authoring

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.

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.
:::note
You can't access any Main Editor functionality from Virtual Players.
:::
Expand Down
4 changes: 4 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ module.exports = {
"type": "doc",
"id": "tutorials/get-started-ngo"
},
{
"type": "doc",
"id": "learn/distributed-authority-quick-start"
},
{
"collapsed": true,
"type": "category",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d611f1c

Please sign in to comment.