Custom Types
Overview
Custom types for Networked Properties and RPC are supported. Custom types must be structs and implement the INetworkStruct
interface.
C#
public struct CustomStruct : INetworkStruct
{
public int IntField;
}
Supported Field Types
INetworkStruct
only supports limited types:
- Primitives: byte, short, int, long, ushort, uint, ulong, float, double
- Unity Types: Vector2, Vector3, Vector4, Quaternion, Vector2Int, Vector3Int, Rect, RectInt Color, Color32
- Fusion Types: NetworkString<>, NetworkBool, Angle, NetworkId, PlayerRef, SceneRef, TickTimer
- Structs implementing INetworkStruct
Bools and Strings
Do NOT use bools or strings inside INetworkStruct
Use NetworkBool
and NetworkString<T>
instead. You can get/set them with regular bool / string values like this:
C#
using Fusion;
using UnityEngine;
public class NetworkStructSampleCode : NetworkBehaviour
{
public struct CustomStruct : INetworkStruct
{
public NetworkString<_16> Name;
public NetworkBool Bool;
}
public CustomStruct MyStruct { get; set; }
public void SetStringAndBool(string name, bool bool)
{
MyStruct.Name = name;
MyStruct.Bool = bool;
}
}
INetworkStruct Nesting
Since structs which implement INetworkStruct
are allowed as fields, they can be used as fields inside other INetworkStruct
definitions.
C#
using Fusion;
using UnityEngine;
public class NetworkStructSampleCode : NetworkBehaviour
{
public struct InnerStruct : INetworkStruct
{
public int IntField;
}
public struct OuterStruct: INetworkStruct
{
public InnerStruct Inner;
}
[Networked]
public OuterStruct Outer { get; set; }
}
OnChanged with Nested INetworkStructs
The OnChanged callback for nested INetworkStructs
detects changes in any change the the entire struct, and individual values cannot be monitored for changes. If you need a callback for individual fields/properties, keep that variable in the NetworkBehaviour
as its own Networked Property.