- Home /
How can I combine 4 textures into one bigger texture at runtime?
Or 9, or 16 for that matter, obviously the same way... It seemed simple enough when I had the idea but I really have no clue how I'd actually script it or if there are existing functions to do that sort of thing easily. What it's for- I have ten possible player colors and am trying to make a custom texture atlas with both player's colored sprites on it so I can use one material without it being gigantic.
Answer by The-IT664 · Apr 18, 2013 at 03:20 AM
I can think of a couple of ways of doing this, but usually you would want to generate this texture atlas in your image editor of choice, not at runtime. I'll assume that for whatever reason you have no choice but to generate it at runtime.
1) If you have Unity Pro, you can render each of the textures onto a larger RenderTexture. I believe this is the most performance efficient method, but it isn't straight forward given that you'll have to setup the meshes/materials and their transforms for each texture before rendering them.
2) If you don't have Unity Pro or don't want to go with the first option, then you can use the Texture2D functions SetPixels32/GetPixels32 (http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.html). This would effectively mean manually transferring the pixel data from one image to another.
Neither of these solve the problem of remapping the texture coordinates in your sprites, which you would have to recalculate.
All the best!