SkeletonUtility.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. /*****************************************************************************
  31. * Skeleton Utility created by Mitch Thompson
  32. * Full irrevocable rights and permissions granted to Esoteric Software
  33. *****************************************************************************/
  34. using UnityEngine;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using Spine;
  38. [RequireComponent(typeof(ISkeletonAnimation))]
  39. [ExecuteInEditMode]
  40. public class SkeletonUtility : MonoBehaviour
  41. {
  42. public static T GetInParent<T>(Transform origin) where T : Component
  43. {
  44. #if UNITY_4_3
  45. Transform parent = origin.parent;
  46. while(parent.GetComponent<T>() == null){
  47. parent = parent.parent;
  48. if(parent == null)
  49. return default(T);
  50. }
  51. return parent.GetComponent<T>();
  52. #else
  53. return origin.GetComponentInParent<T>();
  54. #endif
  55. }
  56. public static PolygonCollider2D AddBoundingBox(Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true)
  57. {
  58. // List<Attachment> attachments = new List<Attachment>();
  59. Skin skin;
  60. if (skinName == "")
  61. skinName = skeleton.Data.DefaultSkin.Name;
  62. skin = skeleton.Data.FindSkin(skinName);
  63. if (skin == null)
  64. {
  65. Debug.LogError("Skin " + skinName + " not found!");
  66. return null;
  67. }
  68. var attachment = skin.GetAttachment(skeleton.FindSlotIndex(slotName), attachmentName);
  69. if (attachment is BoundingBoxAttachment)
  70. {
  71. GameObject go = new GameObject("[BoundingBox]" + attachmentName);
  72. go.transform.parent = parent;
  73. go.transform.localPosition = Vector3.zero;
  74. go.transform.localRotation = Quaternion.identity;
  75. go.transform.localScale = Vector3.one;
  76. var collider = go.AddComponent<PolygonCollider2D>();
  77. collider.isTrigger = isTrigger;
  78. var boundingBox = (BoundingBoxAttachment)attachment;
  79. float[] floats = boundingBox.Vertices;
  80. int floatCount = floats.Length;
  81. int vertCount = floatCount / 2;
  82. Vector2[] verts = new Vector2[vertCount];
  83. int v = 0;
  84. for (int i = 0; i < floatCount; i += 2, v++)
  85. {
  86. verts[v].x = floats[i];
  87. verts[v].y = floats[i + 1];
  88. }
  89. collider.SetPath(0, verts);
  90. return collider;
  91. }
  92. return null;
  93. }
  94. public static PolygonCollider2D AddBoundingBoxAsComponent(BoundingBoxAttachment boundingBox, GameObject gameObject, bool isTrigger = true)
  95. {
  96. if (boundingBox == null)
  97. {
  98. return null;
  99. }
  100. PolygonCollider2D polygonCollider2D = gameObject.AddComponent<PolygonCollider2D>();
  101. polygonCollider2D.isTrigger = isTrigger;
  102. float[] vertices = boundingBox.Vertices;
  103. int num = vertices.Length;
  104. int num2 = num / 2;
  105. Vector2[] array = new Vector2[num2];
  106. int num3 = 0;
  107. int i = 0;
  108. while (i < num)
  109. {
  110. array[num3].x = vertices[i];
  111. array[num3].y = vertices[i + 1];
  112. i += 2;
  113. num3++;
  114. }
  115. polygonCollider2D.SetPath(0, array);
  116. return polygonCollider2D;
  117. }
  118. public delegate void SkeletonUtilityDelegate();
  119. public event SkeletonUtilityDelegate OnReset;
  120. public Transform boneRoot;
  121. void Update()
  122. {
  123. if (boneRoot != null && skeletonRenderer.skeleton != null)
  124. {
  125. Vector3 flipScale = Vector3.one;
  126. if (skeletonRenderer.skeleton.FlipX)
  127. flipScale.x = -1;
  128. if (skeletonRenderer.skeleton.FlipY)
  129. flipScale.y = -1;
  130. boneRoot.localScale = flipScale;
  131. }
  132. }
  133. [HideInInspector]
  134. public SkeletonRenderer skeletonRenderer;
  135. [HideInInspector]
  136. public ISkeletonAnimation skeletonAnimation;
  137. [System.NonSerialized]
  138. public List<SkeletonUtilityBone> utilityBones = new List<SkeletonUtilityBone>();
  139. [System.NonSerialized]
  140. public List<SkeletonUtilityConstraint> utilityConstraints = new List<SkeletonUtilityConstraint>();
  141. // Dictionary<Bone, SkeletonUtilityBone> utilityBoneTable;
  142. protected bool hasTransformBones;
  143. protected bool hasUtilityConstraints;
  144. protected bool needToReprocessBones;
  145. void OnEnable()
  146. {
  147. if (skeletonRenderer == null)
  148. {
  149. skeletonRenderer = GetComponent<SkeletonRenderer>();
  150. }
  151. if (skeletonAnimation == null)
  152. {
  153. skeletonAnimation = GetComponent<SkeletonAnimation>();
  154. if (skeletonAnimation == null)
  155. skeletonAnimation = GetComponent<SkeletonAnimator>();
  156. }
  157. skeletonRenderer.OnReset -= HandleRendererReset;
  158. skeletonRenderer.OnReset += HandleRendererReset;
  159. if (skeletonAnimation != null)
  160. {
  161. skeletonAnimation.UpdateLocal -= UpdateLocal;
  162. skeletonAnimation.UpdateLocal += UpdateLocal;
  163. }
  164. CollectBones();
  165. }
  166. void Start()
  167. {
  168. //recollect because order of operations failure when switching between game mode and edit mode...
  169. // CollectBones();
  170. }
  171. void OnDisable()
  172. {
  173. skeletonRenderer.OnReset -= HandleRendererReset;
  174. if (skeletonAnimation != null)
  175. {
  176. skeletonAnimation.UpdateLocal -= UpdateLocal;
  177. skeletonAnimation.UpdateWorld -= UpdateWorld;
  178. skeletonAnimation.UpdateComplete -= UpdateComplete;
  179. }
  180. }
  181. void HandleRendererReset(SkeletonRenderer r)
  182. {
  183. if (OnReset != null)
  184. OnReset();
  185. CollectBones();
  186. }
  187. public void RegisterBone(SkeletonUtilityBone bone)
  188. {
  189. if (utilityBones.Contains(bone))
  190. return;
  191. else
  192. {
  193. utilityBones.Add(bone);
  194. needToReprocessBones = true;
  195. }
  196. }
  197. public void UnregisterBone(SkeletonUtilityBone bone)
  198. {
  199. utilityBones.Remove(bone);
  200. }
  201. public void RegisterConstraint(SkeletonUtilityConstraint constraint)
  202. {
  203. if (utilityConstraints.Contains(constraint))
  204. return;
  205. else
  206. {
  207. utilityConstraints.Add(constraint);
  208. needToReprocessBones = true;
  209. }
  210. }
  211. public void UnregisterConstraint(SkeletonUtilityConstraint constraint)
  212. {
  213. utilityConstraints.Remove(constraint);
  214. }
  215. public void CollectBones()
  216. {
  217. if (skeletonRenderer.skeleton == null)
  218. return;
  219. if (boneRoot != null)
  220. {
  221. List<string> constraintTargetNames = new List<string>();
  222. foreach (IkConstraint c in skeletonRenderer.skeleton.IkConstraints)
  223. {
  224. constraintTargetNames.Add(c.Target.Data.Name);
  225. }
  226. foreach (var b in utilityBones)
  227. {
  228. if (b.bone == null)
  229. {
  230. return;
  231. }
  232. if (b.mode == SkeletonUtilityBone.Mode.Override)
  233. {
  234. hasTransformBones = true;
  235. }
  236. if (constraintTargetNames.Contains(b.bone.Data.Name))
  237. {
  238. hasUtilityConstraints = true;
  239. }
  240. }
  241. if (utilityConstraints.Count > 0)
  242. hasUtilityConstraints = true;
  243. if (skeletonAnimation != null)
  244. {
  245. skeletonAnimation.UpdateWorld -= UpdateWorld;
  246. skeletonAnimation.UpdateComplete -= UpdateComplete;
  247. if (hasTransformBones || hasUtilityConstraints)
  248. {
  249. skeletonAnimation.UpdateWorld += UpdateWorld;
  250. }
  251. if (hasUtilityConstraints)
  252. {
  253. skeletonAnimation.UpdateComplete += UpdateComplete;
  254. }
  255. }
  256. needToReprocessBones = false;
  257. }
  258. else
  259. {
  260. utilityBones.Clear();
  261. utilityConstraints.Clear();
  262. }
  263. }
  264. void UpdateLocal(SkeletonRenderer anim)
  265. {
  266. if (needToReprocessBones)
  267. CollectBones();
  268. if (utilityBones == null)
  269. return;
  270. foreach (SkeletonUtilityBone b in utilityBones)
  271. {
  272. b.transformLerpComplete = false;
  273. }
  274. UpdateAllBones();
  275. }
  276. void UpdateWorld(SkeletonRenderer anim)
  277. {
  278. UpdateAllBones();
  279. foreach (SkeletonUtilityConstraint c in utilityConstraints)
  280. c.DoUpdate();
  281. }
  282. void UpdateComplete(SkeletonRenderer anim)
  283. {
  284. UpdateAllBones();
  285. }
  286. void UpdateAllBones()
  287. {
  288. if (boneRoot == null)
  289. {
  290. CollectBones();
  291. }
  292. if (utilityBones == null)
  293. return;
  294. foreach (SkeletonUtilityBone b in utilityBones)
  295. {
  296. b.DoUpdate();
  297. }
  298. }
  299. public Transform GetBoneRoot()
  300. {
  301. if (boneRoot != null)
  302. return boneRoot;
  303. boneRoot = new GameObject("SkeletonUtility-Root").transform;
  304. boneRoot.parent = transform;
  305. boneRoot.localPosition = Vector3.zero;
  306. boneRoot.localRotation = Quaternion.identity;
  307. boneRoot.localScale = Vector3.one;
  308. return boneRoot;
  309. }
  310. public GameObject SpawnRoot(SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
  311. {
  312. GetBoneRoot();
  313. Skeleton skeleton = this.skeletonRenderer.skeleton;
  314. GameObject go = SpawnBone(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
  315. CollectBones();
  316. return go;
  317. }
  318. public GameObject SpawnHierarchy(SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
  319. {
  320. GetBoneRoot();
  321. Skeleton skeleton = this.skeletonRenderer.skeleton;
  322. GameObject go = SpawnBoneRecursively(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
  323. CollectBones();
  324. return go;
  325. }
  326. public GameObject SpawnBoneRecursively(Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
  327. {
  328. GameObject go = SpawnBone(bone, parent, mode, pos, rot, sca);
  329. foreach (Bone child in bone.Children)
  330. {
  331. SpawnBoneRecursively(child, go.transform, mode, pos, rot, sca);
  332. }
  333. return go;
  334. }
  335. public GameObject SpawnBone(Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
  336. {
  337. GameObject go = new GameObject(bone.Data.Name);
  338. go.transform.parent = parent;
  339. SkeletonUtilityBone b = go.AddComponent<SkeletonUtilityBone>();
  340. b.skeletonUtility = this;
  341. b.position = pos;
  342. b.rotation = rot;
  343. b.scale = sca;
  344. b.mode = mode;
  345. b.zPosition = true;
  346. b.Reset();
  347. b.bone = bone;
  348. b.boneName = bone.Data.Name;
  349. b.valid = true;
  350. if (mode == SkeletonUtilityBone.Mode.Override)
  351. {
  352. if (rot)
  353. go.transform.localRotation = Quaternion.Euler(0, 0, b.bone.RotationIK);
  354. if (pos)
  355. go.transform.localPosition = new Vector3(b.bone.X, b.bone.Y, 0);
  356. go.transform.localScale = new Vector3(b.bone.scaleX, b.bone.scaleY, 0);
  357. }
  358. return go;
  359. }
  360. public void SpawnSubRenderers(bool disablePrimaryRenderer)
  361. {
  362. int submeshCount = GetComponent<MeshFilter>().sharedMesh.subMeshCount;
  363. for (int i = 0; i < submeshCount; i++)
  364. {
  365. GameObject go = new GameObject("Submesh " + i, typeof(MeshFilter), typeof(MeshRenderer));
  366. go.transform.parent = transform;
  367. go.transform.localPosition = Vector3.zero;
  368. go.transform.localRotation = Quaternion.identity;
  369. go.transform.localScale = Vector3.one;
  370. SkeletonUtilitySubmeshRenderer s = go.AddComponent<SkeletonUtilitySubmeshRenderer>();
  371. s.GetComponent<Renderer>().sortingOrder = i * 10;
  372. s.submeshIndex = i;
  373. }
  374. skeletonRenderer.CollectSubmeshRenderers();
  375. if (disablePrimaryRenderer)
  376. GetComponent<Renderer>().enabled = false;
  377. }
  378. }