Game Development Reference
In-Depth Information
Listing 3.2 Script to Modify the UVs of a Plane
//Texture Atlas Specifications
var startPixel:Vector2;
var endPixel:Vector2;
function Start()
{
//get object mesh
var mesh : Mesh = GetComponent(MeshFilter).mesh;
//get existing uvs
var uvs : Vector2[] = new Vector2[4];
//get existing material
var texture : Texture2D = renderer.material.mainTexture;
//use Sprite shader
renderer.material.shader = Shader.Find ( "Sprite" );
//create new uvs from the start and end pixel values
uvs[0] = new Vector2(startPixel.x/texture.width,
(texture.height - endPixel.y)/texture.height);
uvs[1] = new Vector2(startPixel.x/texture.width,
(texture.height - startPixel.y)/texture.height);
uvs[2] = new Vector2(endPixel.x/texture.width,
(texture.height - startPixel.y)/texture.height);
uvs[3] = new Vector2(endPixel.x/texture.width,
(texture.height - endPixel.y)/texture.height);
//reset the mesh uvs
mesh.uv = uvs;
}
Step 4. The coordinates to be used are inverted in the y axis as
specified in Gimp; therefore, the code turns them up the other way
by subtracting all y values from the texture height. If you used pixel
coordinates with (0,0) in the bottom-left corner then you would
just leave out the texture.height part. Attach the new script to
SimplePlane.
Step 5. Select SimplePlane in the Hierarchy and locate the attached script
in the Inspector. Set the startPixel and endPixel values to (0,0) and (40,40),
respectively, as shown in Figure 3.3 .
Step 6. Play. The texture on SimplePlane will now become the little
blue car. Because the code is focusing on a smaller part of a large image
and stretching that part over the same sized surface, the final texture
may look pixilated or too fuzzy. To fix this you will need to rescale the
SimplePlane to suite. This can be done in the Inspector in the transform
component by changing the x and y scale values.
Search Nedrilad ::




Custom Search