Let’s say you have a cube, where each side needs to display a different texture (1-6). In your main script, declare this:
(this is from memory, so bear with me)
public Texture2d[] AllCubeTextures;
public Texture2d TextureAtlas;
public Rect[] AtlasUvs;
Now, when you look at the 2d texture array in the Unity inspector, let’s say starting out you have 6 textures, though eventually you may have dozens.
Tell the array in the inspector that it has 6 elements
assign the appropriate texture to each element in the inspector
- your first texture
- your second texture,
- … and so on to 6 textures
Now, in back in your mainscript…you need to use Texture2d.Packtextures
to pack these into one big texture, the TextureAtlas
. If I remember correctly, it’s something like this:
AtlasUvs = TextureAtlas.PackTextures(AllCubeTextures);
To generate the cube, just build a mesh “by hand”, check out the examples for creating a procedural mesh. I’m some I or someone else can give more information on this if you need it.
So, let’s say you have a cube, with each face assigned a texture number. We have a cube where the “front” side needs to show texture number 3, maybe it’s a brick texture.
When building the uvs for that cube, you just assign the values from AtlasUvs[3]
to the uvs for that face. Sorry if this is not specific enough, I’m not in a position to actually post code for building a cube’s faces with uvs, etc. When you actually tell unity to create the mesh, you tell it to use your TextureAtlas
texture. All cubes you build would use this same texture atlas, thus everyone really just shares one material.
I hope this is helpful. I think it’s a better approach than having a material for each side of the cube, unless you need different shaders for each side of the cube or something.