SkeletonDataAsset.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 System.Collections.Generic;
  33. using UnityEngine;
  34. using Spine;
  35. public class SkeletonDataAsset : ScriptableObject
  36. {
  37. public AtlasAsset[] atlasAssets;
  38. #if SPINE_TK2D
  39. public tk2dSpriteCollectionData spriteCollection;
  40. #endif
  41. public TextAsset skeletonJSON;
  42. public float scale = 1;
  43. public String[] fromAnimation;
  44. public String[] toAnimation;
  45. public float[] duration;
  46. public float defaultMix;
  47. public RuntimeAnimatorController controller;
  48. private SkeletonData skeletonData;
  49. private AnimationStateData stateData;
  50. public void Reset()
  51. {
  52. skeletonData = null;
  53. stateData = null;
  54. }
  55. public SkeletonData GetSkeletonData(bool quiet)
  56. {
  57. if (atlasAssets == null)
  58. {
  59. atlasAssets = new AtlasAsset[0];
  60. if (!quiet)
  61. Debug.LogError("Atlas not set for SkeletonData asset: " + name, this);
  62. Reset();
  63. return null;
  64. }
  65. if (skeletonJSON == null)
  66. {
  67. if (!quiet)
  68. Debug.LogError("Skeleton JSON file not set for SkeletonData asset: " + name, this);
  69. Reset();
  70. return null;
  71. }
  72. #if !SPINE_TK2D
  73. if (atlasAssets.Length == 0)
  74. {
  75. Reset();
  76. return null;
  77. }
  78. #else
  79. if (atlasAssets.Length == 0 && spriteCollection == null) {
  80. Reset();
  81. return null;
  82. }
  83. #endif
  84. Atlas[] atlasArr = new Atlas[atlasAssets.Length];
  85. for (int i = 0; i < atlasAssets.Length; i++)
  86. {
  87. if (atlasAssets[i] == null)
  88. {
  89. Reset();
  90. return null;
  91. }
  92. atlasArr[i] = atlasAssets[i].GetAtlas();
  93. if (atlasArr[i] == null)
  94. {
  95. Reset();
  96. return null;
  97. }
  98. }
  99. if (skeletonData != null)
  100. return skeletonData;
  101. AttachmentLoader attachmentLoader;
  102. float skeletonDataScale;
  103. #if !SPINE_TK2D
  104. attachmentLoader = new AtlasAttachmentLoader(atlasArr);
  105. skeletonDataScale = scale;
  106. #else
  107. if (spriteCollection != null) {
  108. attachmentLoader = new SpriteCollectionAttachmentLoader(spriteCollection)
  109. skeletonDataScale = (1.0f / (spriteCollection.invOrthoSize * spriteCollection.halfTargetHeight) * scale) * 100f;
  110. } else {
  111. if (atlasArr.Length == 0) {
  112. Reset();
  113. if (!quiet) Debug.LogError("Atlas not set for SkeletonData asset: " + name, this);
  114. return null;
  115. }
  116. attachmentLoader = new AtlasAttachmentLoader(atlasArr);
  117. skeletonDataScale = scale;
  118. }
  119. #endif
  120. try
  121. {
  122. //var stopwatch = new System.Diagnostics.Stopwatch();
  123. if (skeletonJSON.name.ToLower().Contains(".skel"))
  124. {
  125. var input = new MemoryStream(skeletonJSON.bytes);
  126. var binary = new SkeletonBinary(attachmentLoader);
  127. binary.Scale = skeletonDataScale;
  128. //stopwatch.Start();
  129. skeletonData = binary.ReadSkeletonData(input);
  130. }
  131. else
  132. {
  133. var input = new StringReader(skeletonJSON.text);
  134. var json = new SkeletonJson(attachmentLoader);
  135. json.Scale = skeletonDataScale;
  136. //stopwatch.Start();
  137. skeletonData = json.ReadSkeletonData(input);
  138. }
  139. //stopwatch.Stop();
  140. //Debug.Log(stopwatch.Elapsed);
  141. }
  142. catch (Exception ex)
  143. {
  144. if (!quiet)
  145. Debug.LogError("Error reading skeleton JSON file for SkeletonData asset: " + name + "\n" + ex.Message + "\n" + ex.StackTrace, this);
  146. return null;
  147. }
  148. stateData = new AnimationStateData(skeletonData);
  149. FillStateData();
  150. return skeletonData;
  151. }
  152. public void FillStateData()
  153. {
  154. if (stateData == null)
  155. return;
  156. stateData.DefaultMix = defaultMix;
  157. for (int i = 0, n = fromAnimation.Length; i < n; i++)
  158. {
  159. if (fromAnimation[i].Length == 0 || toAnimation[i].Length == 0)
  160. continue;
  161. stateData.SetMix(fromAnimation[i], toAnimation[i], duration[i]);
  162. }
  163. }
  164. public AnimationStateData GetAnimationStateData()
  165. {
  166. if (stateData != null)
  167. return stateData;
  168. GetSkeletonData(false);
  169. return stateData;
  170. }
  171. }