I was making a mapgame, and i stumbled upon an issue I has suspected would come. My very primitive method of generating every pixel makes the game super slow.
My current system loads a map from a txt file in this form :
0 1 1 0
0 1 0 1
0 0 1 0
basicaly every pixel is saved as a value, those values are the nlater used to generate countries.
The problem here is that Im generating every pixel as a new image that is also a child of the parent country object.
A serious problem arose when I noticed that changing colors of all the child pixels in my script makes the game lag for 5 seconds. So I decided to post this problem here.
I basicaly need to know if there is a way to generate an image from scratch, I know it is possible to generate meshes but dont know anything about Images. Or perhaps you could suggest some options in the editor to me that could help with faster rendering.
Edit : Removing all white pixels tripled the framerate to 45, but it still takes 1+ seconds to change color for the countries (no matter their size)
Hi, what exactly do you mean when you say you’re generating each pixel as a new image? If each pixel is it’s own gameObject, then the game will certainly not be able to handle it. I would suggest maybe using the tilemap system, but it can get pretty finnicky.
You could also look into generating a mesh for the entire world, as you said.
Either way you will most likely have to create some kind of chunk system, and there are plenty of good tutorials on youtube, but they will most likely not be tailored to your current situation, so you will have to do a lot on your own. But researching chunk systems would be a pretty good start.
What about creating the texture manually? If you know the dimensions of your image, you can create a texture of that size, and set the individual pixel colors of the texture quite quickly. (Even faster if you use SetPixels or SetPixels32).
var texture = new Texture2D(width, height);
var color = Color.white;
// Set all pixels to white (could change to use an array of colors to determine each individual pixel color)
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
texture.SetPixel(x, y, color);
// Applies all changes to the texture
Then, create a sprite from the texture and set it as the sprite of a sprite renderer. For example:
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height, Vector2.zero);
// This object will now display the map texture
mySpriteRenderer.sprite = sprite;
I’ve been using this technique for my terraria clone. It works pretty well for small chunks of data, but texture.Apply is the expensive method here. Use it sparingly. Hope that helps @CrusadingBanana
Create texture via setting pixel colours like @Llama_w_2Ls said. After the initial map is created, you only need to use SetPixel for any deltas. If the base map is just the grey and white, you should write that file instead of recreating it procedurally all the time, then the deltas are only just iterating through the black and the red pixels. Then apply the texture after those red and black pixels have been set.
Use GetPixel(x,y) based on your mouse position when you click on the image instead of having to create buttons.
Call appropriate method if the colour returned by GetPixel() matches whatever your criteria is.