From d4901b74d05b8e8c8a2d34090e827a482756f8d0 Mon Sep 17 00:00:00 2001 From: pharan Date: Fri, 5 Feb 2016 20:49:51 +0800 Subject: [PATCH] [Unity] GetColor, Bone position and Skeleton pose extensions. --- .../Assets/spine-unity/SkeletonExtensions.cs | 82 ++++++++++++++----- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/spine-unity/Assets/spine-unity/SkeletonExtensions.cs b/spine-unity/Assets/spine-unity/SkeletonExtensions.cs index 03cf57536..19616b2d3 100644 --- a/spine-unity/Assets/spine-unity/SkeletonExtensions.cs +++ b/spine-unity/Assets/spine-unity/SkeletonExtensions.cs @@ -1,7 +1,7 @@ /***************************************************************************** - * Spine Extensions created by Mitch Thompson + * Spine Extensions by Mitch Thompson and John Dy * Full irrevocable rights and permissions granted to Esoteric Software *****************************************************************************/ @@ -11,6 +11,14 @@ using Spine; public static class SkeletonExtensions { + const float ByteToFloat = 1f / 255f; + + #region Colors + public static Color GetColor (this Skeleton s) { return new Color(s.r, s.g, s.b, s.a); } + public static Color GetColor (this RegionAttachment a) { return new Color(a.r, a.g, a.b, a.a); } + public static Color GetColor (this MeshAttachment a) { return new Color(a.r, a.g, a.b, a.a); } + public static Color GetColor (this SkinnedMeshAttachment a) { return new Color(a.r, a.g, a.b, a.a); } + public static void SetColor (this Skeleton skeleton, Color color) { skeleton.A = color.a; skeleton.R = color.r; @@ -19,10 +27,10 @@ public static class SkeletonExtensions { } public static void SetColor (this Skeleton skeleton, Color32 color) { - skeleton.A = color.a / 255f; - skeleton.R = color.r / 255f; - skeleton.G = color.g / 255f; - skeleton.B = color.b / 255f; + skeleton.A = color.a * ByteToFloat; + skeleton.R = color.r * ByteToFloat; + skeleton.G = color.g * ByteToFloat; + skeleton.B = color.b * ByteToFloat; } public static void SetColor (this Slot slot, Color color) { @@ -33,10 +41,10 @@ public static class SkeletonExtensions { } public static void SetColor (this Slot slot, Color32 color) { - slot.A = color.a / 255f; - slot.R = color.r / 255f; - slot.G = color.g / 255f; - slot.B = color.b / 255f; + slot.A = color.a * ByteToFloat; + slot.R = color.r * ByteToFloat; + slot.G = color.g * ByteToFloat; + slot.B = color.b * ByteToFloat; } public static void SetColor (this RegionAttachment attachment, Color color) { @@ -47,10 +55,10 @@ public static class SkeletonExtensions { } public static void SetColor (this RegionAttachment attachment, Color32 color) { - attachment.A = color.a / 255f; - attachment.R = color.r / 255f; - attachment.G = color.g / 255f; - attachment.B = color.b / 255f; + attachment.A = color.a * ByteToFloat; + attachment.R = color.r * ByteToFloat; + attachment.G = color.g * ByteToFloat; + attachment.B = color.b * ByteToFloat; } public static void SetColor (this MeshAttachment attachment, Color color) { @@ -61,10 +69,10 @@ public static class SkeletonExtensions { } public static void SetColor (this MeshAttachment attachment, Color32 color) { - attachment.A = color.a / 255f; - attachment.R = color.r / 255f; - attachment.G = color.g / 255f; - attachment.B = color.b / 255f; + attachment.A = color.a * ByteToFloat; + attachment.R = color.r * ByteToFloat; + attachment.G = color.g * ByteToFloat; + attachment.B = color.b * ByteToFloat; } public static void SetColor (this SkinnedMeshAttachment attachment, Color color) { @@ -75,12 +83,14 @@ public static class SkeletonExtensions { } public static void SetColor (this SkinnedMeshAttachment attachment, Color32 color) { - attachment.A = color.a / 255f; - attachment.R = color.r / 255f; - attachment.G = color.g / 255f; - attachment.B = color.b / 255f; + attachment.A = color.a * ByteToFloat; + attachment.R = color.r * ByteToFloat; + attachment.G = color.g * ByteToFloat; + attachment.B = color.b * ByteToFloat; } + #endregion + #region Bone Position public static void SetPosition (this Bone bone, Vector2 position) { bone.X = position.x; bone.Y = position.y; @@ -91,10 +101,36 @@ public static class SkeletonExtensions { bone.Y = position.y; } + public static Vector2 GetSkeletonSpacePosition (this Bone bone) { + // TODO: This changes in v3.0 + return new Vector2(bone.worldX, bone.worldY); + } + + public static Vector3 GetWorldPosition (this Bone bone, UnityEngine.Transform parentTransform) { + return parentTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY)); + } + #endregion + + #region Posing + /// + /// Shortcut for posing a skeleton at a specific time. Time is in seconds. (frameNumber / 30f) will give you seconds. + /// If you need to do this often, you should get the Animation object yourself using skeleton.data.FindAnimation. and call Apply on that. + /// The skeleton to pose. + /// The name of the animation to use. + /// The time of the pose within the animation. + /// Wraps the time around if it is longer than the duration of the animation. + public static void PoseWithAnimation (this Skeleton skeleton, string animationName, float time, bool loop) { + // Fail loud when skeleton.data is null. + Spine.Animation animation = skeleton.data.FindAnimation(animationName); + if (animation == null) return; + animation.Apply(skeleton, 0, time, loop, null); + } + #endregion + + #region Unity Sprite To Attachments public static Attachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = "Spine/Skeleton") { var att = sprite.ToRegionAttachment(shaderName); skeleton.FindSlot(slotName).Attachment = att; - return att; } @@ -117,4 +153,6 @@ public static class SkeletonExtensions { loader = null; return att; } + #endregion + } \ No newline at end of file