- Home /
Texturing a cube; different textures on a face
I'm probably unaware of the proper keywords to search for this, but i know I can't be the first one to ask this:
I want to texture a cube that has the different textures for each of the sides. Can this be done within unity, on a prefab?
Thanks in advance!
I use a very lazily simple, yet inefficient method. I don't use cubes. Ins$$anonymous$$d, I make a cube of 6 square shaped planes. I then save the 'cube' into a prefab for future use. Then I individually apply such textures. I then
Answer by jc_lvngstn · Nov 04, 2010 at 01:12 PM
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 1 = your first texture 2 = 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.
I'd love it if you could give some code samples, I've tried to do what you have explained but I don't know what I need to put in the "TextureAtlas" and can't figure out how to apply the AtlasUVs to the uvs of the cube.
Answer by gaps · Feb 06, 2014 at 09:45 PM
The default cube from Unity does not allow that. But you can make a new cube from 6 quads!
I've made it into a prefab for you, which you can find here. With it, you can apply different textures to each side :)
Is it possible to set a flag in the inspector to allow us to map to the inside of the cube?
Thanks a bunch! I'm struggling that Textures on the Unity Cube are upside down and your cube lets me place the textures in the direction I want and need! :)
really dumb noob question, but once i model my die or cube in Blender, how do I make the 6 quads and export as 6 quads?
Answer by IJM · Oct 21, 2010 at 05:35 AM
You can use FBX model for that.
How does unity decide what part of the texture goes on what side, is that from the 3d software you used to make the cube in?
Sadly, my game is based around cubes, and because of their simplicity I used Unity's cubes because they're easy, quick and precise. I believe I have to scale your model by 6.5, but even then i'm not sure (there seems to be a seam).
If the above question is yes, I suppose I'll have to rework some levels.
By UV mapping. Yes, UV mapping on this cube sucks..but you can make a better cube in just a few $$anonymous$$s with Blender or something else. Or you can use $$anonymous$$esh class to generate a cube from script inside Unity: http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$esh.html
i cave c4d and tryed to do as $$anonymous$$attew says but it isnt working could u tell me a modeling software that does this?(multiple materials,textures on a mesh)
hmm link is broken again... can we put it somewhere permanent?
most of the Unity example projects contain FBX files. Replacing the broken link with a link to the Unity Resources.
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Texture Compression format for UWP,Texture 0 Answers
3D Models not texturing properly 1 Answer
How to create an image from byte array and display it? 1 Answer
Terrain Problem -2 Answers