源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

366 行
12 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * 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 SOFTWARE 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, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. // Contributed by: Mitch Thompson
  31. using UnityEngine;
  32. using System.Collections.Generic;
  33. using Spine;
  34. namespace Spine.Unity {
  35. [RequireComponent(typeof(ISkeletonAnimation))]
  36. [ExecuteInEditMode]
  37. public class SkeletonUtility : MonoBehaviour {
  38. #region BoundingBoxAttachment
  39. public static PolygonCollider2D AddBoundingBoxGameObject (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) {
  40. Skin skin = string.IsNullOrEmpty(skinName) ? skeleton.data.defaultSkin : skeleton.data.FindSkin(skinName);
  41. if (skin == null) {
  42. Debug.LogError("Skin " + skinName + " not found!");
  43. return null;
  44. }
  45. var attachment = skin.GetAttachment(skeleton.FindSlotIndex(slotName), attachmentName);
  46. if (attachment == null) {
  47. Debug.LogFormat("Attachment in slot '{0}' named '{1}' not found in skin '{2}'.", slotName, attachmentName, skin.name);
  48. return null;
  49. }
  50. var box = attachment as BoundingBoxAttachment;
  51. if (box != null) {
  52. var slot = skeleton.FindSlot(slotName);
  53. return AddBoundingBoxGameObject(box.Name, box, slot, parent, isTrigger);
  54. } else {
  55. Debug.LogFormat("Attachment '{0}' was not a Bounding Box.", attachmentName);
  56. return null;
  57. }
  58. }
  59. public static PolygonCollider2D AddBoundingBoxGameObject (string name, BoundingBoxAttachment box, Slot slot, Transform parent, bool isTrigger = true) {
  60. var go = new GameObject("[BoundingBox]" + (string.IsNullOrEmpty(name) ? box.Name : name));
  61. var got = go.transform;
  62. got.parent = parent;
  63. got.localPosition = Vector3.zero;
  64. got.localRotation = Quaternion.identity;
  65. got.localScale = Vector3.one;
  66. return AddBoundingBoxAsComponent(box, slot, go, isTrigger);
  67. }
  68. public static PolygonCollider2D AddBoundingBoxAsComponent (BoundingBoxAttachment box, Slot slot, GameObject gameObject, bool isTrigger = true, bool isKinematic = true, float gravityScale = 0f) {
  69. if (box == null) return null;
  70. if (slot.bone != slot.Skeleton.RootBone) {
  71. var rb = gameObject.GetComponent<Rigidbody2D>();
  72. if (rb == null) {
  73. rb = gameObject.AddComponent<Rigidbody2D>();
  74. rb.isKinematic = isKinematic;
  75. rb.gravityScale = gravityScale;
  76. }
  77. }
  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. #endregion
  102. public delegate void SkeletonUtilityDelegate ();
  103. public event SkeletonUtilityDelegate OnReset;
  104. public Transform boneRoot;
  105. void Update () {
  106. var skeleton = skeletonRenderer.skeleton;
  107. if (boneRoot != null && skeleton != null) {
  108. Vector3 flipScale = Vector3.one;
  109. if (skeleton.FlipX)
  110. flipScale.x = -1;
  111. if (skeleton.FlipY)
  112. flipScale.y = -1;
  113. boneRoot.localScale = flipScale;
  114. }
  115. }
  116. [HideInInspector]
  117. public SkeletonRenderer skeletonRenderer;
  118. [HideInInspector]
  119. public ISkeletonAnimation skeletonAnimation;
  120. [System.NonSerialized]
  121. public List<SkeletonUtilityBone> utilityBones = new List<SkeletonUtilityBone>();
  122. [System.NonSerialized]
  123. public List<SkeletonUtilityConstraint> utilityConstraints = new List<SkeletonUtilityConstraint>();
  124. protected bool hasTransformBones;
  125. protected bool hasUtilityConstraints;
  126. protected bool needToReprocessBones;
  127. void OnEnable () {
  128. if (skeletonRenderer == null) {
  129. skeletonRenderer = GetComponent<SkeletonRenderer>();
  130. }
  131. if (skeletonAnimation == null) {
  132. skeletonAnimation = GetComponent<SkeletonAnimation>();
  133. if (skeletonAnimation == null)
  134. skeletonAnimation = GetComponent<SkeletonAnimator>();
  135. }
  136. skeletonRenderer.OnRebuild -= HandleRendererReset;
  137. skeletonRenderer.OnRebuild += HandleRendererReset;
  138. if (skeletonAnimation != null) {
  139. skeletonAnimation.UpdateLocal -= UpdateLocal;
  140. skeletonAnimation.UpdateLocal += UpdateLocal;
  141. }
  142. CollectBones();
  143. }
  144. void Start () {
  145. //recollect because order of operations failure when switching between game mode and edit mode...
  146. CollectBones();
  147. }
  148. void OnDisable () {
  149. skeletonRenderer.OnRebuild -= HandleRendererReset;
  150. if (skeletonAnimation != null) {
  151. skeletonAnimation.UpdateLocal -= UpdateLocal;
  152. skeletonAnimation.UpdateWorld -= UpdateWorld;
  153. skeletonAnimation.UpdateComplete -= UpdateComplete;
  154. }
  155. }
  156. void HandleRendererReset (SkeletonRenderer r) {
  157. if (OnReset != null)
  158. OnReset();
  159. CollectBones();
  160. }
  161. public void RegisterBone (SkeletonUtilityBone bone) {
  162. if (utilityBones.Contains(bone))
  163. return;
  164. else {
  165. utilityBones.Add(bone);
  166. needToReprocessBones = true;
  167. }
  168. }
  169. public void UnregisterBone (SkeletonUtilityBone bone) {
  170. utilityBones.Remove(bone);
  171. }
  172. public void RegisterConstraint (SkeletonUtilityConstraint constraint) {
  173. if (utilityConstraints.Contains(constraint))
  174. return;
  175. else {
  176. utilityConstraints.Add(constraint);
  177. needToReprocessBones = true;
  178. }
  179. }
  180. public void UnregisterConstraint (SkeletonUtilityConstraint constraint) {
  181. utilityConstraints.Remove(constraint);
  182. }
  183. public void CollectBones () {
  184. var skeleton = skeletonRenderer.skeleton;
  185. if (skeleton == null) return;
  186. if (boneRoot != null) {
  187. var constraintTargets = new List<System.Object>();
  188. var ikConstraints = skeleton.IkConstraints;
  189. for (int i = 0, n = ikConstraints.Count; i < n; i++)
  190. constraintTargets.Add(ikConstraints.Items[i].target);
  191. var transformConstraints = skeleton.TransformConstraints;
  192. for (int i = 0, n = transformConstraints.Count; i < n; i++)
  193. constraintTargets.Add(transformConstraints.Items[i].target);
  194. var utilityBones = this.utilityBones;
  195. for (int i = 0, n = utilityBones.Count; i < n; i++) {
  196. var b = utilityBones[i];
  197. if (b.bone == null) continue;
  198. hasTransformBones |= (b.mode == SkeletonUtilityBone.Mode.Override);
  199. hasUtilityConstraints |= constraintTargets.Contains(b.bone);
  200. }
  201. hasUtilityConstraints |= utilityConstraints.Count > 0;
  202. if (skeletonAnimation != null) {
  203. skeletonAnimation.UpdateWorld -= UpdateWorld;
  204. skeletonAnimation.UpdateComplete -= UpdateComplete;
  205. if (hasTransformBones || hasUtilityConstraints)
  206. skeletonAnimation.UpdateWorld += UpdateWorld;
  207. if (hasUtilityConstraints)
  208. skeletonAnimation.UpdateComplete += UpdateComplete;
  209. }
  210. needToReprocessBones = false;
  211. } else {
  212. utilityBones.Clear();
  213. utilityConstraints.Clear();
  214. }
  215. }
  216. void UpdateLocal (ISkeletonAnimation anim) {
  217. if (needToReprocessBones)
  218. CollectBones();
  219. var utilityBones = this.utilityBones;
  220. if (utilityBones == null) return;
  221. for (int i = 0, n = utilityBones.Count; i < n; i++)
  222. utilityBones[i].transformLerpComplete = false;
  223. UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Local);
  224. }
  225. void UpdateWorld (ISkeletonAnimation anim) {
  226. UpdateAllBones(SkeletonUtilityBone.UpdatePhase.World);
  227. for (int i = 0, n = utilityConstraints.Count; i < n; i++)
  228. utilityConstraints[i].DoUpdate();
  229. }
  230. void UpdateComplete (ISkeletonAnimation anim) {
  231. UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Complete);
  232. }
  233. void UpdateAllBones (SkeletonUtilityBone.UpdatePhase phase) {
  234. if (boneRoot == null)
  235. CollectBones();
  236. var utilityBones = this.utilityBones;
  237. if (utilityBones == null) return;
  238. for (int i = 0, n = utilityBones.Count; i < n; i++)
  239. utilityBones[i].DoUpdate(phase);
  240. }
  241. public Transform GetBoneRoot () {
  242. if (boneRoot != null)
  243. return boneRoot;
  244. boneRoot = new GameObject("SkeletonUtility-Root").transform;
  245. boneRoot.parent = transform;
  246. boneRoot.localPosition = Vector3.zero;
  247. boneRoot.localRotation = Quaternion.identity;
  248. boneRoot.localScale = Vector3.one;
  249. return boneRoot;
  250. }
  251. public GameObject SpawnRoot (SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  252. GetBoneRoot();
  253. Skeleton skeleton = this.skeletonRenderer.skeleton;
  254. GameObject go = SpawnBone(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
  255. CollectBones();
  256. return go;
  257. }
  258. public GameObject SpawnHierarchy (SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  259. GetBoneRoot();
  260. Skeleton skeleton = this.skeletonRenderer.skeleton;
  261. GameObject go = SpawnBoneRecursively(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
  262. CollectBones();
  263. return go;
  264. }
  265. public GameObject SpawnBoneRecursively (Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  266. GameObject go = SpawnBone(bone, parent, mode, pos, rot, sca);
  267. ExposedList<Bone> childrenBones = bone.Children;
  268. for (int i = 0, n = childrenBones.Count; i < n; i++) {
  269. Bone child = childrenBones.Items[i];
  270. SpawnBoneRecursively(child, go.transform, mode, pos, rot, sca);
  271. }
  272. return go;
  273. }
  274. public GameObject SpawnBone (Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
  275. GameObject go = new GameObject(bone.Data.Name);
  276. go.transform.parent = parent;
  277. SkeletonUtilityBone b = go.AddComponent<SkeletonUtilityBone>();
  278. b.skeletonUtility = this;
  279. b.position = pos;
  280. b.rotation = rot;
  281. b.scale = sca;
  282. b.mode = mode;
  283. b.zPosition = true;
  284. b.Reset();
  285. b.bone = bone;
  286. b.boneName = bone.Data.Name;
  287. b.valid = true;
  288. if (mode == SkeletonUtilityBone.Mode.Override) {
  289. if (rot)
  290. go.transform.localRotation = Quaternion.Euler(0, 0, b.bone.AppliedRotation);
  291. if (pos)
  292. go.transform.localPosition = new Vector3(b.bone.X, b.bone.Y, 0);
  293. go.transform.localScale = new Vector3(b.bone.scaleX, b.bone.scaleY, 0);
  294. }
  295. return go;
  296. }
  297. }
  298. }