com.unity.netcode.gameobjects
com.unity.netcode.gameobjects copied to clipboard
feat: Adds nested NetworkObject support in prefabs
Allows users to spawn nested NetworkObjects in prefabs. This is done using a new dependent NetworkObjects system.
When you spawn a prefab with nested NetworkObjects, all of the nested NetworkObjects will also be spawned/synchronized. These nested NetworkObjects will be dependent on the root NetworkObject. This means they are initially synchronized in the same message as the root NetworkObject, and cannot outlive the root NetworkObject (when the root gets destroyed the depending NetworkObjects will be destroyed as well). Aside from these two exceptions, depending NetworkObjects behave identically to normal NetworkObjects.
To achieve this I made 3 major changes:
- I added two hidden serialized fields to
NetworkObject
for saving its dependent NetworkObject and depending NetworkObjects. Respectively, these arem_DependentNetworkObject
andm_DependingNetworkObjects
. They are determined inOnValidate
and have public getters. - When
Spawn()
is called on a NetworkObject, it locally spawns depending NetworkObjects as well. - The
SceneObject
struct (which contains the data needed to spawn or initially synchronize NetworkObjects) now contains a list ofDependingSceneObject
s. This is a new struct that contains a subset of the properties ofSceneObject
along with aIsSpawned
boolean. When a SceneObject is spawned remotely, the information in theDependingSceneObject
list is used to initialize the depending NetworkObjects. AnyDependingSceneObject
whereIsSpawned
is false has its corresponding NetworkObject destroyed instead of initialized. This occurs when a depending NetworkObject has been destroyed before the NetworkObject it is dependent on.
Resolves #2637
Changelog
- Added: The ability to spawn (and properly network) prefabs with nested NetworkObjects
Testing and Documentation
- Adds tests to ensure that:
- Player prefabs can have nested NetworkObjects
- Depending NetworkObjects are reparented properly
- Depending NetworkObjects are deleted properly
- Dependent NetworkObjects are deleted properly
PR has been updated to fix a number of issues and to add tests
Any update on this? The PR is not merged
Still waiting after almost a year :( In my current production it would be rather necessary to have this feature...