From a2e3d999bc774b76731c269777f5fa484a148a9b Mon Sep 17 00:00:00 2001 From: pharan Date: Thu, 15 Jun 2017 12:38:50 +0800 Subject: [PATCH] [unity] Minor SkeletonAnimator and editor code cleanup. --- .../Editor/SkeletonAnimatorInspector.cs | 9 ++-- .../Modules/Ragdoll/SkeletonRagdoll2D.cs | 29 ------------- .../Assets/spine-unity/SkeletonAnimator.cs | 42 +++++++++++-------- 3 files changed, 28 insertions(+), 52 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Editor/SkeletonAnimatorInspector.cs b/spine-unity/Assets/spine-unity/Editor/SkeletonAnimatorInspector.cs index 5ea5b3278..53ccb47f2 100644 --- a/spine-unity/Assets/spine-unity/Editor/SkeletonAnimatorInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/SkeletonAnimatorInspector.cs @@ -36,19 +36,16 @@ namespace Spine.Unity.Editor { [CustomEditor(typeof(SkeletonAnimator))] [CanEditMultipleObjects] public class SkeletonAnimatorInspector : SkeletonRendererInspector { - protected SerializedProperty layerMixModes; - protected SerializedProperty autoReset; + protected SerializedProperty mecanimTranslator; protected override void OnEnable () { base.OnEnable(); - autoReset = serializedObject.FindProperty("autoReset"); - layerMixModes = serializedObject.FindProperty("layerMixModes"); + mecanimTranslator = serializedObject.FindProperty("mecanimTranslator"); } protected override void DrawInspectorGUI (bool multi) { base.DrawInspectorGUI(multi); - EditorGUILayout.PropertyField(autoReset); - EditorGUILayout.PropertyField(layerMixModes, true); + EditorGUILayout.PropertyField(mecanimTranslator, true); } } } diff --git a/spine-unity/Assets/spine-unity/Modules/Ragdoll/SkeletonRagdoll2D.cs b/spine-unity/Assets/spine-unity/Modules/Ragdoll/SkeletonRagdoll2D.cs index 9f9163f85..9d0695a3e 100644 --- a/spine-unity/Assets/spine-unity/Modules/Ragdoll/SkeletonRagdoll2D.cs +++ b/spine-unity/Assets/spine-unity/Modules/Ragdoll/SkeletonRagdoll2D.cs @@ -30,8 +30,6 @@ // Contributed by: Mitch Thompson -//#define FLIPDEBUG - using UnityEngine; using System.Collections; using System.Collections.Generic; @@ -42,14 +40,6 @@ namespace Spine.Unity.Modules { static Transform parentSpaceHelper; #region Inspector - #if FLIPDEBUG - [Header("DEBUG")] - public bool flipXInitially; - public bool flipYInitially; - public bool spawnKinematic; - public bool disableUpdateBones; - #endif - [Header("Hierarchy")] [SpineBone] public string startingBoneName = ""; @@ -94,20 +84,12 @@ namespace Spine.Unity.Modules { IEnumerator Start () { if (parentSpaceHelper == null) { parentSpaceHelper = (new GameObject("Parent Space Helper")).transform; - #if !FLIPDEBUG - parentSpaceHelper.hideFlags = HideFlags.HideInHierarchy; - #endif } targetSkeletonComponent = GetComponent() as ISkeletonAnimation; if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible."); skeleton = targetSkeletonComponent.Skeleton; - #if FLIPDEBUG - skeleton.flipX = flipXInitially; - skeleton.flipY = flipYInitially; - #endif - if (applyOnStart) { yield return null; Apply(); @@ -144,9 +126,6 @@ namespace Spine.Unity.Modules { RecursivelyCreateBoneProxies(startingBone); RootRigidbody = boneTable[startingBone].GetComponent(); - #if FLIPDEBUG - if (!RootRigidbody.isKinematic) - #endif RootRigidbody.isKinematic = pinStartBone; RootRigidbody.mass = rootMass; var boneColliders = new List(); @@ -324,20 +303,12 @@ namespace Spine.Unity.Modules { var rb = boneGameObject.AddComponent(); rb.gravityScale = this.gravityScale; - #if FLIPDEBUG - rb.isKinematic = spawnKinematic; - #endif - foreach (Bone child in b.Children) RecursivelyCreateBoneProxies(child); } /// Performed every skeleton animation update to translate Unity Transforms positions into Spine bone transforms. void UpdateSpineSkeleton (ISkeletonAnimation animatedSkeleton) { - #if FLIPDEBUG - if (disableUpdateBones) return; - #endif - bool flipX = skeleton.flipX; bool flipY = skeleton.flipY; bool flipXOR = flipX ^ flipY; diff --git a/spine-unity/Assets/spine-unity/SkeletonAnimator.cs b/spine-unity/Assets/spine-unity/SkeletonAnimator.cs index f3f58cbcd..db3416204 100644 --- a/spine-unity/Assets/spine-unity/SkeletonAnimator.cs +++ b/spine-unity/Assets/spine-unity/SkeletonAnimator.cs @@ -37,9 +37,6 @@ namespace Spine.Unity { [RequireComponent(typeof(Animator))] public class SkeletonAnimator : SkeletonRenderer, ISkeletonAnimation { - public enum MixMode { AlwaysMix, MixNext, SpineStyle } - public MixMode[] layerMixModes = new MixMode[0]; - #region Bone Callbacks (ISkeletonAnimation) protected event UpdateBonesDelegate _UpdateLocal; protected event UpdateBonesDelegate _UpdateWorld; @@ -63,29 +60,40 @@ namespace Spine.Unity { public event UpdateBonesDelegate UpdateComplete { add { _UpdateComplete += value; } remove { _UpdateComplete -= value; } } #endregion - public class SpineMecanimTranslator { + public bool AutoReset { + get { return mecanimTranslator.autoReset; } + set { mecanimTranslator.autoReset = value; } + } + + [System.Serializable] + public class MecanimTranslator { + #region Inspector + public bool autoReset = true; + public MixMode[] layerMixModes = new MixMode[0]; + #endregion + + public enum MixMode { AlwaysMix, MixNext, SpineStyle } + readonly Dictionary animationTable = new Dictionary(); readonly Dictionary clipNameHashCodeTable = new Dictionary(); + readonly List previousAnimations = new List(); Animator animator; - List previousAnimations = new List(); - public bool autoReset = true; - public void Initialize (Animator animator, SkeletonDataAsset skeletonDataAsset) { this.animator = animator; animationTable.Clear(); clipNameHashCodeTable.Clear(); var data = skeletonDataAsset.GetSkeletonData(true); foreach (var a in data.Animations) - animationTable.Add(a.Name.GetHashCode(), a); + animationTable.Add(a.Name.GetHashCode(), a); } - public void Apply (Skeleton skeleton, ref MixMode[] layerMixModes) { + public void Apply (Skeleton skeleton) { if (layerMixModes.Length < animator.layerCount) System.Array.Resize(ref layerMixModes, animator.layerCount); - //skeleton.Update(Time.deltaTime); // Doesn't actually do anything, currently. (Spine 3.5). + //skeleton.Update(Time.deltaTime); // Doesn't actually do anything, currently. (Spine 3.6). // Clear Previous if (autoReset) { @@ -138,7 +146,7 @@ namespace Spine.Unity { // Always use Mix instead of Applying the first non-zero weighted clip. for (int c = 0; c < clipInfo.Length; c++) { var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed <0), stateInfo.loop, null, weight, MixPose.Current, MixDirection.In); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed < 0), stateInfo.loop, null, weight, MixPose.Current, MixDirection.In); } if (hasNext) { for (int c = 0; c < nextClipInfo.Length; c++) { @@ -151,13 +159,13 @@ namespace Spine.Unity { int c = 0; for (; c < clipInfo.Length; c++) { var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed <0), stateInfo.loop, null, 1f, MixPose.Current, MixDirection.In); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed < 0), stateInfo.loop, null, 1f, MixPose.Current, MixDirection.In); break; } // Mix the rest for (; c < clipInfo.Length; c++) { var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed <0), stateInfo.loop, null, weight, MixPose.Current, MixDirection.In); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed < 0), stateInfo.loop, null, weight, MixPose.Current, MixDirection.In); } c = 0; @@ -206,21 +214,21 @@ namespace Spine.Unity { } } - public SpineMecanimTranslator translator; + public MecanimTranslator mecanimTranslator; public override void Initialize (bool overwrite) { if (valid && !overwrite) return; base.Initialize(overwrite); if (!valid) return; - translator = new SpineMecanimTranslator(); - translator.Initialize(GetComponent(), this.skeletonDataAsset); + mecanimTranslator = mecanimTranslator ?? new MecanimTranslator(); + mecanimTranslator.Initialize(GetComponent(), this.skeletonDataAsset); } public void Update () { if (!valid) return; - translator.Apply(skeleton, ref layerMixModes); + mecanimTranslator.Apply(skeleton); // UpdateWorldTransform and Bone Callbacks {