@snacktime Iāve tried to use your code (missing some extension methods e.g. ReadInt so I hope i changed it correctly) but Iāve encountered a few problems.
- Regularly it crashes with an Overflow exception creating the StreamWriter ctor and a ārandomā large negative memory size value. I first thought it is related to job.Schedule().Complete() but it also occurs on the same thread with job.Run() so I believe it is because of unmanaged memory access?
- After deserailization the MemorySize seems to be unintialized memory (ārandomā value)
- ReadInt is not available, so I changed it to BlobAssetReference x = reader.Read(); which seems to be correct
- Write(int) is not available either, so I changed it to
BlobAssetReference x = BlobAssetReference.Create(collider->MemorySize); writer.Write(x); which also seems to be working.
Create the (Mesh)Collider within a Burst job
Job
[BurstCompile(CompileSynchronously = true)]
public struct CreateMeshColliderJob : IJob
{
[ReadOnly]
private NativeArray<int> indices;
[ReadOnly]
private NativeArray<float3> vertices;
public NativeArray<Collider> Collider; // Return Value, Array of Size 1
public CreateMeshColliderJob(NativeArray<float3> vertices,
NativeArray<int> indices,
NativeArray<Collider> collider)
{
this.indices = indices;
this.vertices = vertices;
Collider = collider;
}
public void Execute()
{
BlobAssetReference<Collider> blobAssetReferenceCollider = Unity.Physics.MeshCollider.Create(vertices, indices);
Collider[0] = blobAssetReferenceCollider.Value;
}
}
Calling the Code
```csharp
*var positions = new NativeArray(vertices, Allocator.Persistent);
var indices = new NativeArray(triangles, Allocator.Persistent);
var jobResultCollider = new NativeArray<Unity.Physics.Collider>(1, Allocator.Persistent);
var job = new CreateMeshColliderJob(positions, indices, jobResultCollider);
job.Run(); // Seems to fill jobResultCollider[0]
//job.Schedule().Complete(); // Seems to not fill jobResultCollider[0]
// Serialize the Collider
int bytes=0; // bytes==36910
unsafe
{
Collider c = jobResultCollider[0]; //c.Type == Mesh, c.MemorySize==36906
bytes = UnityColliderSerialization.SerializeCollider(&c, serializedColliderPath); // Crashes regularly within StreamWriter ctor because of Overflow and negative sized MemorySize
}
// Deserailize the Collider
Collider deserializedCollider =
UnityColliderSerialization.DeserializeCollider(serializedColliderPath, bytes).Value;
// deserializedCollider.Type == Mesh, deserializedCollider.MemorySize is a random value ā Uninitialized memory?,*
```
Serialize and then deserialize it
public class UnityColliderSerialization
{
public static unsafe BlobAssetReference<Unity.Physics.Collider> DeserializeCollider(string path, int bytes)
{
// bytes:36910
using (StreamBinaryReader reader = new StreamBinaryReader(path, bytes))
{
BlobAssetReference<int> x = reader.Read<int>(); // readInt is not available? Changed to Read<int>
int length = x.Value; // 36906
Collider* collider = (Unity.Physics.Collider*) UnsafeUtility.Malloc(length, 16, Allocator.Temp);
reader.ReadBytes(collider, length);
var blob = BlobAssetReference<Collider>.Create(collider, length);
UnsafeUtility.Free(collider, Allocator.Temp);
return blob;
}
}
public static unsafe int SerializeCollider(Unity.Physics.Collider* collider, string path)
{
int size = collider->MemorySize;
int intsize = sizeof(int);
int bytes = size + intsize;//36910
using (StreamBinaryWriter writer = new StreamBinaryWriter(path, bytes))
{
BlobAssetReference<int> x = BlobAssetReference<int>.Create(collider->MemorySize); // no overload found, changed to BlobAssetReference<int>.Create(collider->MemorySize)
writer.Write(x);
writer.WriteBytes(collider, collider->MemorySize);
}
return bytes;
}
}
Any guidance/links to tutorials/examples are highly appreciated, as this is quite new territory for me.