2

I am making a unity 2D RTS game and I thought of using a big texture for the tiled map (instead of a lot of textures - for the memory reasons...).

The tiled map is supposed to generate randomly at runtime so I don't want to save a texture and upload it. I want the map to be generated and then build it from a set of textures resources.

so, I have a little tiles textures of grass/forest/hills etc. and after I generate the map randomly, I need to draw those little textures on my big map texture so I will use it as my map.

  1. How can I draw a texture from my resources on other texture? I saw there is only a Get/SetPixel functions... so I can use it to copy all the pixels one by one to the big texture, but there is something easier?
  2. Is my solution for the map is OK? (is it better from just create a lot of texture tiles side by side? There is other better solution?)
David
  • 15,894
  • 22
  • 55
  • 66
galhajaj
  • 125
  • 1
  • 10

2 Answers2

1

Well, after more searching I discovered the Get/SetPixel s

Texture2D sourceTex = //get it from somewere        

var pix = sourceTex.GetPixels(x, y, width, height); // get the block of pixels
var destTex = new Texture2D(width, height); // create new texture to copy the pixels to it
destTex.SetPixels(pix);
destTex.Apply(); // important to save changes
galhajaj
  • 125
  • 1
  • 10
1

The correct way to create a large tiled map would be to compose it from smaller, approximately-screen-sized chunks. Unity will correctly not draw the chunks that are off the screen.


As for your question about copying to a texture: I have not done this before in Unity, but this process is called Blitting, and there just happens to be a method in Unity called Graphics.Blit(). It takes a source texture and copies it into a destination texture, which sounds like exactly what you're looking for. However, it requires Unity Pro :(

There is also SetPixels(), but it sounds like this function does the processing on the CPU rather than the GPU, so it's going to be extremely slow/resource-intensive.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
  • awsome answer - but, you say that actually the right thing is not to make a large sprite but to place a lot of squares side by side??? thats a lot of instances... sounds wrong to me... – galhajaj Feb 04 '14 at 17:38
  • No, it's correct. If you have enough sprite-instance that the size of the world would be a concern, you should unload (destroy) sprites that are far away from the camera, and load them back in (instantiate) when you get near them. – BlueRaja - Danny Pflughoeft Feb 04 '14 at 17:51