[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))]
[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);
}
}
}

View File

@ -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<SkeletonRenderer>() 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<Rigidbody2D>();
#if FLIPDEBUG
if (!RootRigidbody.isKinematic)
#endif
RootRigidbody.isKinematic = pinStartBone;
RootRigidbody.mass = rootMass;
var boneColliders = new List<Collider2D>();
@ -324,20 +303,12 @@ namespace Spine.Unity.Modules {
var rb = boneGameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = this.gravityScale;
#if FLIPDEBUG
rb.isKinematic = spawnKinematic;
#endif
foreach (Bone child in b.Children)
RecursivelyCreateBoneProxies(child);
}
/// <summary>Performed every skeleton animation update to translate Unity Transforms positions into Spine bone transforms.</summary>
void UpdateSpineSkeleton (ISkeletonAnimation animatedSkeleton) {
#if FLIPDEBUG
if (disableUpdateBones) return;
#endif
bool flipX = skeleton.flipX;
bool flipY = skeleton.flipY;
bool flipXOR = flipX ^ flipY;

View File

@ -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<int, Spine.Animation> animationTable = new Dictionary<int, Spine.Animation>();
readonly Dictionary<AnimationClip, int> clipNameHashCodeTable = new Dictionary<AnimationClip, int>();
readonly List<Animation> previousAnimations = new List<Animation>();
Animator animator;
List<Animation> previousAnimations = new List<Animation>();
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<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
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<Animator>(), this.skeletonDataAsset);
mecanimTranslator = mecanimTranslator ?? new MecanimTranslator();
mecanimTranslator.Initialize(GetComponent<Animator>(), this.skeletonDataAsset);
}
public void Update () {
if (!valid) return;
translator.Apply(skeleton, ref layerMixModes);
mecanimTranslator.Apply(skeleton);
// UpdateWorldTransform and Bone Callbacks
{