I'm trying to use protobuf-net in Unity but I get the following exception when I try:
TypeLoadException: Could not load type of field 'ProtoBuf.BufferPool:_pool' (0) due to: Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. assembly:System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 type:<unknown type> member:(null) signature:<none>
ProtoBuf.ProtoWriter+StreamProtoWriter.CreateStreamProtoWriter (System.IO.Stream dest, ProtoBuf.Meta.TypeModel model, System.Object userState) (at <7f2f6774d3104e98a2359d5e5462b6bc>:0)
ProtoBuf.ProtoWriter+State.Create (System.IO.Stream dest, ProtoBuf.Meta.TypeModel model, System.Object userState) (at <7f2f6774d3104e98a2359d5e5462b6bc>:0)
ProtoBuf.Serializer.Serialize[T] (System.IO.Stream destination, T instance, System.Object userState) (at <bd1d3a3f27ab42199d646f1541550b36>:0)
ProtoBuf.Serializer.Serialize[T] (System.IO.Stream destination, T instance) (at <bd1d3a3f27ab42199d646f1541550b36>:0)
ProtobufNetTest.Awake () (at Assets/Tests/Protobuf-net/ProtobufNetTest.cs:26)
I've installed protobuf-net using nuget for unity. The test script I'm using looks like this:
using ProtoBuf;
using System.IO;
using UnityEngine;
[ProtoContract]
public class ProtobufTestItself
{
[ProtoMember( 1 )]
public int IntMember { get; set; }
}
public class ProtobufNetTest : MonoBehaviour
{
void Awake()
{
var test = new ProtobufTestItself();
test.IntMember = 313373;
byte[] bytes = null;
using ( var memoryStream = new MemoryStream() )
{
Serializer.Serialize( memoryStream, test );
bytes = memoryStream.ToArray();
}
using ( var memoryStream = new MemoryStream( bytes ) )
{
var roundTripped = Serializer.Deserialize<ProtobufTestItself>( memoryStream );
Debug.Log( "Round tripped: " + roundTripped.IntMember.ToString() );
}
}
}
And this is using Unity 2019.4.15f1.
How do I go about fixing this?