SkeletonUtility.cs 18 KB

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