My Unity version is 2023.2.17, and the Netcode version is 1.8.1
I refer to this article Custom serialization | Unity Multiplayer Networking To perform custom serialization extensions, I need to serialize some Structs from external libraries. However, even if I follow the corresponding definitions in the documentation, there will still be issues during compilation I tried to define any custom Struct but failed, but when I adjusted the data structure to Class, it could be successfully serialized. What is the reason for this?
#nullable enable
using Unity.Netcode;
public static class SerializationExtensions
{
public static void ReadValueSafe(this FastBufferReader reader, out MyStruct value)
{
ByteUnpacker.ReadValuePacked(reader, out int i);
value = new MyStruct { Value = i };
}
public static void WriterValueSafe(this FastBufferWriter writer, in MyStruct value)
{
BytePacker.WriteValuePacked(writer, value.Value);
}
public static void ReadValueSafe(this FastBufferReader reader, out MyClass value)
{
ByteUnpacker.ReadValuePacked(reader, out int i);
value = new MyClass { Value = i };
}
public static void WriteValueSafe(this FastBufferWriter writer, in MyClass value)
{
BytePacker.WriteValuePacked(writer, value.Value);
}
}
public struct MyStruct
{
public int Value;
}
public class MyClass
{
public int Value;
}
#nullable enable
using Unity.Netcode;
using UnityEngine;
public partial class Test : NetworkBehaviour
{
[Rpc(SendTo.Everyone)]
private void TestRpc(MyClass value) // It works
{
Debug.Log($"value: {value}");
}
[Rpc(SendTo.Everyone)]
private void TestRpc(MyStruct value) // It not works
{
Debug.Log($"value: {value}");
}
}