I have a component that currently holds 14 bool variables. I was wondering if burst is able to automatically optimize this away to 1 bit per bool, instead of 1 byte per bool or even 4 bytes per bool (according to certain sources)?
If not then I would probably want to store them all in the form of a 2-bytes (16 bit) bitmask. And are there any existing tools in UnityPackages for creating a bitmask from multiple bools?
If you have things slightly larger than a bool but smaller than a byte (or other awkward sizes like 10 bits), and really need to pack them like C bitfields, I made this:
An alternative is to use enums with the Flags attribute instead of a BitField32/64. It also comes with type safety features, lets you define the size of the field, and lets you name each individual flags, which might lend some readability to whatever you are writing.
The nice thing about the BitField* types is you should be able to use Explicit struct layouts to create a union with an ordinary value of the type for serialization. I haven’t tested this out myself, but I am looking at getting rid of my custom bit manipulation code in favor of BitField and friends.
NativeBitArray seems to be an arbitrary sized array, and there’s conversion tools for converting a block of data to a NativeBitArray.
It seems to support bitwise operations just fine. Casts to and from the underlying type seem to be no-ops when compiled, and it still guarantees a certain level of type safety when reading/writing values. If you are using the flags more widely, resizing it to be larger is easier just by changing the underlying type the flags use. Here’s an excerpt of how I structure the flags on my players: