Skip to content

Commit 16e2da9

Browse files
update: Adding SinglePlayerTransport documentation (#1476)
Co-authored-by: Amy Reeve <[email protected]>
1 parent d366929 commit 16e2da9

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

docs/advanced-topics/transports.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,97 @@ A transport layer can provide:
2222

2323
Netcode's default transport Unity Transport is an entire transport layer that you can use to add multiplayer and network features to your project with or without Netcode. Refer to the [Transport documentation](https://docs-multiplayer.unity3d.com/transport/current/about/) for more information and how to [install the Transport package](https://docs-multiplayer.unity3d.com/transport/current/install/).
2424

25+
## `SinglePlayerTransport`
26+
27+
Netcode for GameObjects provides a `SinglePlayerTransport` that you can use to create a local single player network session. This simplifies switching between multiplayer and single player sessions within the same project, while still being able to use existing netcode scripts. The `SinglePlayerTransport` is a effectively a mock transport that ensures full `NetworkTransport` functionality without any transport dependencies.
28+
29+
### Set up a single player session
30+
31+
In addition to your default network transport, you need to add the `SinglePlayerTransport` to the `NetworkManager` `GameObject` (or child of).
32+
33+
![image](/img/transport/SinglePlayerTransport_AddComponent.png)
34+
35+
To start a single player session, assign the `SinglePlayerTransport` to the `NetworkManager.NetworkConfig.NetworkTransport` configuration property using a script.
36+
37+
For example:
38+
39+
```csharp
40+
using Unity.Netcode;
41+
using Unity.Netcode.Transports.UTP;
42+
using Unity.Netcode.Transports.SinglePlayer;
43+
using UnityEngine;
44+
45+
public class ExtendedNetworkManager : NetworkManager
46+
{
47+
private UnityTransport m_UnityTransport;
48+
private SinglePlayerTransport m_SinglePlayerTransport;
49+
50+
private void Awake()
51+
{
52+
m_UnityTransport = GetComponent<UnityTransport>();
53+
m_SinglePlayerTransport = GetComponent<SinglePlayerTransport>();
54+
}
55+
56+
public void StartSinglePlayer()
57+
{
58+
// Use the single player transport when starting a single player session.
59+
NetworkConfig.NetworkTransport = m_SinglePlayerTransport;
60+
if (!StartHost())
61+
{
62+
NetworkLog.LogError("Failed to start single player session!");
63+
}
64+
}
65+
66+
public void StartHostedSession()
67+
{
68+
// Use the network transport when starting a multiplayer session.
69+
NetworkConfig.NetworkTransport = m_UnityTransport;
70+
if (!StartHost())
71+
{
72+
NetworkLog.LogError("Failed to start hosted session!");
73+
}
74+
}
75+
}
76+
```
77+
78+
As shown in the script above, when starting a single player session the `SinglePlayerTransport` is assigned to the `NetworkConfig.NetworkTransport`, and when starting a hosted multiplayer session the `UnityTransport` is assigned.
79+
80+
### Single player limitations
81+
82+
When running a single player session, you should take any netcode script into consideration that requires an actual multiplayer session to exist.
83+
84+
#### RPC considerations
85+
You can invoke any RPC that includes:
86+
87+
- The host's client.
88+
- The host's server.
89+
- `SendTo` targets that will be invoked locally:
90+
- `SendTo.Me`
91+
- `SendTo.Server`
92+
- `SendTo.Everyone`
93+
- `SendTo.Authority`
94+
- `SendTo.Owner`
95+
- `ClientsAndHost`
96+
- `SpecifiedInParams`: As long as it is targeting the `NetworkManager.LocalClientId`.
97+
- `SendTo` targets that will **not be** invoked locally:
98+
- `SendTo.NotOwner`
99+
- `SendTo.NotServer`
100+
- `SendTo.NotMe`
101+
- `SendTo.NotAuthority`
102+
- `SpecifiedInParams`: Anything not targeting the `NetworkManager.LocalClientId` will not be invoked locally.
103+
104+
#### NetworkVariable considerations
105+
106+
NetworkVariables should work as expected because the host will always be both the server and the owner of anything spawned. This means that write permissions, whether server or owner, should not be an issue.
107+
108+
#### Distributed authority considerations
109+
110+
To start a single player session using the distributed authority network topology you should:
111+
- Keep your network topology setting set to distributed authority.
112+
- Set the `NetworkConfig.NetworkTransport` to the `SinglePlayerTransport` component.
113+
- Start as a host.
114+
115+
25116
## Unity's UNet transport layer API
26117

27118
UNet is a deprecated solution that is no longer supported after Unity 2022.2. Unity Transport Package is the default transport for Netcode for GameObjects. We recommend transitioning to Unity Transport as soon as possible.
Loading

0 commit comments

Comments
 (0)