SpineAttributes.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using UnityEngine;
  30. using System;
  31. using System.Collections;
  32. namespace Spine.Unity {
  33. [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
  34. public abstract class SpineAttributeBase : PropertyAttribute {
  35. public string dataField = "";
  36. public string startsWith = "";
  37. public bool includeNone = true;
  38. public bool fallbackToTextField = false;
  39. }
  40. public class SpineBone : SpineAttributeBase {
  41. /// <summary>
  42. /// Smart popup menu for Spine Bones
  43. /// </summary>
  44. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  45. /// <param name="includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  46. /// <param name = "fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  47. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  48. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  49. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  50. /// </param>
  51. public SpineBone (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  52. this.startsWith = startsWith;
  53. this.dataField = dataField;
  54. this.includeNone = includeNone;
  55. this.fallbackToTextField = fallbackToTextField;
  56. }
  57. public static Spine.Bone GetBone (string boneName, SkeletonRenderer renderer) {
  58. return renderer.skeleton == null ? null : renderer.skeleton.FindBone(boneName);
  59. }
  60. public static Spine.BoneData GetBoneData (string boneName, SkeletonDataAsset skeletonDataAsset) {
  61. var data = skeletonDataAsset.GetSkeletonData(true);
  62. return data.FindBone(boneName);
  63. }
  64. }
  65. public class SpineSlot : SpineAttributeBase {
  66. public bool containsBoundingBoxes = false;
  67. /// <summary>
  68. /// Smart popup menu for Spine Slots
  69. /// </summary>
  70. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  71. /// <param name="containsBoundingBoxes">Disables popup results that don't contain bounding box attachments when true.</param>
  72. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  73. /// <param name = "fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  74. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  75. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  76. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  77. /// </param>
  78. public SpineSlot (string startsWith = "", string dataField = "", bool containsBoundingBoxes = false, bool includeNone = true, bool fallbackToTextField = false) {
  79. this.startsWith = startsWith;
  80. this.dataField = dataField;
  81. this.containsBoundingBoxes = containsBoundingBoxes;
  82. this.includeNone = includeNone;
  83. this.fallbackToTextField = fallbackToTextField;
  84. }
  85. }
  86. public class SpineAnimation : SpineAttributeBase {
  87. /// <summary>
  88. /// Smart popup menu for Spine Animations
  89. /// </summary>
  90. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  91. /// <param name = "fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  92. /// <param name="includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  93. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  94. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  95. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  96. /// </param>
  97. public SpineAnimation (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  98. this.startsWith = startsWith;
  99. this.dataField = dataField;
  100. this.includeNone = includeNone;
  101. this.fallbackToTextField = fallbackToTextField;
  102. }
  103. }
  104. public class SpineEvent : SpineAttributeBase {
  105. /// <summary>
  106. /// Smart popup menu for Spine Events (Spine.EventData)
  107. /// </summary>
  108. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  109. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  110. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  111. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  112. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent(SkeletonRenderer)() will be called as a fallback.
  113. /// </param>
  114. /// <param name="fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  115. public bool audioOnly = false;
  116. public SpineEvent (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false, bool audioOnly = false) {
  117. this.startsWith = startsWith;
  118. this.dataField = dataField;
  119. this.includeNone = includeNone;
  120. this.fallbackToTextField = fallbackToTextField;
  121. this.audioOnly = audioOnly;
  122. }
  123. }
  124. public class SpineIkConstraint : SpineAttributeBase {
  125. /// <summary>
  126. /// Smart popup menu for Spine IK Constraints (Spine.IkConstraint)
  127. /// </summary>
  128. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  129. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  130. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  131. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  132. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent(SkeletonRenderer)() will be called as a fallback.
  133. /// </param>
  134. /// <param name = "fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  135. public SpineIkConstraint (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  136. this.startsWith = startsWith;
  137. this.dataField = dataField;
  138. this.includeNone = includeNone;
  139. this.fallbackToTextField = fallbackToTextField;
  140. }
  141. }
  142. public class SpineTransformConstraint : SpineAttributeBase {
  143. /// <summary>
  144. /// Smart popup menu for Spine Transform Constraints (Spine.TransformConstraint)
  145. /// </summary>
  146. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  147. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  148. /// <param name = "fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  149. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  150. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  151. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  152. /// </param>
  153. public SpineTransformConstraint (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  154. this.startsWith = startsWith;
  155. this.dataField = dataField;
  156. this.includeNone = includeNone;
  157. this.fallbackToTextField = fallbackToTextField;
  158. }
  159. }
  160. public class SpinePathConstraint : SpineAttributeBase {
  161. /// <summary>
  162. /// Smart popup menu for Spine Events (Spine.PathConstraint)
  163. /// </summary>
  164. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  165. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  166. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  167. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  168. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent(SkeletonRenderer)() will be called as a fallback.
  169. /// </param>
  170. public SpinePathConstraint (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  171. this.startsWith = startsWith;
  172. this.dataField = dataField;
  173. this.includeNone = includeNone;
  174. this.fallbackToTextField = fallbackToTextField;
  175. }
  176. }
  177. public class SpineSkin : SpineAttributeBase {
  178. /// <summary>
  179. /// Smart popup menu for Spine Skins
  180. /// </summary>
  181. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  182. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  183. /// <param name = "fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  184. /// <param name = "defaultAsEmptyString">If true, the default choice will be serialized as an empty string.</param>
  185. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  186. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  187. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  188. /// </param>
  189. public bool defaultAsEmptyString = false;
  190. public SpineSkin (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false, bool defaultAsEmptyString = false) {
  191. this.startsWith = startsWith;
  192. this.dataField = dataField;
  193. this.includeNone = includeNone;
  194. this.fallbackToTextField = fallbackToTextField;
  195. this.defaultAsEmptyString = defaultAsEmptyString;
  196. }
  197. }
  198. public class SpineAttachment : SpineAttributeBase {
  199. public bool returnAttachmentPath = false;
  200. public bool currentSkinOnly = false;
  201. public bool placeholdersOnly = false;
  202. public string skinField = "";
  203. public string slotField = "";
  204. /// <summary>
  205. /// Smart popup menu for Spine Attachments
  206. /// </summary>
  207. /// <param name="currentSkinOnly">Filters popup results to only include the current Skin. Only valid when a SkeletonRenderer is the data source.</param>
  208. /// <param name="returnAttachmentPath">Returns a fully qualified path for an Attachment in the format "Skin/Slot/AttachmentName". This path format is only used by the SpineAttachment helper methods like SpineAttachment.GetAttachment and .GetHierarchy. Do not use full path anywhere else in Spine's system.</param>
  209. /// <param name="placeholdersOnly">Filters popup results to exclude attachments that are not children of Skin Placeholders</param>
  210. /// <param name="slotField">If specified, a locally scoped field with the name supplied by in slotField will be used to limit the popup results to children of a named slot</param>
  211. /// <param name="skinField">If specified, a locally scoped field with the name supplied by in skinField will be used to limit the popup results to entries of the named skin</param>
  212. /// <param name="includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  213. /// <param name = "fallbackToTextField">If true, and an animation list source can't be found, the field will fall back to a normal text field. If false, it will show an error.</param>
  214. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  215. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  216. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  217. /// </param>
  218. public SpineAttachment (bool currentSkinOnly = true, bool returnAttachmentPath = false, bool placeholdersOnly = false, string slotField = "", string dataField = "", string skinField = "", bool includeNone = true, bool fallbackToTextField = false) {
  219. this.currentSkinOnly = currentSkinOnly;
  220. this.returnAttachmentPath = returnAttachmentPath;
  221. this.placeholdersOnly = placeholdersOnly;
  222. this.slotField = slotField;
  223. this.dataField = dataField;
  224. this.skinField = skinField;
  225. this.includeNone = includeNone;
  226. this.fallbackToTextField = fallbackToTextField;
  227. }
  228. public static SpineAttachment.Hierarchy GetHierarchy (string fullPath) {
  229. return new SpineAttachment.Hierarchy(fullPath);
  230. }
  231. public static Spine.Attachment GetAttachment (string attachmentPath, Spine.SkeletonData skeletonData) {
  232. var hierarchy = SpineAttachment.GetHierarchy(attachmentPath);
  233. return string.IsNullOrEmpty(hierarchy.name) ? null : skeletonData.FindSkin(hierarchy.skin).GetAttachment(skeletonData.FindSlotIndex(hierarchy.slot), hierarchy.name);
  234. }
  235. public static Spine.Attachment GetAttachment (string attachmentPath, SkeletonDataAsset skeletonDataAsset) {
  236. return GetAttachment(attachmentPath, skeletonDataAsset.GetSkeletonData(true));
  237. }
  238. /// <summary>
  239. /// A struct that represents 3 strings that help identify and locate an attachment in a skeleton.</summary>
  240. public struct Hierarchy {
  241. public string skin;
  242. public string slot;
  243. public string name;
  244. public Hierarchy (string fullPath) {
  245. string[] chunks = fullPath.Split(new char[]{'/'}, System.StringSplitOptions.RemoveEmptyEntries);
  246. if (chunks.Length == 0) {
  247. skin = "";
  248. slot = "";
  249. name = "";
  250. return;
  251. }
  252. else if (chunks.Length < 2) {
  253. throw new System.Exception("Cannot generate Attachment Hierarchy from string! Not enough components! [" + fullPath + "]");
  254. }
  255. skin = chunks[0];
  256. slot = chunks[1];
  257. name = "";
  258. for (int i = 2; i < chunks.Length; i++) {
  259. name += chunks[i];
  260. }
  261. }
  262. }
  263. }
  264. public class SpineAtlasRegion : PropertyAttribute {
  265. public string atlasAssetField;
  266. public SpineAtlasRegion (string atlasAssetField = "") {
  267. this.atlasAssetField = atlasAssetField;
  268. }
  269. }
  270. }