Advertisement
kasru

Animated Sprites

Jan 17th, 2013
3,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. var uvAnimationTileX = 4;
  5. var uvAnimationTileY = 4;
  6. var framesPerSecond = 10.0;
  7.  
  8. function Update () {
  9.  
  10.     // Calculate index
  11.     var index : int = Time.time * framesPerSecond;
  12.  
  13.     // repeat when exhausting all frames
  14.     index = index % (3 * 1);
  15.  
  16.     // Size of every tile
  17.     var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
  18.  
  19.     // split into horizontal and vertical index
  20.     var uIndex = index % uvAnimationTileX;
  21.         var vIndex = index / uvAnimationTileX;
  22.  
  23.     // build offset
  24.     // v coordinate is the bottom of the image in opengl so we need to invert.
  25.     var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
  26.  
  27.         renderer.material.SetTextureOffset ("_MainTex", offset);
  28.         renderer.material.SetTextureScale ("_MainTex", size);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement