void RebuildColliders()
{
while (meshes.transform.childCount > 0) {
DestroyImmediate(meshes.transform.GetChild(0).gameObject);
}
Vector2 chunkOrigin = new Vector2(chunkX * CHUNK_SIZE, chunkY * CHUNK_SIZE); //voxels[0].position;
// Scan horizontally all chunk voxels, and group them into solid rows
// Create one child GameObject for each row
for (int y = 0; y < CHUNK_VOXELS_DIM; y++) {
GameObject coll_go = new GameObject();
coll_go.layer = LAYERID_SOLID;
coll_go.transform.parent = meshes.transform;
BoxCollider2D bc = coll_go.AddComponent<BoxCollider2D>();
bc.transform.parent = coll_go.transform;
bc.name = "Box Collider R" + y;
Vector2 bcBegin = chunkOrigin + new Vector2(0, y * VOXEL_SIZE);
int bcWidth = 0;
for (int x = 0; x < CHUNK_VOXELS_DIM; x++) {
if (voxels[y * CHUNK_VOXELS_DIM + x].IsSolid()) {
bcWidth++;
continue;
}
if (bcWidth > 0) {
AddBoxColliderTo(coll_go, ref bc, bcBegin, bcWidth);
// Make new collider to continue
bc = coll_go.AddComponent<BoxCollider2D>();
bc.transform.parent = coll_go.transform;
bc.name = "Box Collider R" + y;
bcBegin.x += (bcWidth + 1) * VOXEL_SIZE;
bcWidth = 0;
} else {
bcBegin.x += VOXEL_SIZE;
}
}
// Finalize if there was open counting row
if (bcWidth > 0) {
AddBoxColliderTo(coll_go, ref bc, bcBegin, bcWidth);
}
if (bc != null) { Destroy (bc); }
}
}