Skip to content

Instantly share code, notes, and snippets.

@gamemachine
Created July 12, 2019 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamemachine/6eafec133e837249159743b61840b9c6 to your computer and use it in GitHub Desktop.
Save gamemachine/6eafec133e837249159743b61840b9c6 to your computer and use it in GitHub Desktop.
public static string GetSerializationPath(string name)
{
return string.Format("{0}/{1}.bin", Application.streamingAssetsPath, name);
}
public static unsafe BlobAssetReference<Collider> DeserializeCollider(string filename, int bytes)
{
string path = GetSerializationPath(filename);
using (StreamBinaryReader reader = new StreamBinaryReader(path, bytes))
{
int length = reader.ReadInt();
Collider* collider = (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(Collider* collider, string filename)
{
string path = GetSerializationPath(filename);
int bytes = collider->MemorySize + sizeof(int);
using (StreamBinaryWriter writer = new StreamBinaryWriter(path, bytes))
{
writer.Write(collider->MemorySize);
writer.WriteBytes(collider, collider->MemorySize);
}
return bytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment