Animated Texture Sequence

From ETC Public Wiki
Jump to: navigation, search

To create an animated texture via an image sequence

Store the images in a Resources folder: Assets\Resources\

See more info: http://unity3d.com/support/documentation/ScriptReference/Resources.html


Create a private array to store the images in code

 private var texSeq = new Texture[#];


Load them in start

 Function Start() {
   for (var i + 0; i < #; i += 1) {
     var name : String = "name_" + i.ToString("0000");  // to pad to four digits -- no file extension
     texSeq[i] = Resources.Load(name);
   }
 }

Play them: There are a few different ways to do this here is the quick and dirty

 private var texPlayRate : float;
 private var texPlayTime : float;
 Function Start() {
   texPlayRate = 0.3;
   texPlayTime = 0.0;
   texIndex = 0;
 }
 Function Update() {
   texPlayTime += 0.1;
   if ( texPlayTime > texPlayRate) {
     go.renderer.material.mainTexture = texSeq[texIndex];
     texPlayTime = 0.0;
     texIndex += 1;
     if ( texIndex == texSeq.Length){
       // stop or loop by setting index to 0
     }
   }
 }