This commit is contained in:
badlogic 2018-02-06 15:16:44 +01:00
commit 39c63887c0
12 changed files with 130 additions and 47 deletions

View File

@ -138,7 +138,7 @@ namespace Spine.Unity {
// Recommended setup: Use local transform properties if Spine GameObject is the immediate parent // Recommended setup: Use local transform properties if Spine GameObject is the immediate parent
thisTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : thisTransform.localPosition.z); thisTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : thisTransform.localPosition.z);
if (followBoneRotation) { if (followBoneRotation) {
var halfRotation = Mathf.Atan2(bone.c, bone.a) * 0.5f; float halfRotation = Mathf.Atan2(bone.c, bone.a) * 0.5f;
if (followLocalScale && bone.scaleX < 0) // Negate rotation from negative scaleX. Don't use negative determinant. local scaleY doesn't factor into used rotation. if (followLocalScale && bone.scaleX < 0) // Negate rotation from negative scaleX. Don't use negative determinant. local scaleY doesn't factor into used rotation.
halfRotation += Mathf.PI * 0.5f; halfRotation += Mathf.PI * 0.5f;

View File

@ -79,10 +79,10 @@ namespace Spine.Unity.Editor {
var objectReferenceValue = dataField.objectReferenceValue; var objectReferenceValue = dataField.objectReferenceValue;
if (objectReferenceValue is SkeletonDataAsset) { if (objectReferenceValue is SkeletonDataAsset) {
skeletonDataAsset = (SkeletonDataAsset)objectReferenceValue; skeletonDataAsset = (SkeletonDataAsset)objectReferenceValue;
} else if (objectReferenceValue is ISkeletonComponent) { } else if (objectReferenceValue is IHasSkeletonDataAsset) {
var skeletonComponent = (ISkeletonComponent)objectReferenceValue; var hasSkeletonDataAsset = (IHasSkeletonDataAsset)objectReferenceValue;
if (skeletonComponent != null) if (hasSkeletonDataAsset != null)
skeletonDataAsset = skeletonComponent.SkeletonDataAsset; skeletonDataAsset = hasSkeletonDataAsset.SkeletonDataAsset;
} else if (objectReferenceValue != null) { } else if (objectReferenceValue != null) {
EditorGUI.LabelField(position, "ERROR:", "Invalid reference type"); EditorGUI.LabelField(position, "ERROR:", "Invalid reference type");
return; return;
@ -90,9 +90,9 @@ namespace Spine.Unity.Editor {
} else if (property.serializedObject.targetObject is Component) { } else if (property.serializedObject.targetObject is Component) {
var component = (Component)property.serializedObject.targetObject; var component = (Component)property.serializedObject.targetObject;
var skeletonComponent = component.GetComponentInChildren(typeof(ISkeletonComponent)) as ISkeletonComponent; var hasSkeletonDataAsset = component.GetComponentInChildren(typeof(IHasSkeletonDataAsset)) as IHasSkeletonDataAsset;
if (skeletonComponent != null) if (hasSkeletonDataAsset != null)
skeletonDataAsset = skeletonComponent.SkeletonDataAsset; skeletonDataAsset = hasSkeletonDataAsset.SkeletonDataAsset;
} }
if (skeletonDataAsset == null) { if (skeletonDataAsset == null) {

View File

@ -220,9 +220,9 @@ namespace Spine.Unity.Editor {
public static bool TargetsUseSameData (SerializedObject so) { public static bool TargetsUseSameData (SerializedObject so) {
if (so.isEditingMultipleObjects) { if (so.isEditingMultipleObjects) {
int n = so.targetObjects.Length; int n = so.targetObjects.Length;
var first = so.targetObjects[0] as ISkeletonComponent; var first = so.targetObjects[0] as IHasSkeletonDataAsset;
for (int i = 1; i < n; i++) { for (int i = 1; i < n; i++) {
var sr = so.targetObjects[i] as ISkeletonComponent; var sr = so.targetObjects[i] as IHasSkeletonDataAsset;
if (sr != null && sr.SkeletonDataAsset != first.SkeletonDataAsset) if (sr != null && sr.SkeletonDataAsset != first.SkeletonDataAsset)
return false; return false;
} }

View File

@ -42,7 +42,7 @@ namespace Spine.Unity {
} }
/// <summary>Holds a reference to a SkeletonDataAsset.</summary> /// <summary>Holds a reference to a SkeletonDataAsset.</summary>
public interface ISkeletonDataAssetComponent { public interface IHasSkeletonDataAsset {
/// <summary>Gets the SkeletonDataAsset of the Spine Component.</summary> /// <summary>Gets the SkeletonDataAsset of the Spine Component.</summary>
SkeletonDataAsset SkeletonDataAsset { get; } SkeletonDataAsset SkeletonDataAsset { get; }
} }

View File

@ -9,7 +9,7 @@ Shader "Spine/Skeleton Fill" {
[NoScaleOffset]_MainTex ("MainTex", 2D) = "white" {} [NoScaleOffset]_MainTex ("MainTex", 2D) = "white" {}
} }
SubShader { SubShader {
Tags { "IgnoreProjector"="True" "Queue"="Transparent" "RenderType"="Transparent" "PreviewType"="Plane" } Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
Blend One OneMinusSrcAlpha Blend One OneMinusSrcAlpha
Cull Off Cull Off
ZWrite Off ZWrite Off

View File

@ -13,7 +13,7 @@ Shader "Spine/Skeleton Tint" {
} }
SubShader { SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
Fog { Mode Off } Fog { Mode Off }
Cull Off Cull Off

View File

@ -35,7 +35,7 @@ using Spine;
namespace Spine.Unity { namespace Spine.Unity {
[ExecuteInEditMode, RequireComponent(typeof(CanvasRenderer), typeof(RectTransform)), DisallowMultipleComponent] [ExecuteInEditMode, RequireComponent(typeof(CanvasRenderer), typeof(RectTransform)), DisallowMultipleComponent]
[AddComponentMenu("Spine/SkeletonGraphic (Unity UI Canvas)")] [AddComponentMenu("Spine/SkeletonGraphic (Unity UI Canvas)")]
public class SkeletonGraphic : MaskableGraphic, ISkeletonComponent, IAnimationStateComponent, ISkeletonAnimation, ISkeletonDataAssetComponent { public class SkeletonGraphic : MaskableGraphic, ISkeletonComponent, IAnimationStateComponent, ISkeletonAnimation, IHasSkeletonDataAsset {
#region Inspector #region Inspector
public SkeletonDataAsset skeletonDataAsset; public SkeletonDataAsset skeletonDataAsset;

View File

@ -5,7 +5,7 @@ Shader "Spine/Skeleton" {
} }
SubShader { SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane"}
Fog { Mode Off } Fog { Mode Off }
Cull Off Cull Off

View File

@ -38,7 +38,7 @@ namespace Spine.Unity {
/// <summary>Renders a skeleton.</summary> /// <summary>Renders a skeleton.</summary>
[ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer)), DisallowMultipleComponent] [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer)), DisallowMultipleComponent]
[HelpURL("http://esotericsoftware.com/spine-unity-documentation#Rendering")] [HelpURL("http://esotericsoftware.com/spine-unity-documentation#Rendering")]
public class SkeletonRenderer : MonoBehaviour, ISkeletonComponent, ISkeletonDataAssetComponent { public class SkeletonRenderer : MonoBehaviour, ISkeletonComponent, IHasSkeletonDataAsset {
public delegate void SkeletonRendererDelegate (SkeletonRenderer skeletonRenderer); public delegate void SkeletonRendererDelegate (SkeletonRenderer skeletonRenderer);
public event SkeletonRendererDelegate OnRebuild; public event SkeletonRendererDelegate OnRebuild;

View File

@ -28,8 +28,10 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
//#define HINGECHAIN2D
// Contributed by: Mitch Thompson // Contributed by: Mitch Thompson
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System.Collections.Generic; using System.Collections.Generic;
@ -277,6 +279,54 @@ namespace Spine.Unity.Editor {
EditorGUIUtility.PingObject(go); EditorGUIUtility.PingObject(go);
} }
#if HINGECHAIN2D
bool CanCreateHingeChain () {
if (utilityBone == null) return false;
if (utilityBone.GetComponent<Rigidbody2D>() != null) return false;
if (utilityBone.bone != null && utilityBone.bone.Children.Count == 0) return false;
var rigidbodies = utilityBone.GetComponentsInChildren<Rigidbody2D>();
return rigidbodies.Length <= 0;
}
void CreateHingeChain () {
var utilBoneArr = utilityBone.GetComponentsInChildren<SkeletonUtilityBone>();
foreach (var utilBone in utilBoneArr) {
if (utilBone.GetComponent<Collider2D>() == null) {
if (utilBone.bone.Data.Length == 0) {
var sphere = utilBone.gameObject.AddComponent<CircleCollider2D>();
sphere.radius = 0.1f;
} else {
float length = utilBone.bone.Data.Length;
var box = utilBone.gameObject.AddComponent<BoxCollider2D>();
box.size = new Vector3(length, length / 3f, 0.2f);
box.offset = new Vector3(length / 2f, 0, 0);
}
}
utilBone.gameObject.AddComponent<Rigidbody2D>();
}
utilityBone.GetComponent<Rigidbody2D>().isKinematic = true;
foreach (var utilBone in utilBoneArr) {
if (utilBone == utilityBone)
continue;
utilBone.mode = SkeletonUtilityBone.Mode.Override;
var joint = utilBone.gameObject.AddComponent<HingeJoint2D>();
joint.connectedBody = utilBone.transform.parent.GetComponent<Rigidbody2D>();
joint.useLimits = true;
joint.limits = new JointAngleLimits2D {
min = -20,
max = 20
};
utilBone.GetComponent<Rigidbody2D>().mass = utilBone.transform.parent.GetComponent<Rigidbody2D>().mass * 0.75f;
}
}
#else
bool CanCreateHingeChain () { bool CanCreateHingeChain () {
if (utilityBone == null) if (utilityBone == null)
return false; return false;
@ -285,7 +335,7 @@ namespace Spine.Unity.Editor {
if (utilityBone.bone != null && utilityBone.bone.Children.Count == 0) if (utilityBone.bone != null && utilityBone.bone.Children.Count == 0)
return false; return false;
Rigidbody[] rigidbodies = utilityBone.GetComponentsInChildren<Rigidbody>(); var rigidbodies = utilityBone.GetComponentsInChildren<Rigidbody>();
return rigidbodies.Length <= 0; return rigidbodies.Length <= 0;
} }
@ -332,6 +382,7 @@ namespace Spine.Unity.Editor {
utilBone.gameObject.AddComponent<Rigidbody>(); utilBone.gameObject.AddComponent<Rigidbody>();
} }
#endif
} }
} }

View File

@ -267,27 +267,27 @@ namespace Spine.Unity {
for (int i = 0, n = utilityBones.Count; i < n; i++) for (int i = 0, n = utilityBones.Count; i < n; i++)
utilityBones[i].transformLerpComplete = false; utilityBones[i].transformLerpComplete = false;
UpdateAllBones(); UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Local);
} }
void UpdateWorld (ISkeletonAnimation anim) { void UpdateWorld (ISkeletonAnimation anim) {
UpdateAllBones(); UpdateAllBones(SkeletonUtilityBone.UpdatePhase.World);
for (int i = 0, n = utilityConstraints.Count; i < n; i++) for (int i = 0, n = utilityConstraints.Count; i < n; i++)
utilityConstraints[i].DoUpdate(); utilityConstraints[i].DoUpdate();
} }
void UpdateComplete (ISkeletonAnimation anim) { void UpdateComplete (ISkeletonAnimation anim) {
UpdateAllBones(); UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Complete);
} }
void UpdateAllBones () { void UpdateAllBones (SkeletonUtilityBone.UpdatePhase phase) {
if (boneRoot == null) if (boneRoot == null)
CollectBones(); CollectBones();
var utilityBones = this.utilityBones; var utilityBones = this.utilityBones;
if (utilityBones == null) return; if (utilityBones == null) return;
for (int i = 0, n = utilityBones.Count; i < n; i++) for (int i = 0, n = utilityBones.Count; i < n; i++)
utilityBones[i].DoUpdate(); utilityBones[i].DoUpdate(phase);
} }
public Transform GetBoneRoot () { public Transform GetBoneRoot () {

View File

@ -43,6 +43,12 @@ namespace Spine.Unity {
Override Override
} }
public enum UpdatePhase {
Local,
World,
Complete
}
#region Inspector #region Inspector
/// <summary>If a bone isn't set, boneName is used to find the bone.</summary> /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
public string boneName; public string boneName;
@ -71,7 +77,7 @@ namespace Spine.Unity {
skeletonTransform = skeletonUtility.transform; skeletonTransform = skeletonUtility.transform;
skeletonUtility.OnReset -= HandleOnReset; skeletonUtility.OnReset -= HandleOnReset;
skeletonUtility.OnReset += HandleOnReset; skeletonUtility.OnReset += HandleOnReset;
DoUpdate(); DoUpdate(UpdatePhase.Local);
} }
void OnEnable () { void OnEnable () {
@ -95,7 +101,7 @@ namespace Spine.Unity {
} }
} }
public void DoUpdate () { public void DoUpdate (UpdatePhase phase) {
if (!valid) { if (!valid) {
Reset(); Reset();
return; return;
@ -112,46 +118,72 @@ namespace Spine.Unity {
} }
} }
var thisTransform = cachedTransform;
float skeletonFlipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f; float skeletonFlipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
if (mode == Mode.Follow) { if (mode == Mode.Follow) {
if (!bone.appliedValid) switch (phase) {
bone.UpdateAppliedTransform(); case UpdatePhase.Local:
if (position)
thisTransform.localPosition = new Vector3(bone.x, bone.y, 0);
if (position) if (rotation) {
cachedTransform.localPosition = new Vector3(bone.ax, bone.ay, 0); if (bone.data.transformMode.InheritsRotation()) {
thisTransform.localRotation = Quaternion.Euler(0, 0, bone.rotation);
} else {
Vector3 euler = skeletonTransform.rotation.eulerAngles;
thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
}
}
if (rotation) { if (scale) {
if (bone.data.transformMode.InheritsRotation()) { thisTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);
cachedTransform.localRotation = Quaternion.Euler(0, 0, bone.AppliedRotation); incompatibleTransformMode = BoneTransformModeIncompatible(bone);
} else { }
Vector3 euler = skeletonTransform.rotation.eulerAngles; break;
cachedTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation)); case UpdatePhase.World:
} case UpdatePhase.Complete:
// Use Applied transform values (ax, ay, AppliedRotation, ascale) if world values were modified by constraints.
if (!bone.appliedValid) {
bone.UpdateAppliedTransform();
if (position)
thisTransform.localPosition = new Vector3(bone.ax, bone.ay, 0);
if (rotation) {
if (bone.data.transformMode.InheritsRotation()) {
thisTransform.localRotation = Quaternion.Euler(0, 0, bone.AppliedRotation);
} else {
Vector3 euler = skeletonTransform.rotation.eulerAngles;
thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
}
}
if (scale) {
thisTransform.localScale = new Vector3(bone.ascaleX, bone.ascaleY, 1f);
incompatibleTransformMode = BoneTransformModeIncompatible(bone);
}
}
break;
} }
if (scale) {
cachedTransform.localScale = new Vector3(bone.ascaleX, bone.ascaleY, 1f);
incompatibleTransformMode = BoneTransformModeIncompatible(bone);
}
} else if (mode == Mode.Override) { } else if (mode == Mode.Override) {
if (transformLerpComplete) if (transformLerpComplete)
return; return;
if (parentReference == null) { if (parentReference == null) {
if (position) { if (position) {
Vector3 clp = cachedTransform.localPosition; Vector3 clp = thisTransform.localPosition;
bone.x = Mathf.Lerp(bone.x, clp.x, overrideAlpha); bone.x = Mathf.Lerp(bone.x, clp.x, overrideAlpha);
bone.y = Mathf.Lerp(bone.y, clp.y, overrideAlpha); bone.y = Mathf.Lerp(bone.y, clp.y, overrideAlpha);
} }
if (rotation) { if (rotation) {
float angle = Mathf.LerpAngle(bone.Rotation, cachedTransform.localRotation.eulerAngles.z, overrideAlpha); float angle = Mathf.LerpAngle(bone.Rotation, thisTransform.localRotation.eulerAngles.z, overrideAlpha);
bone.Rotation = angle; bone.Rotation = angle;
bone.AppliedRotation = angle; bone.AppliedRotation = angle;
} }
if (scale) { if (scale) {
Vector3 cls = cachedTransform.localScale; Vector3 cls = thisTransform.localScale;
bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha); bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha); bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
} }
@ -161,19 +193,19 @@ namespace Spine.Unity {
return; return;
if (position) { if (position) {
Vector3 pos = parentReference.InverseTransformPoint(cachedTransform.position); Vector3 pos = parentReference.InverseTransformPoint(thisTransform.position);
bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha); bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha);
bone.y = Mathf.Lerp(bone.y, pos.y, overrideAlpha); bone.y = Mathf.Lerp(bone.y, pos.y, overrideAlpha);
} }
if (rotation) { if (rotation) {
float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(Vector3.forward, parentReference.InverseTransformDirection(cachedTransform.up)).eulerAngles.z, overrideAlpha); float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(Vector3.forward, parentReference.InverseTransformDirection(thisTransform.up)).eulerAngles.z, overrideAlpha);
bone.Rotation = angle; bone.Rotation = angle;
bone.AppliedRotation = angle; bone.AppliedRotation = angle;
} }
if (scale) { if (scale) {
Vector3 cls = cachedTransform.localScale; Vector3 cls = thisTransform.localScale;
bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha); bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha); bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
} }