- Home /
Apply uv coordinates to unity cube by script
I'm currently using built-in primitives from unity and I don't need more for my game. Now I want to place a texture on one side of a cube primitive. As far as I know, that is only possible, with uv coordinates. Is it possible to apply such coordinates by script or has anyone another idea, to achive this without messing up with a 3d software?
This is interesting, I am trying the same thing right now! And that spins me out slightly =]
Below is a script to read the mesh info from the cube. I however am having trouble assigning the UV's for the back face.
I have just discovered if you follow the verts, they are all messed up. I am drawing out the tri's with the vert numbers to find what vert goes with what tri to set the UVs.
Check my answer below ($$anonymous$$ $$anonymous$$ay).
Answer by AlucardJay · Aug 01, 2012 at 12:56 PM
First I read the native cube mesh with :
#pragma strict
public var theCube : Transform;
private var theVerts : String = "";
private var theUVs : String = "";
private var theNorms : String = "";
private var theTris : String = "";
function Start ()
{
if (theCube == null)
theCube = this.transform;
var theMesh : Mesh = theCube.GetComponent(MeshFilter).mesh as Mesh;
theVerts = theVerts + "(" + theMesh.vertices.length.ToString() + ") :";
theUVs = theUVs + "(" + theMesh.uv.length.ToString() + ") :";
theNorms = theNorms + "(" + theMesh.normals.length.ToString() + ") :";
theTris = theTris + "(" + theMesh.triangles.length.ToString() + ") :";
for (var i:int = 0; i < theMesh.vertices.length; i ++) {
theVerts = theVerts + " " + i + "=" + theMesh.vertices[i].ToString();
theUVs = theUVs + " " + i + "=" + theMesh.uv[i].ToString();
theNorms = theNorms + " " + i + "=" + theMesh.normals[i].ToString();
}
for (i = 0; i < theMesh.triangles.length; i ++) {
theTris = theTris + " " + i + "=" + theMesh.triangles[i].ToString();
}
}
function OnGUI ()
{
GUI.Label( Rect( 10, Screen.height * 0.00, Screen.width - 20, Screen.height * 0.25), "Verts " + theVerts );
GUI.Label( Rect( 10, Screen.height * 0.25, Screen.width - 20, Screen.height * 0.25), "UVs " + theUVs );
GUI.Label( Rect( 10, Screen.height * 0.50, Screen.width - 20, Screen.height * 0.25), "Normals " + theNorms );
GUI.Label( Rect( 10, Screen.height * 0.75, Screen.width - 20, Screen.height * 0.25), "Tris " + theTris );
}
With the information displayed from OnGUI, I wrote down on a piece of paper each vertices location, then each triangle connection between the vertices.
Imagine looking at the front of the cube, the first 4 vertices are arranged like so
// 2 --- 3
// | |
// | |
// 0 --- 1
then the UV's are mapped as follows :
theUVs[2] = Vector2( 0, 1 );
theUVs[3] = Vector2( 1, 1 );
theUVs[0] = Vector2( 0, 0 );
theUVs[1] = Vector2( 1, 0 );
// 2 3 0 1 Front
// 6 7 10 11 Back
// 19 17 16 18 Left
// 23 21 20 22 Right
// 4 5 8 9 Top
// 15 13 12 14 Bottom
So where the UV's for Vertices 2 is ( 0, 1 ), so is 6, 19, 23, 4, 15.
You can test this by replacing all the zero's with 0.5, then check every face of the cube has the same portion of the image, and rotated the correct way (not inverted) . Note if standing at the origin looking up the positive Z-Axis, Right is your right.
Ok I have found the uv coordinates an I can manipulate them. But I'm not quite sure what the numbers mean. there are 4 uv coordinates for the whole cube?? Or more? I can add only one texture to the object so I have to put all images, i want to assign to the different sides of the cube together in one image - that means two images - one for the front side and one für the other sides - merged to one image The Vector2 saved in each uv are the image coordinates? ore the percetage of the image?
So if I want a special image on the top i have to manipulate the uvs 9,8,5,4?? right?
The UVs are the point of the image to set on the vertices. the UV coordinates are in relation to your image.
normally when you draw a picture, the top-left is 0,0 and the bottom right is 1,1
Unity UV's are up-side-down. So the top of your picture is 0,1 and the bottom is 1,0. This just makes it even more confusing!
Ok so imagine this is the picture/texture/material :
// 0,0 ------------ 1,0
// | |
// | PIC |
// | |
// 0,1 ------------ 1,1
In unity it is actually
// 0,1 ------------ 1,1
// | |
// | PIC |
// | |
// 0,0 ------------ 1,0
so say I want the top left quarter of the image on the cube, the UV coordinates would be
Top-Left of texture : 0.0 , 1.0
Top-Right of texture : 0.5 , 1.0
Bottom-Left of texture : 0.0 , 0.5
Bottom-Right of texture : 0.5 , 0.5
That's the UV's you tell to the vertices.
Ok, I think I got it - I will try it out and let you know, if it works fine for me ;) Thank you anyway.
Ok, since you did all the theory, I'll now give you the script (I'm not sure if you're familiar with working with meshes, and I understand how frustrating it would be if you were so close to finishing, then had to learn alot of new stuff just to finish). There's other (I'll say cheating) ways of doing this, like creating a plane and parenting it to cube, but now you have this you can see what other funky stuff you can do, like changing the UVs in an Update, and therefore playing an animation on the top or any number of faces on the cube =]
Here's the script, just attach it to the cube :
#pragma strict
// TOP
function Start()
{
// Get the mesh
var the$$anonymous$$esh : $$anonymous$$esh;
the$$anonymous$$esh = this.transform.GetComponent($$anonymous$$eshFilter).mesh as $$anonymous$$esh;
// Now store a local reference for the UVs
var theUVs : Vector2[] = new Vector2[the$$anonymous$$esh.uv.Length];
theUVs = the$$anonymous$$esh.uv;
// set UV co-ordinates
theUVs[4] = Vector2( 0.5, 1.0 );
theUVs[5] = Vector2( 1.0, 1.0 );
theUVs[8] = Vector2( 0.5, 0.5 );
theUVs[9] = Vector2( 1.0, 0.5 );
// Assign the mesh its new UVs
the$$anonymous$$esh.uv = theUVs;
}
I've put some different values in for the picture UVs so you can see them working in relation to your image (this should show from the top to near the bottom , and the left-half). Happy Coding =]
Ok, it worked partly: I made an image with two images arranged horizontal. the image on the right side was attached to the top of the cube (worked perfectly!), the second image should appear on each of the other sides of the cube. But there is only a part of it appearing on the sides. here is my uv assigning code - maybe anyone can find a mistake ...
Ahhh - found it myself.
Anyway thanks for the awesome stuff - helped a lot!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613075020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Proper texture mapping with standart primitives 1 Answer
Texturing a icosphere 0 Answers