SpineAttributes.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. using System.Collections;
  31. using UnityEngine;
  32. namespace Spine.Unity {
  33. [AttributeUsage(AttributeTargets.Property | 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. public bool avoidGenericMenu = false;
  40. }
  41. public class SpineBone : SpineAttributeBase {
  42. /// <summary>
  43. /// Smart popup menu for Spine Bones
  44. /// </summary>
  45. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  46. /// <param name="includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  47. /// <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>
  48. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  49. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  50. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  51. /// </param>
  52. public SpineBone (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  53. this.startsWith = startsWith;
  54. this.dataField = dataField;
  55. this.includeNone = includeNone;
  56. this.fallbackToTextField = fallbackToTextField;
  57. }
  58. public static Spine.Bone GetBone (string boneName, SkeletonRenderer renderer) {
  59. return renderer.skeleton == null ? null : renderer.skeleton.FindBone(boneName);
  60. }
  61. public static Spine.BoneData GetBoneData (string boneName, SkeletonDataAsset skeletonDataAsset) {
  62. SkeletonData data = skeletonDataAsset.GetSkeletonData(true);
  63. return data.FindBone(boneName);
  64. }
  65. }
  66. public class SpineSlot : SpineAttributeBase {
  67. public bool containsBoundingBoxes = false;
  68. /// <summary>
  69. /// Smart popup menu for Spine Slots
  70. /// </summary>
  71. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  72. /// <param name="containsBoundingBoxes">Disables popup results that don't contain bounding box attachments when true.</param>
  73. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  74. /// <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>
  75. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  76. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  77. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  78. /// </param>
  79. public SpineSlot (string startsWith = "", string dataField = "", bool containsBoundingBoxes = false, bool includeNone = true, bool fallbackToTextField = false) {
  80. this.startsWith = startsWith;
  81. this.dataField = dataField;
  82. this.containsBoundingBoxes = containsBoundingBoxes;
  83. this.includeNone = includeNone;
  84. this.fallbackToTextField = fallbackToTextField;
  85. }
  86. }
  87. public class SpineAnimation : SpineAttributeBase {
  88. /// <summary>
  89. /// Smart popup menu for Spine Animations
  90. /// </summary>
  91. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  92. /// <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>
  93. /// <param name="includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  94. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  95. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  96. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  97. /// </param>
  98. public SpineAnimation (string startsWith = "", string dataField = "",
  99. bool includeNone = true, bool fallbackToTextField = false, bool avoidGenericMenu = false) {
  100. this.startsWith = startsWith;
  101. this.dataField = dataField;
  102. this.includeNone = includeNone;
  103. this.fallbackToTextField = fallbackToTextField;
  104. this.avoidGenericMenu = avoidGenericMenu;
  105. }
  106. }
  107. public class SpineEvent : SpineAttributeBase {
  108. /// <summary>
  109. /// Smart popup menu for Spine Events (Spine.EventData)
  110. /// </summary>
  111. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  112. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  113. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  114. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  115. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent(SkeletonRenderer)() will be called as a fallback.
  116. /// </param>
  117. /// <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>
  118. public bool audioOnly = false;
  119. public SpineEvent (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false, bool audioOnly = false) {
  120. this.startsWith = startsWith;
  121. this.dataField = dataField;
  122. this.includeNone = includeNone;
  123. this.fallbackToTextField = fallbackToTextField;
  124. this.audioOnly = audioOnly;
  125. }
  126. }
  127. public class SpineIkConstraint : SpineAttributeBase {
  128. /// <summary>
  129. /// Smart popup menu for Spine IK Constraints (Spine.IkConstraint)
  130. /// </summary>
  131. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  132. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  133. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  134. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  135. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent(SkeletonRenderer)() will be called as a fallback.
  136. /// </param>
  137. /// <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>
  138. public SpineIkConstraint (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  139. this.startsWith = startsWith;
  140. this.dataField = dataField;
  141. this.includeNone = includeNone;
  142. this.fallbackToTextField = fallbackToTextField;
  143. }
  144. }
  145. public class SpineTransformConstraint : SpineAttributeBase {
  146. /// <summary>
  147. /// Smart popup menu for Spine Transform Constraints (Spine.TransformConstraint)
  148. /// </summary>
  149. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  150. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  151. /// <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>
  152. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  153. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  154. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  155. /// </param>
  156. public SpineTransformConstraint (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  157. this.startsWith = startsWith;
  158. this.dataField = dataField;
  159. this.includeNone = includeNone;
  160. this.fallbackToTextField = fallbackToTextField;
  161. }
  162. }
  163. public class SpinePathConstraint : SpineAttributeBase {
  164. /// <summary>
  165. /// Smart popup menu for Spine Events (Spine.PathConstraint)
  166. /// </summary>
  167. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  168. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  169. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  170. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives).
  171. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent(SkeletonRenderer)() will be called as a fallback.
  172. /// </param>
  173. public SpinePathConstraint (string startsWith = "", string dataField = "", bool includeNone = true, bool fallbackToTextField = false) {
  174. this.startsWith = startsWith;
  175. this.dataField = dataField;
  176. this.includeNone = includeNone;
  177. this.fallbackToTextField = fallbackToTextField;
  178. }
  179. }
  180. public class SpineSkin : SpineAttributeBase {
  181. /// <summary>
  182. /// Smart popup menu for Spine Skins
  183. /// </summary>
  184. /// <param name="startsWith">Filters popup results to elements that begin with supplied string.</param>
  185. /// <param name = "includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  186. /// <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>
  187. /// <param name = "defaultAsEmptyString">If true, the default choice will be serialized as an empty string.</param>
  188. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  189. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  190. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  191. /// </param>
  192. public bool defaultAsEmptyString = false;
  193. public SpineSkin (string startsWith = "", string dataField = "", bool includeNone = false,
  194. bool fallbackToTextField = false, bool defaultAsEmptyString = false, bool avoidGenericMenu = false) {
  195. this.startsWith = startsWith;
  196. this.dataField = dataField;
  197. this.includeNone = includeNone;
  198. this.fallbackToTextField = fallbackToTextField;
  199. this.defaultAsEmptyString = defaultAsEmptyString;
  200. this.avoidGenericMenu = avoidGenericMenu;
  201. }
  202. }
  203. public class SpineAttachment : SpineAttributeBase {
  204. public bool returnAttachmentPath = false;
  205. public bool currentSkinOnly = false;
  206. public bool placeholdersOnly = false;
  207. public string skinField = "";
  208. public string slotField = "";
  209. /// <summary>
  210. /// Smart popup menu for Spine Attachments
  211. /// </summary>
  212. /// <param name="currentSkinOnly">Filters popup results to only include the current Skin. Only valid when a SkeletonRenderer is the data source.</param>
  213. /// <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>
  214. /// <param name="placeholdersOnly">Filters popup results to exclude attachments that are not children of Skin Placeholders</param>
  215. /// <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>
  216. /// <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>
  217. /// <param name="includeNone">If true, the dropdown list will include a "none" option which stored as an empty string.</param>
  218. /// <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>
  219. /// <param name="dataField">If specified, a locally scoped field with the name supplied by in dataField will be used to fill the popup results.
  220. /// Valid types are SkeletonDataAsset and SkeletonRenderer (and derivatives)
  221. /// If left empty and the script the attribute is applied to is derived from Component, GetComponent<SkeletonRenderer>() will be called as a fallback.
  222. /// </param>
  223. public SpineAttachment (bool currentSkinOnly = true, bool returnAttachmentPath = false, bool placeholdersOnly = false, string slotField = "", string dataField = "", string skinField = "", bool includeNone = true, bool fallbackToTextField = false) {
  224. this.currentSkinOnly = currentSkinOnly;
  225. this.returnAttachmentPath = returnAttachmentPath;
  226. this.placeholdersOnly = placeholdersOnly;
  227. this.slotField = slotField;
  228. this.dataField = dataField;
  229. this.skinField = skinField;
  230. this.includeNone = includeNone;
  231. this.fallbackToTextField = fallbackToTextField;
  232. }
  233. public static SpineAttachment.Hierarchy GetHierarchy (string fullPath) {
  234. return new SpineAttachment.Hierarchy(fullPath);
  235. }
  236. public static Spine.Attachment GetAttachment (string attachmentPath, Spine.SkeletonData skeletonData) {
  237. SpineAttachment.Hierarchy hierarchy = SpineAttachment.GetHierarchy(attachmentPath);
  238. if (string.IsNullOrEmpty(hierarchy.name)) return null;
  239. SlotData slot = skeletonData.FindSlot(hierarchy.slot);
  240. if (slot == null) return null;
  241. return skeletonData.FindSkin(hierarchy.skin).GetAttachment(slot.Index, hierarchy.name);
  242. }
  243. public static Spine.Attachment GetAttachment (string attachmentPath, SkeletonDataAsset skeletonDataAsset) {
  244. return GetAttachment(attachmentPath, skeletonDataAsset.GetSkeletonData(true));
  245. }
  246. /// <summary>
  247. /// A struct that represents 3 strings that help identify and locate an attachment in a skeleton.</summary>
  248. public struct Hierarchy {
  249. public string skin;
  250. public string slot;
  251. public string name;
  252. public Hierarchy (string fullPath) {
  253. string[] chunks = fullPath.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
  254. if (chunks.Length == 0) {
  255. skin = "";
  256. slot = "";
  257. name = "";
  258. return;
  259. } else if (chunks.Length < 2) {
  260. throw new System.Exception("Cannot generate Attachment Hierarchy from string! Not enough components! [" + fullPath + "]");
  261. }
  262. skin = chunks[0];
  263. slot = chunks[1];
  264. name = "";
  265. for (int i = 2; i < chunks.Length; i++) {
  266. name += chunks[i];
  267. }
  268. }
  269. }
  270. }
  271. public class SpineAtlasRegion : PropertyAttribute {
  272. public string atlasAssetField;
  273. public SpineAtlasRegion (string atlasAssetField = "") {
  274. this.atlasAssetField = atlasAssetField;
  275. }
  276. }
  277. }