Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.01k stars 182 forks source link

[Question] Why are unions only supported for class and not struct, and is there a work around? #299

Open Karatakos opened 1 month ago

Karatakos commented 1 month ago

My data is encapsulated via components attached to entities (ECS). For client/server communication i need to serialize a set of struct components for any given entity so i can compress and send over the wire.

Since each component is a unique type, and Memorypack does not output type information, I wanted to solve by using a union on some interface my struct components could implement, but noticed the compiler error noting class only support for unions.

So:

  1. Is it possible to get union working with structs? Assuming not, then:
  2. Is there a recommended workaround or alternative for this use case?

Many thanks.