Procedural generation performance

Hi. I’m working on a roguelike project where I want to procedurally generate a 2D-map.
My plan was to use images to do the actual room design, instead of prefabs for example, and have Unity read the color values and create a “block”-game object corresponding to the color value of each pixel.
Example Image:

That is working fine and all, but the way I’m creating the game objects I’m keeping every single block as a gameobject , and since I plan on having around 10 of these rooms in every scene as well as connections between them, that counts up to a lot of game objects.

Accordingly, I’m experiencing a lot of lag, and I’m not sure what to do to prevent it.

I’ve tried to play with rendering distance but it didn’t seem to have any effect. Should I destroy the objects and recreate them when the player gets close?

Does anyone have another suggestion on how to convert the images into gameobjects? I’ve tried looking around the forum but haven’t really found anything that I could apply to this project!

Thanks for any help!

It sounds like you’re making a voxel type game. It might be worth doing one of two things. I haven’t done them myself, but I know this is the route to take.

Other people who’ve made voxel games before might have better advice, but that’s my 2 cents.

321610
5120 simple game objects. That’s small time. Especially because you’re only displaying 512 at once. Might want to look at your proc gen code. This shouldn’t be causing you any significant lag.

1 Like

Why would you create your map out of game objects.

You can use draw calls like this.

    Mesh floorMesh;
    Mesh wallMesh;
    Material floorMaterial;
    Material wallMaterial;
    int wallIndex;
    int floorIndex;
    int[,] map;

    void Update()
    {
        for(int x = 0; x != map.GetLength(0); x ++)
            for(int z = 0; z != map.GetLength(1); z ++)
            {
                Vector3 renderPosition = new Vector3(x,0.0f,z);
                if(map[x,z] == floorIndex)
                    Graphics.DrawMesh(floorMesh,renderPosition,Quaternion.identity,floorMaterial,0);
                if(map[x,z] == wallIndex)
                    Graphics.DrawMesh(wallMesh,renderPosition,Quaternion.identity,wallMaterial,0);
            }
    }

Generate a mesh that represent your map like most voxel programs do.
Or if its a 2d game draw your map to a texture and use a quad for your map display.