PlaneAnimation.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using UnityEngine;
  2. using System.Collections;
  3. public class PlaneAnimation : MonoBehaviour {
  4. public float frameRate = 30f;
  5. public float currentFrame = 0;
  6. public bool loop;
  7. public Texture[] textures;
  8. private float startTime;
  9. private float duration;
  10. private bool endFristTime = false;
  11. public bool removeWhenEnd = false;
  12. // Use this for initialization
  13. void Start () {
  14. startTime = GameTime.time;
  15. duration = textures.Length / frameRate;
  16. }
  17. // Update is called once per frame
  18. void FixedUpdate ()
  19. {
  20. float oldFrame = currentFrame;
  21. float offset = (GameTime.time - startTime) % duration;
  22. currentFrame = Mathf.Floor(textures.Length * offset / duration);
  23. if (loop)
  24. {
  25. GetComponent<Renderer>().material.mainTexture = textures [(int)currentFrame];
  26. }
  27. else
  28. {
  29. if(!endFristTime)
  30. {
  31. if (currentFrame < oldFrame)
  32. {
  33. endFristTime = true;
  34. if(removeWhenEnd)
  35. {
  36. Destroy(this.gameObject);
  37. }
  38. }
  39. else
  40. {
  41. GetComponent<Renderer>().material.mainTexture = textures [(int)currentFrame];
  42. }
  43. }
  44. }
  45. }
  46. }