SkeletonUtility.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. #if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
  30. #define NEW_PREFAB_SYSTEM
  31. #endif
  32. using UnityEngine;
  33. using System.Collections.Generic;
  34. namespace Spine.Unity {
  35. #if NEW_PREFAB_SYSTEM
  36. [ExecuteAlways]
  37. #else
  38. [ExecuteInEditMode]
  39. #endif
  40. [RequireComponent(typeof(ISkeletonAnimation))]
  41. public sealed class SkeletonUtility : MonoBehaviour {
  42. #region BoundingBoxAttachment
  43. public static PolygonCollider2D AddBoundingBoxGameObject (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) {
  44. Skin skin = string.IsNullOrEmpty(skinName) ? skeleton.data.defaultSkin : skeleton.data.FindSkin(skinName);
  45. if (skin == null) {
  46. Debug.LogError("Skin " + skinName + " not found!");
  47. return null;
  48. }
  49. var attachment = skin.GetAttachment(skeleton.FindSlotIndex(slotName), attachmentName);
  50. if (attachment == null) {
  51. Debug.LogFormat("Attachment in slot '{0}' named '{1}' not found in skin '{2}'.", slotName, attachmentName, skin.name);
  52. return null;
  53. }
  54. var box = attachment as BoundingBoxAttachment;
  55. if (box != null) {
  56. var slot = skeleton.FindSlot(slotName);
  57. return AddBoundingBoxGameObject(box.Name, box, slot, parent, isTrigger);
  58. } else {
  59. Debug.LogFormat("Attachment '{0}' was not a Bounding Box.", attachmentName);
  60. return null;
  61. }
  62. }
  63. public static PolygonCollider2D AddBoundingBoxGameObject (string name, BoundingBoxAttachment box, Slot slot, Transform parent, bool isTrigger = true) {
  64. var go = new GameObject("[BoundingBox]" + (string.IsNullOrEmpty(name) ? box.Name : name));
  65. #if UNITY_EDITOR
  66. if (!Application.isPlaying)
  67. UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Spawn BoundingBox");
  68. # endif
  69. var got = go.transform;
  70. got.parent = parent;
  71. got.localPosition = Vector3.zero;
  72. got.localRotation = Quaternion.identity;
  73. got.localScale = Vector3.one;
  74. return AddBoundingBoxAsComponent(box, slot, go, isTrigger);
  75. }
  76. public static PolygonCollider2D AddBoundingBoxAsComponent (BoundingBoxAttachment box, Slot slot, GameObject gameObject, bool isTrigger = true) {
  77. if (box == null) return null;
  78. var collider = gameObject.AddComponent<PolygonCollider2D>();
  79. collider.isTrigger = isTrigger;
  80. SetColliderPointsLocal(collider, slot, box);
  81. return collider;
  82. }
  83. public static void SetColliderPointsLocal (PolygonCollider2D collider, Slot slot, BoundingBoxAttachment box) {
  84. if (box == null) return;
  85. if (box.IsWeighted()) Debug.LogWarning("UnityEngine.PolygonCollider2D does not support weighted or animated points. Collider points will not be animated and may have incorrect orientation. If you want to use it as a collider, please remove weights and animations from the bounding box in Spine editor.");
  86. var verts = box.GetLocalVertices(slot, null);
  87. collider.SetPath(0, verts);
  88. }
  89. public static Bounds GetBoundingBoxBounds (BoundingBoxAttachment boundingBox, float depth = 0) {
  90. float[] floats = boundingBox.Vertices;
  91. int floatCount = floats.Length;
  92. Bounds bounds = new Bounds();
  93. bounds.center = new Vector3(floats[0], floats[1], 0);
  94. for (int i = 2; i < floatCount; i += 2)
  95. bounds.Encapsulate(new Vector3(floats[i], floats[i + 1], 0));
  96. Vector3 size = bounds.size;
  97. size.z = depth;
  98. bounds.size = size;
  99. return bounds;
  100. }
  101. public static Rigidbody2D AddBoneRigidbody2D (GameObject gameObject, bool isKinematic = true, float gravityScale = 0f) {
  102. var rb = gameObject.GetComponent<Rigidbody2D>();
  103. if (rb == null) {
  104. rb = gameObject.AddComponent<Rigidbody2D>();
  105. rb.isKinematic = isKinematic;
  106. rb.gravityScale = gravityScale;
  107. }
  108. return rb;
  109. }
  110. #endregion
  111. public delegate void SkeletonUtilityDelegate ();
  112. public event SkeletonUtilityDelegate OnReset;
  113. public Transform boneRoot;
  114. /// <summary>
  115. /// If true, <see cref="Skeleton.ScaleX"/> and <see cref="Skeleton.ScaleY"/> are followed
  116. /// by 180 degree rotation. If false, negative Transform scale is used.
  117. /// Note that using negative scale is consistent with previous behaviour (hence the default),
  118. /// however causes serious problems with rigidbodies and physics. Therefore, it is recommended to
  119. /// enable this parameter where possible. When creating hinge chains for a chain of skeleton bones
  120. /// via <see cref="SkeletonUtilityBone"/>, it is mandatory to have <c>flipBy180DegreeRotation</c> enabled.
  121. /// </summary>
  122. public bool flipBy180DegreeRotation = false;
  123. void Update () {
  124. var skeleton = skeletonComponent.Skeleton;
  125. if (skeleton != null && boneRoot != null) {
  126. if (flipBy180DegreeRotation) {
  127. boneRoot.localScale = new Vector3(Mathf.Abs(skeleton.ScaleX), Mathf.Abs(skeleton.ScaleY), 1f);
  128. boneRoot.eulerAngles = new Vector3(skeleton.ScaleY > 0 ? 0 : 180,
  129. skeleton.ScaleX > 0 ? 0 : 180,
  130. 0);
  131. }
  132. else {
  133. boneRoot.localScale = new Vector3(skeleton.ScaleX, skeleton.ScaleY, 1f);
  134. }
  135. }
  136. if (canvas != null) {
  137. positionScale = canvas.referencePixelsPerUnit;
  138. }
  139. }
  140. [HideInInspector] public SkeletonRenderer skeletonRenderer;
  141. [HideInInspector] public SkeletonGraphic skeletonGraphic;
  142. private Canvas canvas;
  143. [System.NonSerialized] public ISkeletonAnimation skeletonAnimation;
  144. private ISkeletonComponent skeletonComponent;
  145. [System.NonSerialized] public List<SkeletonUtilityBone> boneComponents = new List<SkeletonUtilityBone>();
  146. [System.NonSerialized] public List<SkeletonUtilityConstraint> constraintComponents = new List<SkeletonUtilityConstraint>();
  147. public ISkeletonComponent SkeletonComponent {
  148. get {
  149. if (skeletonComponent == null) {
  150. skeletonComponent = skeletonRenderer != null ? skeletonRenderer.GetComponent<ISkeletonComponent>() :
  151. skeletonGraphic != null ? skeletonGraphic.GetComponent<ISkeletonComponent>() :
  152. GetComponent<ISkeletonComponent>();
  153. }
  154. return skeletonComponent;
  155. }
  156. }
  157. public Skeleton Skeleton {
  158. get {
  159. if (SkeletonComponent == null)
  160. return null;
  161. return skeletonComponent.Skeleton;
  162. }
  163. }
  164. public bool IsValid {
  165. get {
  166. return (skeletonRenderer != null && skeletonRenderer.valid) ||
  167. (skeletonGraphic != null && skeletonGraphic.IsValid);
  168. }
  169. }
  170. public float PositionScale { get { return positionScale; } }
  171. float positionScale = 1.0f;
  172. bool hasOverrideBones;
  173. bool hasConstraints;
  174. bool needToReprocessBones;
  175. public void ResubscribeEvents () {
  176. OnDisable();
  177. OnEnable();
  178. }
  179. void OnEnable () {
  180. if (skeletonRenderer == null) {
  181. skeletonRenderer = GetComponent<SkeletonRenderer>();
  182. }
  183. if (skeletonGraphic == null) {
  184. skeletonGraphic = GetComponent<SkeletonGraphic>();
  185. }
  186. if (skeletonAnimation == null) {
  187. skeletonAnimation = skeletonRenderer != null ? skeletonRenderer.GetComponent<ISkeletonAnimation>() :
  188. skeletonGraphic != null ? skeletonGraphic.GetComponent<ISkeletonAnimation>() :
  189. GetComponent<ISkeletonAnimation>();
  190. }
  191. if (skeletonComponent == null) {
  192. skeletonComponent = skeletonRenderer != null ? skeletonRenderer.GetComponent<ISkeletonComponent>() :
  193. skeletonGraphic != null ? skeletonGraphic.GetComponent<ISkeletonComponent>() :
  194. GetComponent<ISkeletonComponent>();
  195. }
  196. if (skeletonRenderer != null) {
  197. skeletonRenderer.OnRebuild -= HandleRendererReset;
  198. skeletonRenderer.OnRebuild += HandleRendererReset;
  199. }
  200. else if (skeletonGraphic != null) {
  201. skeletonGraphic.OnRebuild -= HandleRendererReset;
  202. skeletonGraphic.OnRebuild += HandleRendererReset;
  203. canvas = skeletonGraphic.canvas;
  204. if (canvas == null)
  205. canvas = skeletonGraphic.GetComponentInParent<Canvas>();
  206. if (canvas == null)
  207. positionScale = 100.0f;
  208. }
  209. if (skeletonAnimation != null) {
  210. skeletonAnimation.UpdateLocal -= UpdateLocal;
  211. skeletonAnimation.UpdateLocal += UpdateLocal;
  212. }
  213. CollectBones();
  214. }
  215. void Start () {
  216. //recollect because order of operations failure when switching between game mode and edit mode...
  217. CollectBones();
  218. }
  219. void OnDisable () {
  220. if (skeletonRenderer != null)
  221. skeletonRenderer.OnRebuild -= HandleRendererReset;
  222. if (skeletonGraphic != null)
  223. skeletonGraphic.OnRebuild -= HandleRendererReset;
  224. if (skeletonAnimation != null) {
  225. skeletonAnimation.UpdateLocal -= UpdateLocal;
  226. skeletonAnimation.UpdateWorld -= UpdateWorld;
  227. skeletonAnimation.UpdateComplete -= UpdateComplete;
  228. }
  229. }
  230. void HandleRendererReset (SkeletonRenderer r) {
  231. if (OnReset != null) OnReset();
  232. CollectBones();
  233. }
  234. void HandleRendererReset (SkeletonGraphic g) {
  235. if (OnReset != null) OnReset();
  236. CollectBones();
  237. }
  238. public void RegisterBone (SkeletonUtilityBone bone) {
  239. if (boneComponents.Contains(bone)) {
  240. return;
  241. } else {
  242. boneComponents.Add(bone);
  243. needToReprocessBones = true;
  244. }
  245. }
  246. public void UnregisterBone (SkeletonUtilityBone bone) {
  247. boneComponents.Remove(bone);
  248. }
  249. public void RegisterConstraint (SkeletonUtilityConstraint constraint) {
  250. if (constraintComponents.Contains(constraint))
  251. return;
  252. else {
  253. constraintComponents.Add(constraint);
  254. needToReprocessBones = true;
  255. }
  256. }
  257. public void UnregisterConstraint (SkeletonUtilityConstraint constraint) {
  258. constraintComponents.Remove(constraint);
  259. }
  260. public void CollectBones () {
  261. var skeleton = skeletonComponent.Skeleton;
  262. if (skeleton == null) return;
  263. if (boneRoot != null) {
  264. var constraintTargets = new List<System.Object>();
  265. var ikConstraints = skeleton.IkConstraints;
  266. for (int i = 0, n = ikConstraints.Count; i < n; i++)
  267. constraintTargets.Add(ikConstraints.Items[i].target);
  268. var transformConstraints = skeleton.TransformConstraints;
  269. for (int i = 0, n = transformConstraints.Count; i < n; i++)
  270. constraintTargets.Add(transformConstraints.Items[i].target);
  271. var boneComponents = this.boneComponents;
  272. for (int i = 0, n = boneComponents.Count; i < n; i++) {
  273. var b = boneComponents[i];
  274. if (b.bone == null) {
  275. b.DoUpdate(SkeletonUtilityBone.UpdatePhase.Local);
  276. if (b.bone == null) continue;
  277. }
  278. hasOverrideBones |= (b.mode == SkeletonUtilityBone.Mode.Override);
  279. hasConstraints |= constraintTargets.Contains(b.bone);
  280. }
  281. hasConstraints |= constraintComponents.Count > 0;
  282. if (skeletonAnimation != null) {
  283. skeletonAnimation.UpdateWorld -= UpdateWorld;
  284. skeletonAnimation.UpdateComplete -= UpdateComplete;
  285. if (hasOverrideBones || hasConstraints)
  286. skeletonAnimation.UpdateWorld += UpdateWorld;
  287. if (hasConstraints)
  288. skeletonAnimation.UpdateComplete += UpdateComplete;
  289. }
  290. needToReprocessBones = false;
  291. } else {
  292. boneComponents.Clear();
  293. constraintComponents.Clear();
  294. }
  295. }
  296. void UpdateLocal (ISkeletonAnimation anim) {
  297. if (needToReprocessBones)
  298. CollectBones();
  299. var boneComponents = this.boneComponents;
  300. if (boneComponents == null) return;
  301. for (int i = 0, n = boneComponents.Count; i < n; i++)
  302. boneComponents[i].transformLerpComplete = false;
  303. UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Local);
  304. }
  305. void UpdateWorld (ISkeletonAnimation anim) {
  306. UpdateAllBones(SkeletonUtilityBone.UpdatePhase.World);
  307. for (int i = 0, n = constraintComponents.Count; i < n; i++)
  308. constraintComponents[i].DoUpdate();
  309. }
  310. void UpdateComplete (ISkeletonAnimation anim) {
  311. UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Complete);
  312. }
  313. void UpdateAllBones (SkeletonUtilityBone.UpdatePhase phase) {
  314. if (boneRoot == null)
  315. CollectBones();
  316. var boneComponents = this.boneComponents;
  317. if (boneComponents == null) return;
  318. for (int i = 0, n = boneComponents.Count; i < n; i++)
  319. boneComponents[i].DoUpdate(phase);
  320. }
  321. public Transform GetBoneRoot () {
  322. if (boneRoot != null)
  323. return boneRoot;
  324. var boneRootObject = new GameObject("SkeletonUtility-SkeletonRoot");
  325. #if UNITY_EDITOR
  326. if (!Application.isPlaying)
  327. UnityEditor.Undo.RegisterCreatedObjectUndo(boneRootObject, "Spawn Bone");
  328. #endif
  329. if (skeletonGraphic != null)
  330. boneRootObject.AddComponent<RectTransform>();
  331. boneRoot = boneRootObject.transform;
  332. boneRoot.SetParent(transform);
  333. boneRoot.localPosition = Vector3.zero;
  334. boneRoot.localRotation = Quaternion.identity;
  335. boneRoot.localScale = Vector3.one;
  336. return boneRoot;
  337. }
  338. public GameObject SpawnRoot (SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  339. GetBoneRoot();
  340. Skeleton skeleton = this.skeletonComponent.Skeleton;
  341. GameObject go = SpawnBone(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
  342. CollectBones();
  343. return go;
  344. }
  345. public GameObject SpawnHierarchy (SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  346. GetBoneRoot();
  347. Skeleton skeleton = this.skeletonComponent.Skeleton;
  348. GameObject go = SpawnBoneRecursively(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
  349. CollectBones();
  350. return go;
  351. }
  352. public GameObject SpawnBoneRecursively (Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  353. GameObject go = SpawnBone(bone, parent, mode, pos, rot, sca);
  354. ExposedList<Bone> childrenBones = bone.Children;
  355. for (int i = 0, n = childrenBones.Count; i < n; i++) {
  356. Bone child = childrenBones.Items[i];
  357. SpawnBoneRecursively(child, go.transform, mode, pos, rot, sca);
  358. }
  359. return go;
  360. }
  361. public GameObject SpawnBone (Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  362. GameObject go = new GameObject(bone.Data.Name);
  363. #if UNITY_EDITOR
  364. if (!Application.isPlaying)
  365. UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Spawn Bone");
  366. #endif
  367. if (skeletonGraphic != null)
  368. go.AddComponent<RectTransform>();
  369. var goTransform = go.transform;
  370. goTransform.SetParent(parent);
  371. SkeletonUtilityBone b = go.AddComponent<SkeletonUtilityBone>();
  372. b.hierarchy = this;
  373. b.position = pos;
  374. b.rotation = rot;
  375. b.scale = sca;
  376. b.mode = mode;
  377. b.zPosition = true;
  378. b.Reset();
  379. b.bone = bone;
  380. b.boneName = bone.Data.Name;
  381. b.valid = true;
  382. if (mode == SkeletonUtilityBone.Mode.Override) {
  383. if (rot) goTransform.localRotation = Quaternion.Euler(0, 0, b.bone.AppliedRotation);
  384. if (pos) goTransform.localPosition = new Vector3(b.bone.X * positionScale, b.bone.Y * positionScale, 0);
  385. goTransform.localScale = new Vector3(b.bone.scaleX, b.bone.scaleY, 0);
  386. }
  387. return go;
  388. }
  389. }
  390. }