NetworkObjectExtensions

Static extension methods that add networking convenience APIs to UnityEngine.Object and Unity.Netcode.NetworkObject. Provides shortcuts for retrieving NetworkObject components and for per-object typed messaging.

Definition

Namespace: Paragon Assembly: Paragon.dll

public static class NetworkObjectExtensions

Remarks

The per-object messaging methods use NetworkObjectId.ToString() as the handler ID, creating a unique message channel for each network object. This enables targeted communication to specific objects without requiring RPCs.

circle-info

Per-object messaging pattern: Receivers must call SubscribeToMessage on the same NetworkObject before senders call SendMessage. The handler ID is derived from the object's network ID, so both sides must reference the same spawned network object.

Quick Lookup

Goal
How

Get NetworkObject from a GameObject

gameObject.GetNetworkObject()

Get NetworkObject from a MonoBehaviour

behaviour.GetNetworkObject()

Get a NetworkObjectReference

obj.GetNetworkObjectReference()

Subscribe to per-object messages

networkObject.SubscribeToMessage<T>(callback)

Send a per-object message

networkObject.SendMessage(message)

Methods

GetNetworkObject

Retrieves the NetworkObject component from a UnityEngine.Object. Supports GameObject and MonoBehaviour inputs.

Parameter
Type
Description

@object

UnityEngine.Object

A GameObject or MonoBehaviour

Returns: The NetworkObject component on the object.

Throws: ArgumentException if the object is neither a GameObject nor a MonoBehaviour.

GetNetworkObjectReference

Creates a NetworkObjectReference from any Unity Object that has a NetworkObject component.

Parameter
Type
Description

@object

UnityEngine.Object

A GameObject or MonoBehaviour

Returns: A NetworkObjectReference wrapping the object's NetworkObject.

SubscribeToMessage<TMessage>

Subscribes to typed messages scoped to a specific NetworkObject, using its NetworkObjectId as the handler ID.

Parameter
Type
Description

networkObject

NetworkObject

The network object to scope messages to

callback

Action<TMessage>

Callback invoked when a message arrives for this object

SendMessage<TMessage>

Sends a typed message scoped to a specific NetworkObject, using its NetworkObjectId as the handler ID.

Parameter
Type
Description

networkObject

NetworkObject

The network object to send the message from

message

TMessage

The message payload

Examples

Per-Object Damage Channel

Spawning with NetworkObject

See Also

Last updated