com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

feat: Adds nested NetworkObject support in prefabs

Open Clicky02 opened this issue 1 year ago • 5 comments

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:

  1. I added two hidden serialized fields to NetworkObject for saving its dependent NetworkObject and depending NetworkObjects. Respectively, these are m_DependentNetworkObject and m_DependingNetworkObjects. They are determined in OnValidate and have public getters.
  2. When Spawn() is called on a NetworkObject, it locally spawns depending NetworkObjects as well.
  3. The SceneObject struct (which contains the data needed to spawn or initially synchronize NetworkObjects) now contains a list of DependingSceneObjects. This is a new struct that contains a subset of the properties of SceneObject along with a IsSpawned boolean. When a SceneObject is spawned remotely, the information in the DependingSceneObject list is used to initialize the depending NetworkObjects. Any DependingSceneObject where IsSpawned 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:
    1. Player prefabs can have nested NetworkObjects
    2. Depending NetworkObjects are reparented properly
    3. Depending NetworkObjects are deleted properly
    4. Dependent NetworkObjects are deleted properly

Clicky02 avatar Jul 27 '23 03:07 Clicky02

CLA assistant check
All committers have signed the CLA.

unity-cla-assistant avatar Jul 27 '23 03:07 unity-cla-assistant

PR has been updated to fix a number of issues and to add tests

Clicky02 avatar Jul 30 '23 20:07 Clicky02

Any update on this? The PR is not merged

razveck avatar Jan 30 '24 12:01 razveck

Still waiting after almost a year :( In my current production it would be rather necessary to have this feature...

Toastarve avatar Jun 11 '24 12:06 Toastarve