123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Spine;
- [RequireComponent(typeof(ISkeletonAnimation))]
- [ExecuteInEditMode]
- public class SkeletonUtility : MonoBehaviour
- {
- public static T GetInParent<T>(Transform origin) where T : Component
- {
- #if UNITY_4_3
- Transform parent = origin.parent;
- while(parent.GetComponent<T>() == null){
- parent = parent.parent;
- if(parent == null)
- return default(T);
- }
- return parent.GetComponent<T>();
- #else
- return origin.GetComponentInParent<T>();
- #endif
- }
- public static PolygonCollider2D AddBoundingBox(Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true)
- {
-
- Skin skin;
- if (skinName == "")
- skinName = skeleton.Data.DefaultSkin.Name;
- skin = skeleton.Data.FindSkin(skinName);
- if (skin == null)
- {
- Debug.LogError("Skin " + skinName + " not found!");
- return null;
- }
- var attachment = skin.GetAttachment(skeleton.FindSlotIndex(slotName), attachmentName);
- if (attachment is BoundingBoxAttachment)
- {
- GameObject go = new GameObject("[BoundingBox]" + attachmentName);
- go.transform.parent = parent;
- go.transform.localPosition = Vector3.zero;
- go.transform.localRotation = Quaternion.identity;
- go.transform.localScale = Vector3.one;
- var collider = go.AddComponent<PolygonCollider2D>();
- collider.isTrigger = isTrigger;
- var boundingBox = (BoundingBoxAttachment)attachment;
- float[] floats = boundingBox.Vertices;
- int floatCount = floats.Length;
- int vertCount = floatCount / 2;
- Vector2[] verts = new Vector2[vertCount];
- int v = 0;
- for (int i = 0; i < floatCount; i += 2, v++)
- {
- verts[v].x = floats[i];
- verts[v].y = floats[i + 1];
- }
- collider.SetPath(0, verts);
- return collider;
- }
- return null;
- }
- public static PolygonCollider2D AddBoundingBoxAsComponent(BoundingBoxAttachment boundingBox, GameObject gameObject, bool isTrigger = true)
- {
- if (boundingBox == null)
- {
- return null;
- }
- PolygonCollider2D polygonCollider2D = gameObject.AddComponent<PolygonCollider2D>();
- polygonCollider2D.isTrigger = isTrigger;
- float[] vertices = boundingBox.Vertices;
- int num = vertices.Length;
- int num2 = num / 2;
- Vector2[] array = new Vector2[num2];
- int num3 = 0;
- int i = 0;
- while (i < num)
- {
- array[num3].x = vertices[i];
- array[num3].y = vertices[i + 1];
- i += 2;
- num3++;
- }
- polygonCollider2D.SetPath(0, array);
- return polygonCollider2D;
- }
- public delegate void SkeletonUtilityDelegate();
- public event SkeletonUtilityDelegate OnReset;
- public Transform boneRoot;
- void Update()
- {
- if (boneRoot != null && skeletonRenderer.skeleton != null)
- {
- Vector3 flipScale = Vector3.one;
- if (skeletonRenderer.skeleton.FlipX)
- flipScale.x = -1;
- if (skeletonRenderer.skeleton.FlipY)
- flipScale.y = -1;
- boneRoot.localScale = flipScale;
- }
- }
- [HideInInspector]
- public SkeletonRenderer skeletonRenderer;
- [HideInInspector]
- public ISkeletonAnimation skeletonAnimation;
- [System.NonSerialized]
- public List<SkeletonUtilityBone> utilityBones = new List<SkeletonUtilityBone>();
- [System.NonSerialized]
- public List<SkeletonUtilityConstraint> utilityConstraints = new List<SkeletonUtilityConstraint>();
-
- protected bool hasTransformBones;
- protected bool hasUtilityConstraints;
- protected bool needToReprocessBones;
- void OnEnable()
- {
- if (skeletonRenderer == null)
- {
- skeletonRenderer = GetComponent<SkeletonRenderer>();
- }
- if (skeletonAnimation == null)
- {
- skeletonAnimation = GetComponent<SkeletonAnimation>();
- if (skeletonAnimation == null)
- skeletonAnimation = GetComponent<SkeletonAnimator>();
- }
- skeletonRenderer.OnReset -= HandleRendererReset;
- skeletonRenderer.OnReset += HandleRendererReset;
- if (skeletonAnimation != null)
- {
- skeletonAnimation.UpdateLocal -= UpdateLocal;
- skeletonAnimation.UpdateLocal += UpdateLocal;
- }
- CollectBones();
- }
- void Start()
- {
-
-
- }
- void OnDisable()
- {
- skeletonRenderer.OnReset -= HandleRendererReset;
- if (skeletonAnimation != null)
- {
- skeletonAnimation.UpdateLocal -= UpdateLocal;
- skeletonAnimation.UpdateWorld -= UpdateWorld;
- skeletonAnimation.UpdateComplete -= UpdateComplete;
- }
- }
- void HandleRendererReset(SkeletonRenderer r)
- {
- if (OnReset != null)
- OnReset();
- CollectBones();
- }
- public void RegisterBone(SkeletonUtilityBone bone)
- {
- if (utilityBones.Contains(bone))
- return;
- else
- {
- utilityBones.Add(bone);
- needToReprocessBones = true;
- }
- }
- public void UnregisterBone(SkeletonUtilityBone bone)
- {
- utilityBones.Remove(bone);
- }
- public void RegisterConstraint(SkeletonUtilityConstraint constraint)
- {
- if (utilityConstraints.Contains(constraint))
- return;
- else
- {
- utilityConstraints.Add(constraint);
- needToReprocessBones = true;
- }
- }
- public void UnregisterConstraint(SkeletonUtilityConstraint constraint)
- {
- utilityConstraints.Remove(constraint);
- }
- public void CollectBones()
- {
- if (skeletonRenderer.skeleton == null)
- return;
- if (boneRoot != null)
- {
- List<string> constraintTargetNames = new List<string>();
- foreach (IkConstraint c in skeletonRenderer.skeleton.IkConstraints)
- {
- constraintTargetNames.Add(c.Target.Data.Name);
- }
- foreach (var b in utilityBones)
- {
- if (b.bone == null)
- {
- return;
- }
- if (b.mode == SkeletonUtilityBone.Mode.Override)
- {
- hasTransformBones = true;
- }
- if (constraintTargetNames.Contains(b.bone.Data.Name))
- {
- hasUtilityConstraints = true;
- }
- }
- if (utilityConstraints.Count > 0)
- hasUtilityConstraints = true;
- if (skeletonAnimation != null)
- {
- skeletonAnimation.UpdateWorld -= UpdateWorld;
- skeletonAnimation.UpdateComplete -= UpdateComplete;
- if (hasTransformBones || hasUtilityConstraints)
- {
- skeletonAnimation.UpdateWorld += UpdateWorld;
- }
- if (hasUtilityConstraints)
- {
- skeletonAnimation.UpdateComplete += UpdateComplete;
- }
- }
- needToReprocessBones = false;
- }
- else
- {
- utilityBones.Clear();
- utilityConstraints.Clear();
- }
- }
- void UpdateLocal(SkeletonRenderer anim)
- {
- if (needToReprocessBones)
- CollectBones();
- if (utilityBones == null)
- return;
- foreach (SkeletonUtilityBone b in utilityBones)
- {
- b.transformLerpComplete = false;
- }
- UpdateAllBones();
- }
- void UpdateWorld(SkeletonRenderer anim)
- {
- UpdateAllBones();
- foreach (SkeletonUtilityConstraint c in utilityConstraints)
- c.DoUpdate();
- }
- void UpdateComplete(SkeletonRenderer anim)
- {
- UpdateAllBones();
- }
- void UpdateAllBones()
- {
- if (boneRoot == null)
- {
- CollectBones();
- }
- if (utilityBones == null)
- return;
- foreach (SkeletonUtilityBone b in utilityBones)
- {
- b.DoUpdate();
- }
- }
- public Transform GetBoneRoot()
- {
- if (boneRoot != null)
- return boneRoot;
- boneRoot = new GameObject("SkeletonUtility-Root").transform;
- boneRoot.parent = transform;
- boneRoot.localPosition = Vector3.zero;
- boneRoot.localRotation = Quaternion.identity;
- boneRoot.localScale = Vector3.one;
- return boneRoot;
- }
- public GameObject SpawnRoot(SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
- {
- GetBoneRoot();
- Skeleton skeleton = this.skeletonRenderer.skeleton;
- GameObject go = SpawnBone(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
- CollectBones();
- return go;
- }
- public GameObject SpawnHierarchy(SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
- {
- GetBoneRoot();
- Skeleton skeleton = this.skeletonRenderer.skeleton;
- GameObject go = SpawnBoneRecursively(skeleton.RootBone, boneRoot, mode, pos, rot, sca);
- CollectBones();
- return go;
- }
- public GameObject SpawnBoneRecursively(Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
- {
- GameObject go = SpawnBone(bone, parent, mode, pos, rot, sca);
- foreach (Bone child in bone.Children)
- {
- SpawnBoneRecursively(child, go.transform, mode, pos, rot, sca);
- }
- return go;
- }
- public GameObject SpawnBone(Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca)
- {
- GameObject go = new GameObject(bone.Data.Name);
- go.transform.parent = parent;
- SkeletonUtilityBone b = go.AddComponent<SkeletonUtilityBone>();
- b.skeletonUtility = this;
- b.position = pos;
- b.rotation = rot;
- b.scale = sca;
- b.mode = mode;
- b.zPosition = true;
- b.Reset();
- b.bone = bone;
- b.boneName = bone.Data.Name;
- b.valid = true;
- if (mode == SkeletonUtilityBone.Mode.Override)
- {
- if (rot)
- go.transform.localRotation = Quaternion.Euler(0, 0, b.bone.RotationIK);
- if (pos)
- go.transform.localPosition = new Vector3(b.bone.X, b.bone.Y, 0);
- go.transform.localScale = new Vector3(b.bone.scaleX, b.bone.scaleY, 0);
- }
- return go;
- }
- public void SpawnSubRenderers(bool disablePrimaryRenderer)
- {
- int submeshCount = GetComponent<MeshFilter>().sharedMesh.subMeshCount;
- for (int i = 0; i < submeshCount; i++)
- {
- GameObject go = new GameObject("Submesh " + i, typeof(MeshFilter), typeof(MeshRenderer));
- go.transform.parent = transform;
- go.transform.localPosition = Vector3.zero;
- go.transform.localRotation = Quaternion.identity;
- go.transform.localScale = Vector3.one;
- SkeletonUtilitySubmeshRenderer s = go.AddComponent<SkeletonUtilitySubmeshRenderer>();
- s.GetComponent<Renderer>().sortingOrder = i * 10;
- s.submeshIndex = i;
- }
- skeletonRenderer.CollectSubmeshRenderers();
- if (disablePrimaryRenderer)
- GetComponent<Renderer>().enabled = false;
- }
- }
|