Skip to content

feat: AttachableBehaviour and ComponentController #3518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: develop-2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
43906be
update
NoelStephensUnity May 22, 2025
6267a3e
update
NoelStephensUnity May 28, 2025
83ee650
update
NoelStephensUnity May 28, 2025
3914204
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity May 28, 2025
41b14b5
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity Jun 23, 2025
1832d21
refactor
NoelStephensUnity Jun 24, 2025
d08d496
test
NoelStephensUnity Jun 24, 2025
4a9775a
style
NoelStephensUnity Jun 24, 2025
1f81ebd
style - PVP
NoelStephensUnity Jun 24, 2025
4148bfe
style - standards
NoelStephensUnity Jun 24, 2025
2322d48
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity Jun 24, 2025
1fce513
refactor
NoelStephensUnity Jun 24, 2025
1ceb76c
update
NoelStephensUnity Jun 24, 2025
db05292
test
NoelStephensUnity Jun 24, 2025
2d391ed
test - update
NoelStephensUnity Jun 24, 2025
ae29873
style
NoelStephensUnity Jun 24, 2025
078bca1
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity Jun 24, 2025
ec59da9
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity Jul 10, 2025
043b0e7
update
NoelStephensUnity Jul 10, 2025
6e9530e
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity Jul 10, 2025
abd75d0
update
NoelStephensUnity Jul 10, 2025
e098e31
update
NoelStephensUnity Jul 13, 2025
cab1983
refactor
NoelStephensUnity Jul 13, 2025
1341aa0
style
NoelStephensUnity Jul 13, 2025
13d9a03
update & style
NoelStephensUnity Jul 13, 2025
8bd712c
refactor
NoelStephensUnity Jul 15, 2025
a63de0b
test
NoelStephensUnity Jul 15, 2025
17b769d
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity Jul 15, 2025
912b26f
update
NoelStephensUnity Jul 15, 2025
6d8f7a6
Merge branch 'develop-2.0.0' into feat/attachable-networkbehaviour-an…
NoelStephensUnity Jul 15, 2025
9dd32d5
update
NoelStephensUnity Jul 15, 2025
793cdb6
fix
NoelStephensUnity Jul 15, 2025
fb73b8c
style - PVP
NoelStephensUnity Jul 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Added

- Added `AttachableBehaviour` helper component to provide an alternate approach to parenting items without using the `NetworkObject` parenting. (#3518)
- Added `AttachableNode` helper component that is used by `AttachableBehaviour` as the target node for parenting. (#3518)
- Added `ComponentController` helper component that can be used to synchronize the enabling and disabling of components and can be used in conjunction with `AttachableBehaviour`. (#3518)
- Added `NetworkBehaviour.OnNetworkPreDespawn` that is invoked before running through the despawn sequence for the `NetworkObject` and all `NetworkBehaviour` children of the `NetworkObject` being despawned. (#3518)
- Added methods `GetDefaultNetworkSettings` and `GetDefaultPipelineConfigurations` to `UnityTransport`. These can be used to retrieve the default settings and pipeline stages that are used by `UnityTransport`. This is useful when providing a custom driver constructor through `UnityTransport.s_DriverConstructor`, since it allows reusing or tuning the existing configuration instead of trying to recreate it. This means a transport with a custom driver can now easily benefit from most of the features of `UnityTransport`, like integration with the Network Simulator and Network Profiler from the multiplayer tools package. (#3501)
- Added mappings between `ClientId` and `TransportId`. (#3516)
- Added `NetworkPrefabInstanceHandlerWithData<T>`, a variant of `INetworkPrefabInstanceHandler` that provides access to custom instantiation data directly within the `Instantiate()` method. (#3430)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Collections.Generic;
using Unity.Netcode;
using Unity.Netcode.Components;


/// <summary>
/// This component is used in conjunction with <see cref="AttachableBehaviour"/> and is used to
/// denote a specific child <see cref="UnityEngine.GameObject"/> that an <see cref="AttachableBehaviour"/>
/// can attach itself to.
/// </summary>
/// <remarks>
/// Primarily, the <see cref="AttachableNode"/> can be used as it is or can be extended to perform additional
/// logical operations when something attaches to or detaches from the <see cref="AttachableNode"/> instance.
/// </remarks>
public class AttachableNode : NetworkBehaviour
{
/// <summary>
/// Returns true if the <see cref="AttachableNode"/> instance has one or more attached <see cref="AttachableBehaviour"/> components.
/// </summary>
public bool HasAttachments => m_AttachedBehaviours.Count > 0;

/// <summary>
/// When enabled, any attached <see cref="AttachableBehaviour"/>s will be automatically detached and re-parented under its original parent.
/// </summary>
public bool DetachOnDespawn = true;

/// <summary>
/// A <see cref="List{T}"/> of the currently attached <see cref="AttachableBehaviour"/>s.
/// </summary>
protected readonly List<AttachableBehaviour> m_AttachedBehaviours = new List<AttachableBehaviour>();

/// <inheritdoc/>
/// <remarks>
/// If the <see cref="NetworkObject"/> this <see cref="AttachableNode"/> belongs to is despawned,
/// then any attached <see cref="AttachableBehaviour"/> will be detached during <see cref="OnNetworkDespawn"/>.
/// </remarks>
public override void OnNetworkPreDespawn()
{
if (IsSpawned && HasAuthority && DetachOnDespawn)
{
for (int i = m_AttachedBehaviours.Count - 1; i >= 0; i--)
{
m_AttachedBehaviours[i]?.Detach();
}
}
base.OnNetworkPreDespawn();
}

/// <inheritdoc/>
public override void OnNetworkDespawn()
{
m_AttachedBehaviours.Clear();
base.OnNetworkDespawn();
}

/// <summary>
/// Override this method to be notified when an <see cref="AttachableBehaviour"/> has attached to this node.
/// </summary>
/// <param name="attachableBehaviour">The <see cref="AttachableBehaviour"/> that has been attached.</param>
protected virtual void OnAttached(AttachableBehaviour attachableBehaviour)
{

}

internal void Attach(AttachableBehaviour attachableBehaviour)
{
if (m_AttachedBehaviours.Contains(attachableBehaviour))
{
NetworkLog.LogError($"[{nameof(AttachableNode)}][{name}][Attach] {nameof(AttachableBehaviour)} {attachableBehaviour.name} is already attached!");
return;
}

m_AttachedBehaviours.Add(attachableBehaviour);
OnAttached(attachableBehaviour);
}

/// <summary>
/// Override this method to be notified when an <see cref="AttachableBehaviour"/> has detached from this node.
/// </summary>
/// <param name="attachableBehaviour">The <see cref="AttachableBehaviour"/> that has been detached.</param>
protected virtual void OnDetached(AttachableBehaviour attachableBehaviour)
{

}

internal void Detach(AttachableBehaviour attachableBehaviour)
{
if (!m_AttachedBehaviours.Contains(attachableBehaviour))
{
NetworkLog.LogError($"[{nameof(AttachableNode)}][{name}][Detach] {nameof(AttachableBehaviour)} {attachableBehaviour.name} is not attached!");
return;
}

m_AttachedBehaviours.Remove(attachableBehaviour);
OnDetached(attachableBehaviour);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading