I’ve been writing an editor tool in the form of a MonoBehavior with [ExecuteInEditMode]
. That tool uses native Unity Collections such NativeBitArray
.
One function of my script needs to save that bit array to a file, so I use AsByteArray
to get its bytes and dump that.
var presenceBytes = grid.presence.AsNativeArray<byte>();
binaryWriter.Write((uint)presenceBytes.Length);
for (int i = 0; i < presenceBytes.Length; ++i)
{
binaryWriter.Write(presenceBytes[i]);
}
It has worked in the past but today it keeps printing this error:
Assertion failed on expression: '!IsSecondaryVersion(handle)'
UnityEngine.StackTraceUtility:ExtractStackTrace ()
Unity.Collections.NativeBitArray:AsNativeArray<byte> () (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/NativeBitArray.cs:179)
What’s weird is that this specifically happens on the line where I do AsByteArray
. If I do anything else such as iterating and reading all bits of the array, it passes through them just fine and prints expected results:
int setCount = 0;
int unsetCount = 0;
for (int i = 0; i < grid.presence.Length; ++i)
{
if (grid.presence.IsSet(i))
{
++setCount;
}
else
{
++unsetCount;
}
}
Debug.Log($"Set: {setCount}, Unset: {unsetCount}");
And all other features of my script that access that bit array work fine. It’s just AsByteArray
failing with that weird error.
I checked the number of bits in that array, and it has 49,152, which is divisible by 8, and even 64, so there shouldn’t even be a problem with interpreting it as bytes…