Skip to content

Commit 81290ba

Browse files
ShadauxCat0xFA11
andauthored
feat: Addressables Redux (#1882)
* feat: Addressables Redux * Standards * - Changelog - Disable deferring spawn messages when `ForceSamePrefabs` is true because there's no point * Added comments to ComponentFactory * Update com.unity.netcode.gameobjects/CHANGELOG.md Co-authored-by: Fatih Mar <[email protected]> * Removed some debug logging. * Mentioned SpawnTimeout in the changelog. * Review feedback. * - Removed another debug log - Reverted unintentional file change - Addressed more review feedback * Refactor suggested by Noel * Edited *.unity to replace MessageBufferTimeout with SpawnTimeout Co-authored-by: Fatih Mar <[email protected]>
1 parent 381dc4f commit 81290ba

File tree

46 files changed

+2629
-1052
lines changed

Some content is hidden

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

46 files changed

+2629
-1052
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
2828
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)
2929
- `UnityTransport` settings can now be set programmatically. (#1845)
3030
- `FastBufferWriter` and Reader IsInitialized property. (#1859)
31+
- Prefabs can now be added to the network at **runtime** (i.e., from an addressable asset). If `ForceSamePrefabs` is false, this can happen after a connection has been formed. (#1882)
32+
- When `ForceSamePrefabs` is false, a configurable delay (default 1 second, configurable via `NetworkConfig.SpawnTimeout`) has been introduced to gracefully handle race conditions where a spawn call has been received for an object whose prefab is still being loaded. (#1882)
3133

3234
### Changed
3335

com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ public class NetworkConfig
128128
public int LoadSceneTimeOut = 120;
129129

130130
/// <summary>
131-
/// The amount of time a message should be buffered for without being consumed. If it is not consumed within this time, it will be dropped.
131+
/// The amount of time a message should be buffered if the asset or object needed to process it doesn't exist yet. If the asset is not added/object is not spawned within this time, it will be dropped.
132132
/// </summary>
133-
[Tooltip("The amount of time a message should be buffered for without being consumed. If it is not consumed within this time, it will be dropped")]
134-
public float MessageBufferTimeout = 20f;
133+
[Tooltip("The amount of time a message should be buffered if the asset or object needed to process it doesn't exist yet. If the asset is not added/object is not spawned within this time, it will be dropped")]
134+
public float SpawnTimeout = 1f;
135135

136136
/// <summary>
137137
/// Whether or not to enable network logs.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Unity.Netcode
5+
{
6+
/// <summary>
7+
/// This class is used to support testable code by allowing any supported component used by NetworkManager to be replaced
8+
/// with a mock component or a test version that overloads certain methods to change or record their behavior.
9+
/// Components currently supported by ComponentFactory:
10+
/// - IDeferredMessageManager
11+
/// </summary>
12+
internal static class ComponentFactory
13+
{
14+
internal delegate object CreateObjectDelegate(NetworkManager networkManager);
15+
16+
private static Dictionary<Type, CreateObjectDelegate> s_Delegates = new Dictionary<Type, CreateObjectDelegate>();
17+
18+
/// <summary>
19+
/// Instantiates an instance of a given interface
20+
/// </summary>
21+
/// <param name="networkManager">The network manager</param>
22+
/// <typeparam name="T">The interface to instantiate it with</typeparam>
23+
/// <returns></returns>
24+
public static T Create<T>(NetworkManager networkManager)
25+
{
26+
return (T)s_Delegates[typeof(T)](networkManager);
27+
}
28+
29+
/// <summary>
30+
/// Overrides the default creation logic for a given interface type
31+
/// </summary>
32+
/// <param name="creator">The factory delegate to create the instance</param>
33+
/// <typeparam name="T">The interface type to override</typeparam>
34+
public static void Register<T>(CreateObjectDelegate creator)
35+
{
36+
s_Delegates[typeof(T)] = creator;
37+
}
38+
39+
/// <summary>
40+
/// Reverts the creation logic for a given interface type to the default logic
41+
/// </summary>
42+
/// <typeparam name="T">The interface type to revert</typeparam>
43+
public static void Deregister<T>()
44+
{
45+
s_Delegates.Remove(typeof(T));
46+
SetDefaults();
47+
}
48+
49+
/// <summary>
50+
/// Initializes the default creation logic for all supported component types
51+
/// </summary>
52+
public static void SetDefaults()
53+
{
54+
SetDefault<IDeferredMessageManager>(networkManager => new DeferredMessageManager(networkManager));
55+
}
56+
57+
private static void SetDefault<T>(CreateObjectDelegate creator)
58+
{
59+
if (!s_Delegates.ContainsKey(typeof(T)))
60+
{
61+
s_Delegates[typeof(T)] = creator;
62+
}
63+
}
64+
}
65+
}

com.unity.netcode.gameobjects/Runtime/Core/ComponentFactory.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)