Hello, I have been working on a procedurally generated 2d map program using perlin noise. I have the project working, using minecraft as a sort of starting point for how to think about things. I have been able to generate chunks of 10x10 tiles and put them together into a single texture, then have a map that takes all those textures and displays them as a single... However it is slow as can be. Code (CSharp): private Sprite BuildMapSprite() { //create a texture a number of pixels large as the number of chunks to show Texture2D texture = new Texture2D(Chunk.CHUNK_WIDTH * Chunk.BLOCK_WIDTH * MAP_WIDTH, Chunk.CHUNK_HEIGHT * Chunk.BLOCK_HEIGHT * MAP_HEIGHT); for (int x = 0; x < MAP_WIDTH; ++x) { for(int y = 0; y < MAP_HEIGHT; ++y) { //get the block (x) in the chunk ((int)Camera.main.transform.position / 4) Color[] chunkPixels = getChunkByPos(x + (int)Camera.main.transform.position.x / 4, y + (int)Camera.main.transform.position.y / 4).ChunkMesh.GetPixels(); texture.SetPixels(x * Chunk.CHUNK_WIDTH * Chunk.BLOCK_WIDTH, y * Chunk.CHUNK_HEIGHT * Chunk.BLOCK_HEIGHT, Chunk.CHUNK_WIDTH * Chunk.BLOCK_WIDTH, Chunk.CHUNK_HEIGHT * Chunk.BLOCK_HEIGHT, chunkPixels); } } texture.Apply(); Rect rect = new Rect(0, 0, texture.width, texture.height); return Sprite.Create(texture, rect, new Vector2(.5f,.5f)); } The above code works, but getting all the pixels from each chunk that I want to show and then putting those pixels on the map texture is chugging the game every time I call the method. Is there any better way of putting all the chunk textures together rather than pixel by pixel?
Cache Camera.main. It uses Gameobject.Find. Why don't you use tilemaps? It will be faster than any custom system.
Right, the camera should be a fairly minor stress in all of this, though. I'm not using tilemaps because the map is going to be editable by tile, I didn't think tilemaps were good for that, though I might be wrong.
So I have discovered part of my issue is that creating a sprite with a texture that big is really slow. It is accounting for nearly half my frame rate drop. Is there a better way to change a texture at runtime?
Tilemaps is definitely much better to achieve what you’re after. Currently I’m building a dwarf fortress/rimworld type game and this is what I’m using. To switch a given tile (I.e dirt to stone) in a tilemap you simply apply a new TIle (which has a sprite/sprite animation attached). This is very easily done via code and I can show examples. It’s literally as simple as Code (csharp): setTile Tilemaps and tiles can use physics just like any other regular sprite that you drop into the scene. Much more efficient rendering, you can get things down to a few batches quite easily. Also you’ll want to cache your camera as already suggested, your calling gameobject.find literally tens of thousands of times in your for loop.
Thanks a bunch, I have been looking more into tilemaps and I can see how I misunderstood them before. I have been working on cashing more and more stuff and I have gotten up to 90fps using tilemaps rendering more than 10,000 tiles.