From e120ed382c07fb6fcd72cb3f92cbddddd5d8c967 Mon Sep 17 00:00:00 2001 From: pharan Date: Fri, 23 May 2014 14:26:07 +0800 Subject: [PATCH] Fixed BoneComponent Z-positioning. New checkboxes to customize its behavior. SkeletonRenderer property to safely reattach to a different SkeletonRenderer. --- spine-tk2d/Assets/spine-tk2d/BoneComponent.cs | 46 +++++++++++++++---- .../Editor/BoneComponentInspector.cs | 8 +++- .../Assets/spine-unity/BoneComponent.cs | 46 +++++++++++++++---- .../Editor/BoneComponentInspector.cs | 8 +++- 4 files changed, 90 insertions(+), 18 deletions(-) diff --git a/spine-tk2d/Assets/spine-tk2d/BoneComponent.cs b/spine-tk2d/Assets/spine-tk2d/BoneComponent.cs index 4486f3343..acfe183cb 100644 --- a/spine-tk2d/Assets/spine-tk2d/BoneComponent.cs +++ b/spine-tk2d/Assets/spine-tk2d/BoneComponent.cs @@ -44,12 +44,29 @@ public class BoneComponent : MonoBehaviour { public SkeletonRenderer skeletonRenderer; public Bone bone; + public bool followZPosition = true; + public bool followBoneRotation = true; + + public SkeletonRenderer SkeletonRenderer { + get { return skeletonRenderer; } + set { + skeletonRenderer = value; + Reset (); + } + } + + // TODO: Make the rotation behavior more customizable + // public bool followTransformRotation = false; + + // TODO: Make transform follow bone scale? too specific? This is really useful for shared shadow assets. + //public bool followBoneScale = false; + /// If a bone isn't set, boneName is used to find the bone. public String boneName; protected Transform cachedTransform; protected Transform skeletonTransform; - + public void Reset () { bone = null; cachedTransform = transform; @@ -77,15 +94,28 @@ public class BoneComponent : MonoBehaviour { } } + Spine.Skeleton skeleton = skeletonRenderer.skeleton; + float flipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f; + if (cachedTransform.parent == skeletonTransform) { - cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, cachedTransform.localPosition.z); - Vector3 rotation = cachedTransform.localRotation.eulerAngles; - cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation); + cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : cachedTransform.localPosition.z); + + if(followBoneRotation) { + Vector3 rotation = cachedTransform.localRotation.eulerAngles; + cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation * flipRotation); + } + } else { - cachedTransform.position = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, cachedTransform.position.z)); - Vector3 rotation = skeletonTransform.rotation.eulerAngles; - cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y, - skeletonTransform.rotation.eulerAngles.z + bone.worldRotation); + Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, 0f)); + if(!followZPosition) targetWorldPosition.z = cachedTransform.position.z; + + cachedTransform.position = targetWorldPosition; + + if(followBoneRotation) { + Vector3 rotation = skeletonTransform.rotation.eulerAngles; + cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y, + skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * flipRotation) ); + } } } } \ No newline at end of file diff --git a/spine-tk2d/Assets/spine-tk2d/Editor/BoneComponentInspector.cs b/spine-tk2d/Assets/spine-tk2d/Editor/BoneComponentInspector.cs index 494b3db2d..39325acd2 100644 --- a/spine-tk2d/Assets/spine-tk2d/Editor/BoneComponentInspector.cs +++ b/spine-tk2d/Assets/spine-tk2d/Editor/BoneComponentInspector.cs @@ -34,11 +34,14 @@ using UnityEngine; [CustomEditor(typeof(BoneComponent))] public class BoneComponentInspector : Editor { - private SerializedProperty boneName, skeletonRenderer; + private SerializedProperty boneName, skeletonRenderer, followZPosition, followBoneRotation; void OnEnable () { skeletonRenderer = serializedObject.FindProperty("skeletonRenderer"); boneName = serializedObject.FindProperty("boneName"); + followBoneRotation = serializedObject.FindProperty("followBoneRotation"); + followZPosition = serializedObject.FindProperty("followZPosition"); + } override public void OnInspectorGUI () { @@ -62,6 +65,9 @@ public class BoneComponentInspector : Editor { EditorGUILayout.EndHorizontal(); boneName.stringValue = boneIndex == 0 ? null : bones[boneIndex]; + + EditorGUILayout.PropertyField(followBoneRotation); + EditorGUILayout.PropertyField(followZPosition); } if (serializedObject.ApplyModifiedProperties() || diff --git a/spine-unity/Assets/spine-unity/BoneComponent.cs b/spine-unity/Assets/spine-unity/BoneComponent.cs index 4486f3343..acfe183cb 100644 --- a/spine-unity/Assets/spine-unity/BoneComponent.cs +++ b/spine-unity/Assets/spine-unity/BoneComponent.cs @@ -44,12 +44,29 @@ public class BoneComponent : MonoBehaviour { public SkeletonRenderer skeletonRenderer; public Bone bone; + public bool followZPosition = true; + public bool followBoneRotation = true; + + public SkeletonRenderer SkeletonRenderer { + get { return skeletonRenderer; } + set { + skeletonRenderer = value; + Reset (); + } + } + + // TODO: Make the rotation behavior more customizable + // public bool followTransformRotation = false; + + // TODO: Make transform follow bone scale? too specific? This is really useful for shared shadow assets. + //public bool followBoneScale = false; + /// If a bone isn't set, boneName is used to find the bone. public String boneName; protected Transform cachedTransform; protected Transform skeletonTransform; - + public void Reset () { bone = null; cachedTransform = transform; @@ -77,15 +94,28 @@ public class BoneComponent : MonoBehaviour { } } + Spine.Skeleton skeleton = skeletonRenderer.skeleton; + float flipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f; + if (cachedTransform.parent == skeletonTransform) { - cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, cachedTransform.localPosition.z); - Vector3 rotation = cachedTransform.localRotation.eulerAngles; - cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation); + cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : cachedTransform.localPosition.z); + + if(followBoneRotation) { + Vector3 rotation = cachedTransform.localRotation.eulerAngles; + cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation * flipRotation); + } + } else { - cachedTransform.position = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, cachedTransform.position.z)); - Vector3 rotation = skeletonTransform.rotation.eulerAngles; - cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y, - skeletonTransform.rotation.eulerAngles.z + bone.worldRotation); + Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, 0f)); + if(!followZPosition) targetWorldPosition.z = cachedTransform.position.z; + + cachedTransform.position = targetWorldPosition; + + if(followBoneRotation) { + Vector3 rotation = skeletonTransform.rotation.eulerAngles; + cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y, + skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * flipRotation) ); + } } } } \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Editor/BoneComponentInspector.cs b/spine-unity/Assets/spine-unity/Editor/BoneComponentInspector.cs index 494b3db2d..39325acd2 100644 --- a/spine-unity/Assets/spine-unity/Editor/BoneComponentInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/BoneComponentInspector.cs @@ -34,11 +34,14 @@ using UnityEngine; [CustomEditor(typeof(BoneComponent))] public class BoneComponentInspector : Editor { - private SerializedProperty boneName, skeletonRenderer; + private SerializedProperty boneName, skeletonRenderer, followZPosition, followBoneRotation; void OnEnable () { skeletonRenderer = serializedObject.FindProperty("skeletonRenderer"); boneName = serializedObject.FindProperty("boneName"); + followBoneRotation = serializedObject.FindProperty("followBoneRotation"); + followZPosition = serializedObject.FindProperty("followZPosition"); + } override public void OnInspectorGUI () { @@ -62,6 +65,9 @@ public class BoneComponentInspector : Editor { EditorGUILayout.EndHorizontal(); boneName.stringValue = boneIndex == 0 ? null : bones[boneIndex]; + + EditorGUILayout.PropertyField(followBoneRotation); + EditorGUILayout.PropertyField(followZPosition); } if (serializedObject.ApplyModifiedProperties() ||