-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
2411425
commit d611f1c
Showing
17 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
 | ||
|
||
2. Ensure that the project is connected to a Unity Cloud project. | ||
|
||
 | ||
|
||
:::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. | ||
|
||
 | ||
|
||
## Netcode for GameObjects setup | ||
|
||
1. Create a new object in your scene called *NetworkManager*. Attach a Network Manager component to it. | ||
|
||
 | ||
|
||
2. Set **Session Mode** to **Distributed Authority**. | ||
|
||
 | ||
|
||
3. Under **Network Transport**, select **UnityTransport** from the list of options to add. | ||
|
||
 | ||
|
||
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. | ||
|
||
 | ||
|
||
3. Attach a child object in the prefab. Select the root of the prefab, right-click, and select **3D Object > Cube**. | ||
|
||
 | ||
|
||
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**. | ||
|
||
 | ||
|
||
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. | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+12 KB
static/img/learn/distributed-authority-quick-start/connect-unity-cloud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.3 KB
static/img/learn/distributed-authority-quick-start/create-connection-manager.png
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.
Binary file added
BIN
+174 KB
static/img/learn/distributed-authority-quick-start/multiplayer-package.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+36.5 KB
static/img/learn/distributed-authority-quick-start/network-manager.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+81.8 KB
static/img/learn/distributed-authority-quick-start/network-transform.png
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.
Binary file added
BIN
+29.4 KB
static/img/learn/distributed-authority-quick-start/unity-transport.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.