Browse Source

[Unity] GetColor, Bone position and Skeleton pose extensions.

pharan 9 năm trước cách đây
mục cha
commit
d4901b74d0

+ 60 - 22
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
  * Full irrevocable rights and permissions granted to Esoteric Software
 *****************************************************************************/
 *****************************************************************************/
 
 
@@ -11,6 +11,14 @@ using Spine;
 
 
 public static class SkeletonExtensions {
 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) {
 	public static void SetColor (this Skeleton skeleton, Color color) {
 		skeleton.A = color.a;
 		skeleton.A = color.a;
 		skeleton.R = color.r;
 		skeleton.R = color.r;
@@ -19,10 +27,10 @@ public static class SkeletonExtensions {
 	}
 	}
 
 
 	public static void SetColor (this Skeleton skeleton, Color32 color) {
 	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) {
 	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) {
 	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) {
 	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) {
 	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) {
 	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) {
 	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) {
 	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) {
 	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) {
 	public static void SetPosition (this Bone bone, Vector2 position) {
 		bone.X = position.x;
 		bone.X = position.x;
 		bone.Y = position.y;
 		bone.Y = position.y;
@@ -91,10 +101,36 @@ public static class SkeletonExtensions {
 		bone.Y = position.y;
 		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
+	/// <summary>
+	/// 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.</summary>
+	/// <param name = "skeleton">The skeleton to pose.</param>
+	/// <param name="animationName">The name of the animation to use.</param>
+	/// <param name = "time">The time of the pose within the animation.</param>
+	/// <param name = "loop">Wraps the time around if it is longer than the duration of the animation.</param>
+	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") {
 	public static Attachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = "Spine/Skeleton") {
 		var att = sprite.ToRegionAttachment(shaderName);
 		var att = sprite.ToRegionAttachment(shaderName);
 		skeleton.FindSlot(slotName).Attachment = att;
 		skeleton.FindSlot(slotName).Attachment = att;
-
 		return att;
 		return att;
 	}
 	}
 
 
@@ -117,4 +153,6 @@ public static class SkeletonExtensions {
 		loader = null;
 		loader = null;
 		return att;
 		return att;
 	}
 	}
+	#endregion
+
 }
 }