UVAnimation.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace Xft
  5. {
  6. public class UVAnimation
  7. {
  8. public void Reset()
  9. {
  10. this.curFrame = 0;
  11. this.stepDir = 1;
  12. this.numLoops = 0;
  13. }
  14. public void PlayInReverse()
  15. {
  16. this.stepDir = -1;
  17. this.curFrame = this.frames.Length - 1;
  18. }
  19. public bool GetNextFrame(ref Vector2 uv, ref Vector2 dm)
  20. {
  21. if (this.curFrame + this.stepDir >= this.frames.Length || this.curFrame + this.stepDir < 0)
  22. {
  23. if (this.stepDir > 0 && this.loopReverse)
  24. {
  25. this.stepDir = -1;
  26. this.curFrame += this.stepDir;
  27. uv = this.frames[this.curFrame];
  28. dm = this.UVDimensions[this.curFrame];
  29. }
  30. else
  31. {
  32. if (this.numLoops + 1 > this.loopCycles && this.loopCycles != -1)
  33. {
  34. return false;
  35. }
  36. this.numLoops++;
  37. if (this.loopReverse)
  38. {
  39. this.stepDir *= -1;
  40. this.curFrame += this.stepDir;
  41. }
  42. else
  43. {
  44. this.curFrame = 0;
  45. }
  46. uv = this.frames[this.curFrame];
  47. dm = this.UVDimensions[this.curFrame];
  48. }
  49. }
  50. else
  51. {
  52. this.curFrame += this.stepDir;
  53. uv = this.frames[this.curFrame];
  54. dm = this.UVDimensions[this.curFrame];
  55. }
  56. return true;
  57. }
  58. public void BuildFromFile(string path, int index, float uvTime, Texture mainTex)
  59. {
  60. if (!File.Exists(path))
  61. {
  62. UnityEngine.Debug.LogError("wrong ean file path!");
  63. return;
  64. }
  65. FileStream fileStream = new FileStream(path, FileMode.Open);
  66. BinaryReader br = new BinaryReader(fileStream);
  67. EanFile eanFile = new EanFile();
  68. eanFile.Load(br, fileStream);
  69. fileStream.Close();
  70. EanAnimation eanAnimation = eanFile.Anims[index];
  71. this.frames = new Vector2[(int)eanAnimation.TotalCount];
  72. this.UVDimensions = new Vector2[(int)eanAnimation.TotalCount];
  73. int tileCount = (int)eanAnimation.TileCount;
  74. int num = ((int)eanAnimation.TotalCount + tileCount - 1) / tileCount;
  75. int num2 = 0;
  76. int width = mainTex.width;
  77. int height = mainTex.height;
  78. for (int i = 0; i < num; i++)
  79. {
  80. int num3 = 0;
  81. while (num3 < tileCount && num2 < (int)eanAnimation.TotalCount)
  82. {
  83. Vector2 zero = Vector2.zero;
  84. zero.x = (float)eanAnimation.Frames[num2].Width / (float)width;
  85. zero.y = (float)eanAnimation.Frames[num2].Height / (float)height;
  86. this.frames[num2].x = (float)eanAnimation.Frames[num2].X / (float)width;
  87. this.frames[num2].y = 1f - (float)eanAnimation.Frames[num2].Y / (float)height;
  88. this.UVDimensions[num2] = zero;
  89. this.UVDimensions[num2].y = -this.UVDimensions[num2].y;
  90. num2++;
  91. num3++;
  92. }
  93. }
  94. }
  95. public Vector2[] BuildUVAnim(Vector2 start, Vector2 cellSize, int cols, int rows, int totalCells)
  96. {
  97. int num = 0;
  98. this.frames = new Vector2[totalCells];
  99. this.UVDimensions = new Vector2[totalCells];
  100. this.frames[0] = start;
  101. for (int i = 0; i < rows; i++)
  102. {
  103. int num2 = 0;
  104. while (num2 < cols && num < totalCells)
  105. {
  106. this.frames[num].x = start.x + cellSize.x * (float)num2;
  107. this.frames[num].y = start.y + cellSize.y * (float)i;
  108. this.UVDimensions[num] = cellSize;
  109. num++;
  110. num2++;
  111. }
  112. }
  113. return this.frames;
  114. }
  115. public void SetAnim(Vector2[] anim)
  116. {
  117. this.frames = anim;
  118. }
  119. public Vector2[] frames;
  120. public Vector2[] UVDimensions;
  121. public int curFrame;
  122. protected int stepDir = 1;
  123. public int numLoops;
  124. public string name;
  125. public int loopCycles;
  126. public bool loopReverse;
  127. }
  128. }