I have a struct that I am using for generic object data.
namespace Dev.Component
{
[Serializable]
public struct Data
{
// BOOLS: an array of bool for an object to use as data
public NativeArray<bool> bools;
// INTS: an array of ints for an object to use as data
public NativeArray<int> ints;
// FLOATS: an array of floats for an object to use as data
public NativeArray<float> floats;
}
}
And I am including that in a nativehashmap.
map.data = new NativeParallelHashMap<int, Dev.Component.Data>(1, Allocator.Persistent);
I’m guessing this isn’t allowed? I can save it with the binaryformatter and an adapter but when I load it, it says its disposed.
Here is what the hashmap adapter looks like.
namespace Adapter
{
unsafe class Hashmap<TKey, TValue> : IBinaryAdapter<NativeParallelHashMap<TKey, TValue>> where TKey : unmanaged, IEquatable<TKey> where TValue : unmanaged
{
public void Serialize(in BinarySerializationContext<NativeParallelHashMap<TKey, TValue>> context, NativeParallelHashMap<TKey, TValue> value)
{
var copy = value.GetKeyValueArrays(Allocator.Temp);
try { context.Writer->Add(copy.Keys); context.Writer->Add(copy.Values); }
finally { copy.Dispose(); }
}
public NativeParallelHashMap<TKey, TValue> Deserialize(in BinaryDeserializationContext<NativeParallelHashMap<TKey, TValue>> context)
{
NativeArray<TKey> keys = default;
NativeArray<TValue> values = default;
try {
context.Reader->ReadNext<TKey>(out keys, Allocator.Temp);
context.Reader->ReadNext<TValue>(out values, Allocator.Temp);
var map = new NativeParallelHashMap<TKey, TValue>(keys.Length, Allocator.Persistent);
for (int index = 0; index < keys.Length; index++) { map.Add(keys[index], values[index]); }
return map;
}
finally {
if (keys.IsCreated) { keys.Dispose(); }
if (values.IsCreated) { values.Dispose(); }
}
}
}
}