Class AttachableBehaviour
Attachable NetworkBehaviours
This component handles the parenting synchronization of the GameObject that this component is attached to.
under another NetworkBehaviour's GameObject.
The GameObject to be parented must have this component attached to it and must be nested on any child GameObject under the NetworkObject's GameObject.
The GameObject target parent must have an AttachableNode component attached to it and must belong to a
different NetworkObject than that of the AttachableBehaviour's.
Inherited Members
Namespace: Unity.Netcode.Components
Assembly: Unity.Netcode.Runtime.dll
Syntax
public class AttachableBehaviour : NetworkBehaviour
Remarks
The term "attach" is used in place of parenting in order to distinguish between NetworkObject parenting and
AttachableBehaviour parenting ("attaching" and "detaching").
This component can be used along with one or more ComponentController in order to enable or disable different components depending
upon the AttachableBehaviour instance's current state.
AttachableNode invocation order:
- When attaching, the AttachableNode's OnAttached(AttachableBehaviour) is invoked just before the OnAttachStateChanged(AttachState, AttachableNode) is invoked with the Attached state.
- When detaching, the AttachableNode's OnDetached(AttachableBehaviour) is invoked right after the OnAttachStateChanged(AttachState, AttachableNode) is invoked with the Detached notification.
Fields
AutoDetach
Determines if this AttachableBehaviour will automatically detach on all instances if it has one of the AttachableBehaviour.AutoDetachTypes flags.
Declaration
public AttachableBehaviour.AutoDetachTypes AutoDetach
Field Value
Type | Description |
---|---|
AttachableBehaviour.AutoDetachTypes |
Properties
m_AttachState
The current AttachableBehaviour instance's AttachableBehaviour.AttachState.
Declaration
protected AttachableBehaviour.AttachState m_AttachState { get; }
Property Value
Type | Description |
---|---|
AttachableBehaviour.AttachState |
m_AttachableNode
If attached, attaching, or detaching this will be the AttachableNode this AttachableBehaviour instance is attached to.
Declaration
protected AttachableNode m_AttachableNode { get; }
Property Value
Type | Description |
---|---|
AttachableNode |
m_DefaultParent
The original parent of this AttachableBehaviour instance.
Declaration
protected GameObject m_DefaultParent { get; }
Property Value
Type | Description |
---|---|
GameObject |
Methods
Attach(AttachableNode)
Attaches the GameObject of this AttachableBehaviour instance to the GameObject of the AttachableNode.
Declaration
public void Attach(AttachableNode attachableNode)
Parameters
Type | Name | Description |
---|---|---|
AttachableNode | attachableNode | The AttachableNode to attach this instance to. |
Remarks
This effectively applies a new parent to a nested NetworkBehaviour and all GameObject children
of the nested NetworkBehaviour.
Both the AttachableNode and this AttachableBehaviour instances should be in the spawned state before this
is invoked.
Awake()
If you create a custom AttachableBehaviour and override this method, you must invoke this base instance of Awake().
Declaration
protected virtual void Awake()
Detach()
Invoke to detach from a AttachableNode.
Declaration
public void Detach()
OnAttachStateChanged(AttachState, AttachableNode)
For customized/derived AttachableBehaviours, override this method to receive notifications when the AttachableBehaviour.AttachState has changed.
Declaration
protected virtual void OnAttachStateChanged(AttachableBehaviour.AttachState attachState, AttachableNode attachableNode)
Parameters
Type | Name | Description |
---|---|---|
AttachableBehaviour.AttachState | attachState | The new AttachableBehaviour.AttachState. |
AttachableNode | attachableNode | The AttachableNode being attached to or from. Will be null when completely detached. |
OnAwake()
Override this method in place of Awake. This method is invoked during Awake.
Declaration
protected virtual void OnAwake()
Remarks
The AttachableBehaviour's Awake method is protected to assure it initializes itself at this point in time.
OnHasAuthority()
Override this method to change how the instance determines the authority.
The default is to use the HasAuthority method.
Declaration
protected virtual bool OnHasAuthority()
Returns
Type | Description |
---|---|
bool | true = has authoriy | false = does not have authority |
Remarks
Useful when using a ClientServer network topology and you would like to have the owner be the authority of this ComponentController instance.
OnNetworkPreDespawn()
Gets called before OnNetworkDespawn() has been invoked for all NetworkBehaviours associated with the currently spawned NetworkObject instance.
Declaration
public override void OnNetworkPreDespawn()
Overrides
OnNetworkSessionSynchronized()
This method is only available client-side. When a new client joins it's synchronized with all spawned NetworkObjects and scenes loaded for the session joined. At the end of the synchronization process, when all NetworkObjects and scenes (if scene management is enabled) have finished synchronizing, all NetworkBehaviour components associated with spawned NetworkObjects will have this method invoked.
Declaration
protected override void OnNetworkSessionSynchronized()
Overrides
Remarks
This can be used to handle post-synchronization actions where you might need to access a different NetworkObject and/or NetworkBehaviour not local to the current NetworkObject context. This is only invoked on clients during a client-server network topology session.
OnOwnershipChanged(ulong, ulong)
Invoked on all clients. Override this method to be notified of any ownership changes (even if the instance was neither the previous or newly assigned current owner).
Declaration
protected override void OnOwnershipChanged(ulong previous, ulong current)
Parameters
Type | Name | Description |
---|---|---|
ulong | previous | the previous owner |
ulong | current | the current owner |
Overrides
OnSynchronize<T>(ref BufferSerializer<T>)
Override this method if your derived NetworkBehaviour requires custom synchronization data. Use of this method is only for the initial client synchronization of NetworkBehaviours and will increase the payload size for client synchronization and dynamically spawned NetworkObjects.
Declaration
protected override void OnSynchronize<T>(ref BufferSerializer<T> serializer) where T : IReaderWriter
Parameters
Type | Name | Description |
---|---|---|
BufferSerializer<T> | serializer | The serializer to use to read and write the data. |
Type Parameters
Name | Description |
---|---|
T | Either BufferSerializerReader or BufferSerializerWriter, depending whether the serializer is in read mode or write mode. |
Overrides
Remarks
When serializing (writing), this method is invoked during the client synchronization period and when spawning new NetworkObjects. When deserializing (reading), this method is invoked prior to the NetworkBehaviour's associated NetworkObject being spawned.
OnValidate()
Declaration
protected virtual void OnValidate()
Remarks
In the event an AttachableBehaviour is placed on the same GameObject as the NetworkObject, this will automatically create a child and add an AttachableBehaviour to that.
Events
AttachStateChange
Invoked when the AttachableBehaviour.AttachState of this instance has changed.
Declaration
public event Action<AttachableBehaviour.AttachState, AttachableNode> AttachStateChange
Event Type
Type | Description |
---|---|
Action<AttachableBehaviour.AttachState, AttachableNode> |