Skip to content

Commit 5673a99

Browse files
Vic-Cooperr-machjabbacakesLagowiecDevamanda-butler-unity
authored andcommitted
Merge develop into main (#1244)
Co-authored-by: Rémi MACH <[email protected]> Co-authored-by: Amy Reeve <[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 0cd0d6e commit 5673a99

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

docs/learn/sample-dedicated-server.md

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
id: sample-dedicated-server
3+
title: Dedicated game server sample
4+
---
5+
6+
## Dedicated Game Server sample
7+
8+
[The Dedicated Game Server sample](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize/tree/main/Experimental/DedicatedGameServer) project demonstrates how the dedicated server model works and the tools that you can use to test multiplayer in the editor.
9+
10+
This project uses the following packages and services:
11+
12+
* [Dedicated Server](https://docs.unity3d.com/Packages/[email protected]/manual/index.html)
13+
* [Netcode For GameObjects](https://docs-multiplayer.unity3d.com/netcode/current/about/)
14+
* [Unity Transport](https://docs-multiplayer.unity3d.com/transport/current/about/)
15+
* [Multiplayer Play Mode](https://docs-multiplayer.unity3d.com/mppm/current/about/)
16+
* [Game Server Hosting](https://docs.unity.com/ugs/en-us/manual/game-server-hosting/manual/welcome-to-multiplay)
17+
* [Matchmaker](https://docs.unity.com/ugs/en-us/manual/matchmaker/manual/matchmaker-overview)
18+
19+
20+
### Sample features
21+
22+
This section describes how the following features are configured to use the dedicated server:
23+
24+
- [Dedicated Game Server sample](#dedicated-game-server-sample)
25+
- [Sample features](#sample-features)
26+
- [Project settings](#project-settings)
27+
- [Strip generic components from the server](#strip-generic-components-from-the-server)
28+
- [Split scripts across the client, server, or network](#split-scripts-across-the-client-server-or-network)
29+
- [Synchronize animations between clients](#synchronize-animations-between-clients)
30+
- [Automatically synchronized animations](#automatically-synchronized-animations)
31+
- [Manually synchronized animations](#manually-synchronized-animations)
32+
- [Navigation](#navigation)
33+
- [Use multiplayer roles to control game logic](#use-multiplayer-roles-to-control-game-logic)
34+
- [Integrate with Unity Gaming Services (UGS)](#integrate-with-unity-gaming-services-ugs)
35+
- [Set default command line arguments](#set-default-command-line-arguments)
36+
37+
### Project settings
38+
39+
In this sample some GameObjects and components exist on the clients, some exist on the server, and some exist on both. This section explains how this sample strips generic GameObjects and components from the client or the server.
40+
41+
#### Strip generic components from the server
42+
43+
This project automatically strips the rendering, UI, and Audio components. To learn which settings automatically strip components, perform the following actions:
44+
45+
* Open the Project Settings window (menu: **Edit** > **Project Settings**).
46+
* Select **Multiplayer Roles**.
47+
* Select **Content Selection**.
48+
49+
For more information, refer to [Automatically remove components from a Multiplayer Role](https://docs.unity3d.com/Packages/[email protected]/manual/automatic-selection.html).
50+
51+
This sample strips other components and GameObjects from the server manually. To learn how to do this, refer to [Control which GameObjects and components exist on the client or the server](https://docs.unity3d.com/Packages/[email protected]/manual/content-selection.html).
52+
53+
#### Split scripts across the client, server, or network
54+
55+
This sample splits the logic of the Player Character and the AI Character into separate scripts so that you can use the Content Selection window to run each character separately on the client, server and network. For example, the `PlayerCharacter` script logic is split into the following scripts:
56+
* Client Player Character. This script only exists on the clients.
57+
* Server Player Character.This script only exists on the server.
58+
* Networked Player Character: This script inherits from `NetworkBehaviour`.It synchronizes data and sends messages between the server and clients. This script exists on both clients and the server.
59+
60+
These scripts separate the logic of each player which means you can strip the components that each script uses. For example, Client Player Character is the only script that uses the Player Character’s `CharacterController` component, so you can safely strip the `CharacterController` component from the server. To learn where the scripts in this sample exist, do the following:
61+
1. Select a Player Character GameObject.
62+
2. Open the GameObject’s Inspector window.
63+
3. Refer to the script component’s [Multiplayer role icon](https://docs.unity3d.com/Packages/[email protected]/manual/mutliplayer-roles-icons.html).
64+
65+
The logic for scripts that contain a small class, like the doors and switches in this sample scene, exist in a single script that inherits from ``NetworkBehaviour``.
66+
67+
#### Synchronize animations between clients
68+
69+
This sample handles synchronizes animations between clients in the following ways:
70+
71+
* Automatically synchronized animations. This process uses the NetworkAnimator component.
72+
* Manually synchronized animations. Use this process to strip animators from the server.
73+
74+
##### Automatically synchronized animations
75+
76+
The Animator component and the NetworkAnimator script synchronize animations across the clients and servers. The server or the owner of the animations, for example a PlayerCharacter script that uses a `ClientNetworkAnimator`, handles and automatically synchronizes animations between clients. This is necessary for animations that drive logic on the server. For example, in this sample the door animation (door_boss_ani) is synchronized automatically, but its game state determines whether to enable or disable its colliders and navmesh obstacles.
77+
78+
##### Manually synchronized animations
79+
80+
The Animator components on the AICharacter prefab in this sample are stripped from the server to reduce the build size which reduces the bandwidth and CPU resources that the server uses. This means that the NetworkAnimator doesn’t automatically synchronize animations between clients and requires manually synchronized animations.
81+
82+
Each client uses data and events that are synchronized between them to drive the animation. The server handles their movement, and synchronizes their speed and transform values. Clients use that information to drive the character’s animation.
83+
84+
`ServerAICharacter.cs`:
85+
86+
```
87+
void Update()
88+
{
89+
if (!m_NavMeshAgent.pathPending && m_NavMeshAgent.remainingDistance &lt; k_ReachDist)
90+
{
91+
GotoNextPoint();
92+
}
93+
m_NetworkedAICharacter.Speed = m_NavMeshAgent.velocity.magnitude;
94+
}
95+
```
96+
97+
`ClientAICharacter.cs`:
98+
99+
```
100+
void Update()
101+
{
102+
if (m_NetworkedAICharacter.IsSpawned)
103+
{
104+
m_Animator.SetFloat(k_AnimIdSpeed, m_NetworkedAICharacter.Speed);
105+
}
106+
}
107+
```
108+
109+
#### Navigation
110+
111+
The AI characters in this sample use the navigation system. The AI characters exist on the server which means their navigation data and components are not required on clients and can be stripped. This includes the Navmesh and the NavMeshAgent, NavMeshModifier, and NavMeshObstacle components.
112+
113+
Other GameObjects specific to servers are also stripped from clients, like the player spawn locations and the patrols.
114+
115+
#### Use multiplayer roles to control game logic
116+
117+
This sample assigns a [Multiplayer Role](https://docs.unity3d.com/Packages/[email protected]/manual/multiplayer-roles.html) to move Clients and Servers to different scenes when the application starts.
118+
119+
```
120+
/// <summary>
121+
/// Initializes the application's network-related behaviour according to the configuration. Servers load the main
122+
/// game scene and automatically start. Clients load the metagame scene and, if autoconnect is set to true, attempt
123+
/// to connect to a server automatically based on the IP and port passed through the configuration or the command
124+
/// line arguments.
125+
/// <summary>
126+
127+
void InitializeNetworkLogic()
128+
{
129+
var commandLineArgumentsParser = new CommandLineArgumentsParser();
130+
ushort listeningPort = (ushort) commandLineArgumentsParser.Port;
131+
switch (MultiplayerRolesManager.ActiveMultiplayerRoleMask)
132+
{
133+
case MultiplayerRoleFlags.Server:
134+
//lock framerate on dedicated servers
135+
Application.targetFrameRate = commandLineArgumentsParser.TargetFramerate;
136+
QualitySettings.vSyncCount = 0;
137+
m_ConnectionManager.StartServerIP(k_DefaultServerListenAddress, listeningPort);
138+
break;
139+
case MultiplayerRoleFlags.Client:
140+
{
141+
SceneManager.LoadScene("MetagameScene");
142+
if (AutoConnectOnStartup)
143+
{
144+
m_ConnectionManager.StartClient(k_DefaultClientAutoConnectServerAddress, listeningPort);
145+
}
146+
break;
147+
}
148+
case MultiplayerRoleFlags.ClientAndServer:
149+
throw new ArgumentOutOfRangeException("MultiplayerRole", "ClientAndServer is an invalid multiplayer role in this sample. Please select the Client or Server role.");
150+
}
151+
}
152+
```
153+
154+
The Multiplayer Role also defines how the game handles connection events:
155+
156+
```
157+
void OnConnectionEvent(ConnectionEvent evt)
158+
{
159+
if (MultiplayerRolesManager.ActiveMultiplayerRoleMask == MultiplayerRoleFlags.Server)
160+
{
161+
switch (evt.status)
162+
{
163+
case ConnectStatus.GenericDisconnect:
164+
case ConnectStatus.ServerEndedSession:
165+
case ConnectStatus.StartServerFailed:
166+
// If server ends networked session or fails to start, quit the application
167+
Quit();
168+
break;
169+
case ConnectStatus.Success:
170+
// If server successfully starts, load game scene
171+
NetworkManager.Singleton.SceneManager.LoadScene("GameScene01", LoadSceneMode.Single);
172+
break;
173+
}
174+
}
175+
else
176+
{
177+
switch (evt.status)
178+
{
179+
case ConnectStatus.GenericDisconnect:
180+
case ConnectStatus.UserRequestedDisconnect:
181+
case ConnectStatus.ServerEndedSession:
182+
// If client is disconnected, return to metagame scene
183+
SceneManager.LoadScene("MetagameScene");
184+
break;
185+
}
186+
}
187+
}
188+
```
189+
190+
## Integrate with Unity Gaming Services (UGS)
191+
192+
This sample uses the following Unity Gaming Services (UGS):
193+
194+
* The [Game Server Hosting](https://docs.unity.com/ugs/en-us/manual/game-server-hosting/manual/welcome) service hosts the dedicated server builds in the cloud.
195+
* The [Matchmaker](https://docs.unity.com/ugs/en-us/manual/matchmaker/manual/matchmaker-overview) service allows clients to connect to those servers.
196+
197+
## Set default command line arguments
198+
199+
Some of the parameters in this sample are set in command line arguments, for example, the port that the servers listen on and the target framerate of the server which apply in a build. When you test a project in the editor, Unity uses a fallback value instead.
200+
201+
This allows the Game Server Hosting service to specify which port a server uses when it is allocated. This sample uses the `DedicatedServer.Arguments` static class to read those values. It uses the [CLI Arguments Defaults](https://docs.unity3d.com/Packages/[email protected]/manual/cli-arguments.html) feature of the [Dedicated Server package](https://docs.unity3d.com/Packages/[email protected]/manual/index.html) to provide default values for those arguments.

sidebars.js

+4
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ module.exports = {
477477
"type": "category",
478478
"label": "Educational samples",
479479
"items": [
480+
{
481+
"type": "doc",
482+
"id": "learn/sample-dedicated-server"
483+
},
480484
{
481485
"collapsed": true,
482486
"type": "category",

0 commit comments

Comments
 (0)