NetworkMessage

Generic struct that wraps a typed payload for network serialization. Implements Unity Netcode's INetworkSerializable to handle reading and writing the payload to/from network buffers. Supports a fixed set of primitive types, Unity vector types, and any custom INetworkSerializable type.

Definition

Namespace: Paragon.Core.Network Assembly: Paragon.dll

public struct NetworkMessage<TPayload> : INetworkSerializable

Implements: Unity.Netcode.INetworkSerializable

Remarks

NetworkMessage<TPayload> is a thin serialization wrapper — it does not define message routing or handling. The NetworkMessenger creates instances when sending, and NetworkMessageHandler<TMessage> deserializes them when receiving.

Serialization is handled via type-checked branching rather than generic constraints, which means unsupported types will throw NotSupportedException at runtime, not compile time.

Supported Payload Types

Type
Category

string

Primitive

int

Primitive

byte

Primitive

Vector2

Unity Vector

Vector3

Unity Vector

Vector2Int

Unity Vector

Vector3Int

Unity Vector

Any INetworkSerializable

Custom

Quick Lookup

Goal
How

Create a message

new NetworkMessage<int>(42)

Access the payload

message.Payload

Send over network

Pass to NetworkMessenger.SendMessage() (done internally)

Properties

Payload

The typed payload carried by this message. Read-only after construction.

Constructor

NetworkMessage(TPayload)

Creates a new message wrapping the given payload.

Parameter
Type
Description

payload

TPayload

The value to transport over the network

Methods

NetworkSerialize<T>

Serializes or deserializes the payload using Unity Netcode's BufferSerializer. Called automatically by the Netcode transport layer.

Parameter
Type
Description

serializer

BufferSerializer<T>

The Netcode buffer serializer (reader or writer)

Reading: Determines the payload type, reads the appropriate value from the FastBufferReader, and casts it to TPayload.

Writing: Pattern-matches the payload value to determine its type, writes it to the FastBufferWriter.

Type Parameters

Parameter
Constraint
Description

TPayload

(none)

The type of the message payload. Must be one of the supported types (see below).

Common Pitfalls

circle-exclamation
circle-exclamation

Examples

Primitive Payload

Custom INetworkSerializable Payload

See Also

Last updated