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

Commit 6f9f61a

Browse files
authored
Develop into main for 2.0.0-exp release (#1248)
2 parents b3a6fd2 + 8e5a729 commit 6f9f61a

File tree

141 files changed

+14641
-140
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+14641
-140
lines changed

LICENSE.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
1-
The following Licenses cover all documentation and sample code in [this repository](https://github.com/Unity-Technologies/com.unity.multiplayer.docs) and website.
1+
The following Licenses cover all documentation and sample code in [this repository](https://github.com/Unity-Technologies/com.unity.multiplayer.docs) and website.
22

33
## Documentation License
44

55
All documentation sections have licenses covering content for those Unity features and code:
66

77
| Documentation | License |
88
| -- | -- |
9-
| [Netcode for GameObjects](https://docs-multiplayer.unity3d.com/docs/getting-started/about) | [MIT License](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects) |
9+
| [Netcode for GameObjects](https://docs-multiplayer.unity3d.com/docs/getting-started/about) | [Unity Companion License](https://unity3d.com/legal/licenses/Unity_Companion_License) |
1010
| [Transport](https://docs-multiplayer.unity3d.com/docs/transport/introduction) | [Unity Companion License](https://unity3d.com/legal/licenses/Unity_Companion_License) |
11-
| [Learn](https://docs-multiplayer.unity3d.com/docs/learn/introduction) | [UCL License](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/LICENSE.md) |
11+
| [Learn](https://docs-multiplayer.unity3d.com/docs/learn/introduction) | [Unity Companion License](https://unity3d.com/legal/licenses/Unity_Companion_License) |
1212

13-
Documentation site files and markdown content outside of these features is covered under the MIT License:
13+
Documentation site files and markdown content outside of these features are also covered under the Unity Companion License:
1414

15-
MIT License
16-
17-
Copyright (c) 2021 Unity Technologies
18-
19-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
20-
21-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15+
Licensed under the Unity Companion License for Unity-dependent projects (see https://unity3d.com/legal/licenses/unity_companion_license).
2216

23-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Please review the license for details on these and other terms and conditions.
2418

2519
## Netcode for GameObjects License
2620

27-
The [Netcode for GameObjects repository](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects) and code are under the MIT License.
28-
29-
MIT License
21+
The [Netcode for GameObjects repository](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects) and code are under the Unity Companion License (UCL).
3022

31-
Copyright (c) 2020 Unity Technologies
32-
33-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
23+
UCL License
24+
Netcode for GameObjects © 2021-2024 Unity Technologies
3425

35-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
26+
Licensed under the Unity Companion License for Unity-dependent projects (see https://unity3d.com/legal/licenses/unity_companion_license).
3627

37-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28+
Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Please review the license for details on these and other terms and conditions.
3829

3930
## Multiplayer Sample Co-op License
4031

41-
The [Multiplayer Sample Co-op respository](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop) and code are under a UCL License.
32+
The [Multiplayer Sample Co-op respository](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop) and code are under a UCL License.
4233

4334
UCL License
4435
Boss Room: Multiplayer Sample Co-op © 2021 Unity Technologies
@@ -49,7 +40,7 @@ Unless expressly provided otherwise, the Software under this license is made ava
4940

5041
## Multiplayer Bitesize Sample License
5142

52-
The [Multiplayer Bitesize Sample respository](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize) and code are under a UCL License.
43+
The [Multiplayer Bitesize Sample respository](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize) and code are under a UCL License.
5344

5445
UCL License
5546
Boss Room: Multiplayer Bitesize Sample© 2021 Unity Technologies
@@ -62,8 +53,9 @@ Unless expressly provided otherwise, the Software under this license is made ava
6253

6354
Transport code is under a [Unity Companion License](https://unity3d.com/legal/licenses/Unity_Companion_License). The repository is currently private.
6455

56+
UCL License
6557
Unity Transport copyright © 2018 Unity Technologies ApS
6658

67-
Licensed under the Unity Companion License for Unity-dependent projects--see Unity Companion License.
59+
Licensed under the Unity Companion License for Unity-dependent projects (see https://unity3d.com/legal/licenses/unity_companion_license).
6860

6961
Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Please review the license for details on these and other terms and conditions.

docs/advanced-topics/physics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Add the ClientNetworkTransform component to your GameObject first. Otherwise the
3838

3939
## Physics and latency
4040

41-
A common issue with physics in multiplayer games is lag and how objects update on basically different timelines. For example, a player would be on a timeline thats offset by the network latency relative to your servers objects. One way to prepare for this is to test your game with artificial lag. You might catch some weird delayed collisions that would otherwise make it into production.
41+
A common issue with physics in multiplayer games is lag and how objects update on basically different timelines. For example, a player would be on a timeline that's offset by the network latency relative to your server's objects. One way to prepare for this is to test your game with artificial lag. You might catch some weird delayed collisions that would otherwise make it into production.
4242

4343
The ClientDriven [bitesize sample](../learn/bitesize/bitesize-clientdriven.md) addresses this by manually adding forces server-side to offer a buffer before an actual collision, but it still feels wobbly at times. However, this isn't really a solution.
4444

docs/basics/deferred-despawning.md

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
---
2+
id: deferred-despawning
3+
title: Deferred despawning
4+
---
5+
6+
:::info Distributed authority only
7+
8+
Deferred despawning is only available for games using a [distributed authority topology](../terms-concepts/distributed-authority.md).
9+
10+
:::
11+
12+
In addition to the visual latency issues described on the [spawning synchronization page](spawning-synchronization.md), networked games also experience latency issues when despawning objects. In [client-server](../terms-concepts/client-server.md) contexts, these problems are typically addressed using client prediction models, but in a distributed authority context there is a simpler alternative solution: deferred despawning.
13+
14+
## Why deferred despawning
15+
16+
Issues with despawning occur when objects are despawned too early. The client that owns the projectile detects a collision, triggers any local animations, and despawns the projectile locally. For the player being hit, they see the projectile despawn before it reaches them and animations can appear to trigger at the wrong point in time. This can be seen in the diagram below.
17+
18+
![Without deferred despawning](/img/without-deferred-despawning.jpg)
19+
20+
Deferred despawning allows you to add a tick-based delay to a NetworkObject prior to despawning it locally. The deferred despawn tick value is then added to the `DestroyObjectMessage` so that other clients, upon receiving the message, defer processing the destroy message until the specified tick value has been reached. The results of this deferred despawn adjustment can be seen in the diagram below.
21+
22+
![With deferred despawning](/img/with-deferred-despawning.jpg)
23+
24+
Deferred despawning allows non-owner instances of an object to complete their interpolation towards the last transform state update before despawning the object. Even though non-owner clients can be visually latent by several network ticks behind the authority, all clients end up with the same visual experience since NetworkTransform deltas are tick synchronized.
25+
26+
## Implementing deferred despawning
27+
28+
Implementing deferred despawning is a three-step process, as described in the diagram below.
29+
30+
![Implementing deferred despawning](/img/implementing-deferred-despawning.jpg)
31+
32+
The process above is broken down into two steps that are performed on the authority instance, and one step performed on the non-authority instance(s).
33+
34+
|Authority instance|Non-authority instance(s)|
35+
|---|---|
36+
|1. Invoke the `NetworkObject.DeferDespawn` method while providing the number of ticks to offset despawning by on non-authority instance(s), relative to the local client's current known network tick.<br/> 2. If overridden, handle any changes to state (i.e. NetworkVariables) when `NetworkBehaviour.OnDeferringDespawn` is invoked.|1. Use any updated states to synchronize the visual portion of a deferred despawn (for example, starting a particle system to represent the point of impact).|
37+
38+
### Deferred despawning example
39+
40+
Below is a basic example demonstrating how deferred despawning can be used to visually synchronize a projectile's impact with explosion effects:
41+
42+
```
43+
public class ExplodingProjectile : NetworkBehaviour
44+
{
45+
// The explosion network prefab
46+
public GameObject ExplosionPrefab;
47+
public int DespawnTickOffset = 4;
48+
private ExplosionFx m_SpawnedExplosion;
49+
50+
51+
// Permission are always owner write and everyone read in distributed authority
52+
// Authority assigns this value after spawning the ExplosionFx within the overridden
53+
// OnDeferringDespawn method prior to the local ExplodingPrrojectile is despawned
54+
// locally.
55+
private NetworkVariable<NetworkBehaviourReference> m_ExplosionFx = new NetworkVariable<NetworkBehaviourReference>();
56+
57+
58+
public override void OnNetworkSpawn()
59+
{
60+
if (!HasAuthority)
61+
{
62+
m_ExplosionFx.OnValueChanged += OnExplosionFxChanged;
63+
}
64+
65+
66+
base.OnNetworkSpawn();
67+
}
68+
69+
70+
private void OnCollisionEnter(Collision collision)
71+
{
72+
if (!IsSpawned || !HasAuthority)
73+
{
74+
return;
75+
}
76+
// Typically you would want to check what you hit
77+
// to make sure you want to "explode" the projectile
78+
// first. This example assumes that a check was performed.
79+
80+
81+
// Start with the projectile's position
82+
var explodePoint = transform.position;
83+
if (collision.contacts.Length > 0 )
84+
{
85+
// Example purposes, just use the first contact
86+
explodePoint = collision.contacts[0].point;
87+
}
88+
HandleExplosion(explodePoint);
89+
}
90+
91+
92+
private void HandleExplosion(Vector3 explodePoint)
93+
{
94+
// Example purposes only, we recommend using object pools
95+
// using an INetworkPrefabInstanceHandler implementation
96+
// and registering that with the NetworkManager.PrefabHandler.
97+
var instance = Instantiate(ExplosionPrefab);
98+
99+
// position the explosion
100+
instance.transform.position = explodePoint;
101+
102+
103+
// Assign this for later use (example purposes)
104+
m_SpawnedExplosion = instance.GetComponent<ExplosionFx>();
105+
106+
// Spawn the explosion
107+
var instanceObj = instance.GetComponent<NetworkObject>();
108+
instanceObj.Spawn();
109+
110+
111+
// Defer the despawning of the projectile instance
112+
NetworkObject.DeferDespawn(DespawnTickOffset);
113+
114+
// The local instance should be despawned at this point
115+
}
116+
117+
118+
/// <summary>
119+
/// Invoked on the authority side when it is deferring the
120+
/// despawning of the NetworkObject.
121+
/// </summary>
122+
/// <remarks>
123+
/// This is a good time to set any NetworkVariable states as
124+
/// they will be sent to clients prior to the defer despawn message.
125+
/// </remarks>
126+
/// <param name="despawnTick">the final future network tick
127+
/// non-authority instances will despawn the clone instance.</param>
128+
public override void OnDeferringDespawn(int despawnTick)
129+
{
130+
// This lets the non-authority instances know what ExplosionFx instance is
131+
// associated with this NetworkObject.
132+
m_ExplosionFx = new NetworkVariable<NetworkBehaviourReference>(m_SpawnedExplosion);
133+
134+
135+
// Apply any other state updates here
136+
137+
138+
base.OnDeferringDespawn(despawnTick);
139+
}
140+
141+
142+
/// <summary>
143+
/// Non-authority registers for this and acquires the ExplosionFX
144+
/// of the network prefab spawned.
145+
/// </summary>
146+
private void OnExplosionFxChanged(NetworkBehaviourReference previous, NetworkBehaviourReference current)
147+
{
148+
// If possible, get the ExplosionFx component
149+
current.TryGet(out m_SpawnedExplosion);
150+
}
151+
152+
153+
public override void OnNetworkDespawn()
154+
{
155+
// When non-authority instances finally despawn,
156+
// the explosion FX will begin playing.
157+
if (!HasAuthority)
158+
{
159+
if (m_SpawnedExplosion)
160+
{
161+
m_SpawnedExplosion.SetParticlePlayingState(true);
162+
}
163+
m_ExplosionFx.OnValueChanged -= OnExplosionFxChanged;
164+
}
165+
base.OnNetworkDespawn();
166+
}
167+
}
168+
```
169+
170+
The pseudo script above excludes the motion of the projectile, but makes the assumption that authority is moving the projectile and that when the projectile impacts a valid object, the `OnCollisionEnter` method will be invoked.
171+
172+
Authority instance|Non-authority instance(s)|
173+
|---|---|
174+
|1. Move until collision (excluded from the above example)<br/> 2. Upon collision: instantiate ExplosionFX, position ExplosionFX, acquire the ExplosionFX component, spawn the ExplosionFX, defer despawning the projectile<br/> 3. When deferring despawning the projectile, assign the NetworkBehaviourReference to the ExplosionFX<br/> 4. Despawn locally (happens automatically at end of the DeferDespawn)|1. When spawned, register changes to the m_ExplosionFX NetworkVariable<br/> 2. Continue to interpolate towards last updated position (handled by NetworkTransform)<br/> 3. When m_ExplosionFX NetworkVariable changes, assign the local m_SpawnExplosion ExplosionFX component of the explosion associated with the current projectile instance (to be used when despawned)<br/> 4.When despawned, start the ExplosionFX particle system via m_SpawnExplosion|
175+
176+
The non-authority instance still spawns the ExplosionFX NetworkObject at the same relative time frame as the authority, however it doesn't start its particle system until the local non-authority projectile instance is despawned. Since the projectile has had its despawn deferred until a future network tick, the non-authority instance interpolates up to its last updated position from the authority side and then shortly (in milliseconds) after is despawned and the explosion particle system started.
177+
178+
:::info Example purposes only
179+
180+
The spawning explosion FX example above is only for example purposes and would be less bandwidth and processor intensive if you used a local particle FX pool that you pull from and began playing on both the authority and non-authority sides when the object in question is despawned. The same deferred despawn principles would be used without the need to spawn an additional object. However, providing the spawned explosion example also covers other scenarios where spawning is required.
181+
182+
:::
183+
184+
For example purposes, the below script is all that you would need to control starting and stopping the explosion particle system:
185+
186+
```
187+
public class ExplosionFx : NetworkBehaviour
188+
{
189+
public ParticleSystem ParticleSystem;
190+
191+
192+
private void OnEnable()
193+
{
194+
// In the event a pool is used, it is always good to
195+
// make sure the particle system is not playing
196+
SetParticlePlayingState();
197+
}
198+
199+
200+
public override void OnNetworkSpawn()
201+
{
202+
// Authority starts the particle system (locally) when spawned
203+
if (HasAuthority)
204+
{
205+
SetParticlePlayingState(true);
206+
}
207+
base.OnNetworkSpawn();
208+
}
209+
210+
211+
/// <summary>
212+
/// Used to start/stop the particle system
213+
/// Non-Authority starts this when its associated projectile
214+
/// is finally despawned
215+
/// </summary>
216+
public void SetParticlePlayingState(bool isPlaying = false)
217+
{
218+
if (ParticleSystem)
219+
{
220+
if(isPlaying)
221+
{
222+
ParticleSystem.Play();
223+
}
224+
else
225+
{
226+
ParticleSystem.Stop();
227+
}
228+
}
229+
}
230+
}
231+
```
232+
233+
Note that the authority automatically starts the particle system upon being spawned. Non-authority instances are started by their paired projectile when despawned at the end of the deferred despawn defined network tick.

docs/basics/networkobject.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ This will process the currently opened scene as well.
158158

159159
The `NetworkObject.SpawnWithObservers` property (default is true) provides you with the ability to spawn a `NetworkObject` with no initial observers. This is the recommended alternative to using `NetworkObject.CheckObjectVisibility` when you just want it to be applied globally to all clients (only when spawning an instance of the `NetworkObject` in question). If you want more precise per-client control then `NetworkObject.CheckObjectVisibility` is recommended. `NetworkObject.SpawnWithObservers` is only applied upon the initial server-side spawning and once spawned it has no impact on object visibility.
160160

161+
161162
## Transform synchronization
162163

163164
![image](/img/NetworkObject-TransformSynchronization.png)

0 commit comments

Comments
 (0)