Im trying to apply a simple 2d texture to a cube from my atlas script.
#pragma strict
// Source textures.
var atlasTextures: Texture2D[];
// Rectangles for individual atlas textures.
var rects: Rect[];
function Start () {
// Pack the individual textures into the smallest possible space,
// while leaving a two pixel gap between their edges.
var atlas = new Texture2D(8192, 8192);
rects = atlas.PackTextures(atlasTextures, 2, 8192);
print(atlasTextures[0]);
// atlasTextures[0] = GameObject.Find("Cube1");
var temp = transform.Find("Cube1");
atlasTextures[0] = temp.renderer.material.mainTexture;
}
this is what i got…I think the last 2 lines need to change… they are throwing nullreference exceptions. Any direction would be greatly appreciated. Thanks!
transform.Find only searches in the object’s children - if “Cube1” isn’t a child of the object this script is attached to, temp will be set to null and generate Null Reference Exception. Use GameObject.Find instead:
...
var temp = GameObject.Find("Cube1");
atlasTextures[0] = temp.renderer.material.mainTexture;
}
EDITED: PackTextures merges the textures passed in the array into a single texture and returns an array of Rects that shows the location of each texture in the atlas. In order to use the packed textures, assign the atlas to the mainTexture and define the texture offset and scale according to the corresponding rect. If you want to use the packed version of atlasTextures[2], for instance, do the following: