I’m testing my ability by trying to make a minecraft clone, and so far I have had a lot of trouble with Garbage collection. I have handled most of the issues causing GC but this one function has had me stumped.
void GenerateDefaultBlocks() //fills 8 by 8 by 8 chunk
{
for (int z = 0; z < 8; z++)
{
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
blocks.Add(new Block(x + xPos, y + yPos, z + zPos, DefaultBlock));
}
}
}
}
This adds 512 blocks to a chunk with their respective positions and block type.
“blocks” is a list of all the chunk’s blocks.
Below is what the block class looks like. The index and length variables are stored so I can take a specific block’s data out of the triangle, vertex, and uv lists in the Chunk script when the block is destroyed. “BlockSO” means block scriptable-object which is how I am storing block types like grass or stone. umin, umax, vmin, and vmax are the block’s texture’s uv coordinates on the master texture of all the blocks.
[System.Serializable]
public class Block
{
//position
public int x;
public int y;
public int z;
//indexes
public int vertIndex;
public int uvIndex;
public int triIndex;
public int triLength;
//block type
public BlockSO block;
//Texture Co-ords
public float umin;
public float umax;
public float vmin;
public float vmax;
public Block(int X, int Y, int Z, BlockSO Block)
{
x = X;
y = Y;
z = Z;
block = Block;
umin = Block.umin;
umax = Block.umax;
vmin = Block.vmin;
vmax = Block.vmax;
}
}
Here is the Profiler reading of the function. It creates ~36 kilobytes per chunk and this adds up easily.
Is there any workaround or fix for my issue? I have heard that making the Block class into a struct might work, but I need specific references to the blocks quite frequently, so making it a value type would be troublesome.