[unity] Minor SkeletonAnimator and editor code cleanup.

This commit is contained in:
pharan 2017-06-15 12:38:50 +08:00
parent e037470fb9
commit a2e3d999bc
3 changed files with 28 additions and 52 deletions

View File

@ -36,19 +36,16 @@ namespace Spine.Unity.Editor {
[CustomEditor(typeof(SkeletonAnimator))] [CustomEditor(typeof(SkeletonAnimator))]
[CanEditMultipleObjects] [CanEditMultipleObjects]
public class SkeletonAnimatorInspector : SkeletonRendererInspector { public class SkeletonAnimatorInspector : SkeletonRendererInspector {
protected SerializedProperty layerMixModes; protected SerializedProperty mecanimTranslator;
protected SerializedProperty autoReset;
protected override void OnEnable () { protected override void OnEnable () {
base.OnEnable(); base.OnEnable();
autoReset = serializedObject.FindProperty("autoReset"); mecanimTranslator = serializedObject.FindProperty("mecanimTranslator");
layerMixModes = serializedObject.FindProperty("layerMixModes");
} }
protected override void DrawInspectorGUI (bool multi) { protected override void DrawInspectorGUI (bool multi) {
base.DrawInspectorGUI(multi); base.DrawInspectorGUI(multi);
EditorGUILayout.PropertyField(autoReset); EditorGUILayout.PropertyField(mecanimTranslator, true);
EditorGUILayout.PropertyField(layerMixModes, true);
} }
} }
} }

View File

@ -30,8 +30,6 @@
// Contributed by: Mitch Thompson // Contributed by: Mitch Thompson
//#define FLIPDEBUG
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -42,14 +40,6 @@ namespace Spine.Unity.Modules {
static Transform parentSpaceHelper; static Transform parentSpaceHelper;
#region Inspector #region Inspector
#if FLIPDEBUG
[Header("DEBUG")]
public bool flipXInitially;
public bool flipYInitially;
public bool spawnKinematic;
public bool disableUpdateBones;
#endif
[Header("Hierarchy")] [Header("Hierarchy")]
[SpineBone] [SpineBone]
public string startingBoneName = ""; public string startingBoneName = "";
@ -94,20 +84,12 @@ namespace Spine.Unity.Modules {
IEnumerator Start () { IEnumerator Start () {
if (parentSpaceHelper == null) { if (parentSpaceHelper == null) {
parentSpaceHelper = (new GameObject("Parent Space Helper")).transform; parentSpaceHelper = (new GameObject("Parent Space Helper")).transform;
#if !FLIPDEBUG
parentSpaceHelper.hideFlags = HideFlags.HideInHierarchy;
#endif
} }
targetSkeletonComponent = GetComponent<SkeletonRenderer>() as ISkeletonAnimation; targetSkeletonComponent = GetComponent<SkeletonRenderer>() as ISkeletonAnimation;
if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible."); if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible.");
skeleton = targetSkeletonComponent.Skeleton; skeleton = targetSkeletonComponent.Skeleton;
#if FLIPDEBUG
skeleton.flipX = flipXInitially;
skeleton.flipY = flipYInitially;
#endif
if (applyOnStart) { if (applyOnStart) {
yield return null; yield return null;
Apply(); Apply();
@ -144,9 +126,6 @@ namespace Spine.Unity.Modules {
RecursivelyCreateBoneProxies(startingBone); RecursivelyCreateBoneProxies(startingBone);
RootRigidbody = boneTable[startingBone].GetComponent<Rigidbody2D>(); RootRigidbody = boneTable[startingBone].GetComponent<Rigidbody2D>();
#if FLIPDEBUG
if (!RootRigidbody.isKinematic)
#endif
RootRigidbody.isKinematic = pinStartBone; RootRigidbody.isKinematic = pinStartBone;
RootRigidbody.mass = rootMass; RootRigidbody.mass = rootMass;
var boneColliders = new List<Collider2D>(); var boneColliders = new List<Collider2D>();
@ -324,20 +303,12 @@ namespace Spine.Unity.Modules {
var rb = boneGameObject.AddComponent<Rigidbody2D>(); var rb = boneGameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = this.gravityScale; rb.gravityScale = this.gravityScale;
#if FLIPDEBUG
rb.isKinematic = spawnKinematic;
#endif
foreach (Bone child in b.Children) foreach (Bone child in b.Children)
RecursivelyCreateBoneProxies(child); RecursivelyCreateBoneProxies(child);
} }
/// <summary>Performed every skeleton animation update to translate Unity Transforms positions into Spine bone transforms.</summary> /// <summary>Performed every skeleton animation update to translate Unity Transforms positions into Spine bone transforms.</summary>
void UpdateSpineSkeleton (ISkeletonAnimation animatedSkeleton) { void UpdateSpineSkeleton (ISkeletonAnimation animatedSkeleton) {
#if FLIPDEBUG
if (disableUpdateBones) return;
#endif
bool flipX = skeleton.flipX; bool flipX = skeleton.flipX;
bool flipY = skeleton.flipY; bool flipY = skeleton.flipY;
bool flipXOR = flipX ^ flipY; bool flipXOR = flipX ^ flipY;

View File

@ -37,9 +37,6 @@ namespace Spine.Unity {
[RequireComponent(typeof(Animator))] [RequireComponent(typeof(Animator))]
public class SkeletonAnimator : SkeletonRenderer, ISkeletonAnimation { public class SkeletonAnimator : SkeletonRenderer, ISkeletonAnimation {
public enum MixMode { AlwaysMix, MixNext, SpineStyle }
public MixMode[] layerMixModes = new MixMode[0];
#region Bone Callbacks (ISkeletonAnimation) #region Bone Callbacks (ISkeletonAnimation)
protected event UpdateBonesDelegate _UpdateLocal; protected event UpdateBonesDelegate _UpdateLocal;
protected event UpdateBonesDelegate _UpdateWorld; protected event UpdateBonesDelegate _UpdateWorld;
@ -63,29 +60,40 @@ namespace Spine.Unity {
public event UpdateBonesDelegate UpdateComplete { add { _UpdateComplete += value; } remove { _UpdateComplete -= value; } } public event UpdateBonesDelegate UpdateComplete { add { _UpdateComplete += value; } remove { _UpdateComplete -= value; } }
#endregion #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<int, Spine.Animation> animationTable = new Dictionary<int, Spine.Animation>(); readonly Dictionary<int, Spine.Animation> animationTable = new Dictionary<int, Spine.Animation>();
readonly Dictionary<AnimationClip, int> clipNameHashCodeTable = new Dictionary<AnimationClip, int>(); readonly Dictionary<AnimationClip, int> clipNameHashCodeTable = new Dictionary<AnimationClip, int>();
readonly List<Animation> previousAnimations = new List<Animation>();
Animator animator; Animator animator;
List<Animation> previousAnimations = new List<Animation>();
public bool autoReset = true;
public void Initialize (Animator animator, SkeletonDataAsset skeletonDataAsset) { public void Initialize (Animator animator, SkeletonDataAsset skeletonDataAsset) {
this.animator = animator; this.animator = animator;
animationTable.Clear(); animationTable.Clear();
clipNameHashCodeTable.Clear(); clipNameHashCodeTable.Clear();
var data = skeletonDataAsset.GetSkeletonData(true); var data = skeletonDataAsset.GetSkeletonData(true);
foreach (var a in data.Animations) 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) if (layerMixModes.Length < animator.layerCount)
System.Array.Resize<MixMode>(ref layerMixModes, animator.layerCount); System.Array.Resize<MixMode>(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 // Clear Previous
if (autoReset) { if (autoReset) {
@ -138,7 +146,7 @@ namespace Spine.Unity {
// Always use Mix instead of Applying the first non-zero weighted clip. // Always use Mix instead of Applying the first non-zero weighted clip.
for (int c = 0; c < clipInfo.Length; c++) { for (int c = 0; c < clipInfo.Length; c++) {
var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; 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) { if (hasNext) {
for (int c = 0; c < nextClipInfo.Length; c++) { for (int c = 0; c < nextClipInfo.Length; c++) {
@ -151,13 +159,13 @@ namespace Spine.Unity {
int c = 0; int c = 0;
for (; c < clipInfo.Length; c++) { for (; c < clipInfo.Length; c++) {
var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; 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; break;
} }
// Mix the rest // Mix the rest
for (; c < clipInfo.Length; c++) { for (; c < clipInfo.Length; c++) {
var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; 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; c = 0;
@ -206,21 +214,21 @@ namespace Spine.Unity {
} }
} }
public SpineMecanimTranslator translator; public MecanimTranslator mecanimTranslator;
public override void Initialize (bool overwrite) { public override void Initialize (bool overwrite) {
if (valid && !overwrite) return; if (valid && !overwrite) return;
base.Initialize(overwrite); base.Initialize(overwrite);
if (!valid) return; if (!valid) return;
translator = new SpineMecanimTranslator(); mecanimTranslator = mecanimTranslator ?? new MecanimTranslator();
translator.Initialize(GetComponent<Animator>(), this.skeletonDataAsset); mecanimTranslator.Initialize(GetComponent<Animator>(), this.skeletonDataAsset);
} }
public void Update () { public void Update () {
if (!valid) return; if (!valid) return;
translator.Apply(skeleton, ref layerMixModes); mecanimTranslator.Apply(skeleton);
// UpdateWorldTransform and Bone Callbacks // UpdateWorldTransform and Bone Callbacks
{ {