AtlasAttachmentLoader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. namespace Spine
  3. {
  4. public class AtlasAttachmentLoader : AttachmentLoader
  5. {
  6. public AtlasAttachmentLoader(params Atlas[] atlasArray)
  7. {
  8. if (atlasArray == null)
  9. {
  10. throw new ArgumentNullException("atlas array cannot be null.");
  11. }
  12. this.atlasArray = atlasArray;
  13. }
  14. public RegionAttachment NewRegionAttachment(Skin skin, string name, string path)
  15. {
  16. AtlasRegion atlasRegion = this.FindRegion(path);
  17. if (atlasRegion == null)
  18. {
  19. throw new Exception(string.Concat(new string[]
  20. {
  21. "Region not found in atlas: ",
  22. path,
  23. " (region attachment: ",
  24. name,
  25. ")"
  26. }));
  27. }
  28. RegionAttachment regionAttachment = new RegionAttachment(name);
  29. regionAttachment.RendererObject = atlasRegion;
  30. regionAttachment.SetUVs(atlasRegion.u, atlasRegion.v, atlasRegion.u2, atlasRegion.v2, atlasRegion.rotate);
  31. regionAttachment.regionOffsetX = atlasRegion.offsetX;
  32. regionAttachment.regionOffsetY = atlasRegion.offsetY;
  33. regionAttachment.regionWidth = (float)atlasRegion.width;
  34. regionAttachment.regionHeight = (float)atlasRegion.height;
  35. regionAttachment.regionOriginalWidth = (float)atlasRegion.originalWidth;
  36. regionAttachment.regionOriginalHeight = (float)atlasRegion.originalHeight;
  37. return regionAttachment;
  38. }
  39. public MeshAttachment NewMeshAttachment(Skin skin, string name, string path)
  40. {
  41. AtlasRegion atlasRegion = this.FindRegion(path);
  42. if (atlasRegion == null)
  43. {
  44. throw new Exception(string.Concat(new string[]
  45. {
  46. "Region not found in atlas: ",
  47. path,
  48. " (mesh attachment: ",
  49. name,
  50. ")"
  51. }));
  52. }
  53. return new MeshAttachment(name)
  54. {
  55. RendererObject = atlasRegion,
  56. RegionU = atlasRegion.u,
  57. RegionV = atlasRegion.v,
  58. RegionU2 = atlasRegion.u2,
  59. RegionV2 = atlasRegion.v2,
  60. RegionRotate = atlasRegion.rotate,
  61. regionOffsetX = atlasRegion.offsetX,
  62. regionOffsetY = atlasRegion.offsetY,
  63. regionWidth = (float)atlasRegion.width,
  64. regionHeight = (float)atlasRegion.height,
  65. regionOriginalWidth = (float)atlasRegion.originalWidth,
  66. regionOriginalHeight = (float)atlasRegion.originalHeight
  67. };
  68. }
  69. public SkinnedMeshAttachment NewSkinnedMeshAttachment(Skin skin, string name, string path)
  70. {
  71. AtlasRegion atlasRegion = this.FindRegion(path);
  72. if (atlasRegion == null)
  73. {
  74. throw new Exception(string.Concat(new string[]
  75. {
  76. "Region not found in atlas: ",
  77. path,
  78. " (skinned mesh attachment: ",
  79. name,
  80. ")"
  81. }));
  82. }
  83. return new SkinnedMeshAttachment(name)
  84. {
  85. RendererObject = atlasRegion,
  86. RegionU = atlasRegion.u,
  87. RegionV = atlasRegion.v,
  88. RegionU2 = atlasRegion.u2,
  89. RegionV2 = atlasRegion.v2,
  90. RegionRotate = atlasRegion.rotate,
  91. regionOffsetX = atlasRegion.offsetX,
  92. regionOffsetY = atlasRegion.offsetY,
  93. regionWidth = (float)atlasRegion.width,
  94. regionHeight = (float)atlasRegion.height,
  95. regionOriginalWidth = (float)atlasRegion.originalWidth,
  96. regionOriginalHeight = (float)atlasRegion.originalHeight
  97. };
  98. }
  99. public BoundingBoxAttachment NewBoundingBoxAttachment(Skin skin, string name)
  100. {
  101. return new BoundingBoxAttachment(name);
  102. }
  103. public AtlasRegion FindRegion(string name)
  104. {
  105. for (int i = 0; i < this.atlasArray.Length; i++)
  106. {
  107. AtlasRegion atlasRegion = this.atlasArray[i].FindRegion(name);
  108. if (atlasRegion != null)
  109. {
  110. return atlasRegion;
  111. }
  112. }
  113. return null;
  114. }
  115. private Atlas[] atlasArray;
  116. }
  117. }