Game Development Reference
private var nextFrame: float = 0;
private var timeBetweenFrames;
totalWidth = textures.width * textures.length;
//make texture atlas
atlas = new Texture2D(totalWidth, textures.height);
renderer.material.mainTexture = atlas;
renderer.material.color = Color.white;
renderer.material.shader = Shader.Find ( "Sprite" );
for(var i: int = 0; i < textures.length; i++)
for (var y : int = 0;y < textures[i].height; y++)
for (var x : int = 0;x < textures[i].width; x++)
atlas.SetPixel (x +i*textures[i].width,y,
Step 6. Attach AnimatedSprite.js to the plane.
Step 7. With the plane selected in the Hierarchy, locate the
AnimatedSprite script component in the Inspector. Set the Size
value for Textures to 16 and drag and drop all the walk cycle images
onto the elements where Element 0 is walkcycle1, Element 1 is
walkcycle2, and so on.
Step 8. Play. The texture atlas will be created and applied to the
plane. In the game you will be able to see an image on the plane
made up of the 16 walk cycle textures. The effect can be seen
better if you stretch the plane out by modifying its x scale as
shown in Figure 3.5 .
Step 9. Ensure that the plane scale is (1,1,1). Now instead of all 16
images appearing on the plane at once we only want 1. To achieve
this, the texture scale on the plane should be reduced to 1/16th of
its size along the x axis. This can be done in the Inspector where
the tiling and offset values for a material are set. However, because
the atlas is being created dynamically (i.e., as the program runs),
you'll want the code to set it to align with the number of frames
in the walk cycle. To do this, modify AnimatedSprite.js as shown in
Listing 3.4 .