AtlasAsset.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2.1
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software (typically granted by licensing Spine), you
  12. * may not (a) modify, translate, adapt or otherwise create derivative works,
  13. * improvements of the Software or develop new applications using the Software
  14. * or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. * trademark, patent or other intellectual property or proprietary rights
  16. * notices on or in the Software, including any copy thereof. Redistributions
  17. * in binary or source form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. using System.IO;
  32. using UnityEngine;
  33. using Spine;
  34. /// <summary>Loads and stores a Spine atlas and list of materials.</summary>
  35. public class AtlasAsset : ScriptableObject
  36. {
  37. public TextAsset atlasFile;
  38. public Material[] materials;
  39. private Atlas atlas;
  40. public void Reset()
  41. {
  42. atlas = null;
  43. }
  44. /// <returns>The atlas or null if it could not be loaded.</returns>
  45. public Atlas GetAtlas()
  46. {
  47. if (atlasFile == null)
  48. {
  49. Debug.LogError("Atlas file not set for atlas asset: " + name, this);
  50. Reset();
  51. return null;
  52. }
  53. if (materials == null || materials.Length == 0)
  54. {
  55. Debug.LogError("Materials not set for atlas asset: " + name, this);
  56. Reset();
  57. return null;
  58. }
  59. if (atlas != null)
  60. return atlas;
  61. try
  62. {
  63. atlas = new Atlas(new StringReader(atlasFile.text), "", new MaterialsTextureLoader(this));
  64. atlas.FlipV();
  65. return atlas;
  66. }
  67. catch (Exception ex)
  68. {
  69. Debug.LogError("Error reading atlas file for atlas asset: " + name + "\n" + ex.Message + "\n" + ex.StackTrace, this);
  70. return null;
  71. }
  72. }
  73. }
  74. public class MaterialsTextureLoader : TextureLoader
  75. {
  76. AtlasAsset atlasAsset;
  77. public MaterialsTextureLoader(AtlasAsset atlasAsset)
  78. {
  79. this.atlasAsset = atlasAsset;
  80. }
  81. public void Load(AtlasPage page, String path)
  82. {
  83. String name = Path.GetFileNameWithoutExtension(path);
  84. Material material = null;
  85. foreach (Material other in atlasAsset.materials)
  86. {
  87. if (other.mainTexture == null)
  88. {
  89. Debug.LogError("Material is missing texture: " + other.name, other);
  90. return;
  91. }
  92. if (other.mainTexture.name == name)
  93. {
  94. material = other;
  95. break;
  96. }
  97. }
  98. if (material == null)
  99. {
  100. Debug.LogError("Material with texture name \"" + name + "\" not found for atlas asset: " + atlasAsset.name, atlasAsset);
  101. return;
  102. }
  103. page.rendererObject = material;
  104. // Very old atlas files expected the texture's actual size to be used at runtime.
  105. if (page.width == 0 || page.height == 0)
  106. {
  107. page.width = material.mainTexture.width;
  108. page.height = material.mainTexture.height;
  109. }
  110. }
  111. public void Unload(object texture)
  112. {
  113. }
  114. }