I have this class which contains a collection of CellData, each cell stores the position and type of object to create. In the image this shows hundred of different cells.
public class CellDataArray {
public CellData[] cells;
public CellDataArray(PointDataArray pointDataArray) {
PointData[] pointData = pointDataArray.points;
cells = new CellData[pointData.Length];
for (int i = 0; i < pointData.Length; i++) {
CellData newCell = new CellData(pointData[i]);
cells[i] = newCell;
}
}
}
public struct CellData {
public int ID; // ID corresponds with a resource mesh prefab
public Vector3 Position;
public CellData(PointData data) {
cellID = data.id;
cellPosition = new Vector3(data.p[0], 0f, data.p[1]);
}
}```I need to generate the actual objects, but I want to improve my current approach, that is to for-loop over all the cells instantiate a GameObject prefab which loads the correct mesh there.
There are 2 things I dislike about this
1: It feels like its far too heavy when I have thousands of them in the scene. I have ideas for optimizing the way cells are constructed, but I need to have a better way of representing those as actual game objects.
2: Constructing/Disposing the cell instances feels like it takes extra steps (perhaps this is just a reality of Unity), i.e I cant simply do `CellDataArray.cells = new CellData[0];` and expect the objects in the scene to automatically be destroyed.
Ideally all I want is for as long as a CellData exists, I want an object to exist in the scene at its position. How its actually represented as a game object, doesnt really matter a whole bunch, it just needs to be able to load one of the FBX meshes