TileAnim.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using System.Collections;
  3. public class TileAnim : MonoBehaviour {
  4. public float width;
  5. public float height;
  6. public float totalWidth;
  7. public float totalHeight;
  8. private int count;
  9. private int cols;
  10. private int rows;
  11. public int loop = 0;
  12. private int currentLoopTime = 0;
  13. public float removeDelay = 0;
  14. private float overTime = -1f;
  15. // Use this for initialization
  16. void Start ()
  17. {
  18. count = 0;
  19. cols = (int)(totalWidth/width);
  20. rows = (int)(totalHeight/height);
  21. }
  22. // Update is called once per frame
  23. void Update ()
  24. {
  25. if(overTime > 0)
  26. {
  27. if(GameTime.time-overTime>removeDelay)
  28. {
  29. Destroy(this.gameObject);
  30. }
  31. return;
  32. }
  33. int col = count%cols;
  34. int row = count/cols;
  35. float offsetX = col*width/totalWidth;
  36. float offsetY = row*height/totalHeight;
  37. GetComponent<Renderer>().material.mainTextureOffset = new Vector2(offsetX, offsetY);
  38. count++;
  39. if(count >= cols*rows)
  40. {
  41. count = 0;
  42. currentLoopTime++;
  43. if(loop >= 0 && currentLoopTime >= loop)
  44. {
  45. if(removeDelay == 0)
  46. {
  47. Destroy(this.gameObject);
  48. }
  49. else
  50. {
  51. this.GetComponent<Renderer>().enabled = false;
  52. overTime = GameTime.time;
  53. }
  54. }
  55. }
  56. }
  57. }