From 4df97a8d06ee15f36c7a52c5ea9553a1cd5cfe60 Mon Sep 17 00:00:00 2001 From: pharan Date: Wed, 15 Feb 2017 14:51:21 +0800 Subject: [PATCH] [unity] Some docs and minor additions to SkeletonExtensions --- .../Assets/spine-unity/SkeletonExtensions.cs | 106 +++++++++++++++++- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/spine-unity/Assets/spine-unity/SkeletonExtensions.cs b/spine-unity/Assets/spine-unity/SkeletonExtensions.cs index 8f04b0a8b..0e6e43987 100644 --- a/spine-unity/Assets/spine-unity/SkeletonExtensions.cs +++ b/spine-unity/Assets/spine-unity/SkeletonExtensions.cs @@ -100,16 +100,19 @@ namespace Spine.Unity { #endregion #region Bone + /// Sets the bone's (local) X and Y according to a Vector2 public static void SetPosition (this Bone bone, Vector2 position) { bone.X = position.x; bone.Y = position.y; } + /// Sets the bone's (local) X and Y according to a Vector3. The z component is ignored. public static void SetPosition (this Bone bone, Vector3 position) { bone.X = position.x; bone.Y = position.y; } + /// Gets the bone's local X and Y as a Vector2. public static Vector2 GetLocalPosition (this Bone bone) { return new Vector2(bone.x, bone.y); } @@ -126,10 +129,12 @@ namespace Spine.Unity { return o; } - public static Vector3 GetWorldPosition (this Bone bone, UnityEngine.Transform parentTransform) { - return parentTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY)); + /// Gets the bone's Unity World position using its Spine GameObject Transform. UpdateWorldTransform needs to have been called for this to return the correct, updated value. + public static Vector3 GetWorldPosition (this Bone bone, UnityEngine.Transform spineGameObjectTransform) { + return spineGameObjectTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY)); } + /// Gets the internal bone matrix as a Unity bonespace-to-skeletonspace transformation matrix. public static Matrix4x4 GetMatrix4x4 (this Bone bone) { return new Matrix4x4 { m00 = bone.a, m01 = bone.b, m03 = bone.worldX, @@ -138,7 +143,7 @@ namespace Spine.Unity { }; } - /// Outputs a 2x2 Transformation Matrix that can convert a skeleton-space position to a bone-local position. + /// Calculates a 2x2 Transformation Matrix that can convert a skeleton-space position to a bone-local position. public static void GetWorldToLocalMatrix (this Bone bone, out float ia, out float ib, out float ic, out float id) { float a = bone.a, b = bone.b, c = bone.c, d = bone.d; float invDet = 1 / (a * d - b * c); @@ -236,6 +241,8 @@ namespace Spine.Unity { } namespace Spine { + using System.Collections.Generic; + public static class SkeletonExtensions { public static bool IsWeighted (this VertexAttachment va) { return va.bones != null && va.bones.Length > 0; @@ -259,6 +266,85 @@ namespace Spine { animation.Apply(skeleton, lastTime, time, loop, events, 1f, false, false); } + internal static void SetPropertyToSetupPose (this Skeleton skeleton, int propertyID) { + int tt = propertyID >> 24; + var timelineType = (TimelineType)tt; + int i = propertyID - (tt << 24); + + Bone bone; + IkConstraint ikc; + PathConstraint pc; + + switch (timelineType) { + // Bone + case TimelineType.Rotate: + bone = skeleton.bones.Items[i]; + bone.rotation = bone.data.rotation; + break; + case TimelineType.Translate: + bone = skeleton.bones.Items[i]; + bone.x = bone.data.x; + bone.y = bone.data.y; + break; + case TimelineType.Scale: + bone = skeleton.bones.Items[i]; + bone.scaleX = bone.data.scaleX; + bone.scaleY = bone.data.scaleY; + break; + case TimelineType.Shear: + bone = skeleton.bones.Items[i]; + bone.shearX = bone.data.shearX; + bone.shearY = bone.data.shearY; + break; + + // Slot + case TimelineType.Attachment: + skeleton.SetSlotAttachmentToSetupPose(i); + break; + case TimelineType.Color: + skeleton.slots.Items[i].SetColorToSetupPose(); + break; + case TimelineType.Deform: + skeleton.slots.Items[i].attachmentVertices.Clear(); + break; + + // Skeleton + case TimelineType.DrawOrder: + skeleton.SetDrawOrderToSetupPose(); + break; + + // IK Constraint + case TimelineType.IkConstraint: + ikc = skeleton.ikConstraints.Items[i]; + ikc.mix = ikc.data.mix; + ikc.bendDirection = ikc.data.bendDirection; + break; + case TimelineType.TransformConstraint: + var tc = skeleton.transformConstraints.Items[i]; + var tcData = tc.data; + tc.rotateMix = tcData.rotateMix; + tc.translateMix = tcData.translateMix; + tc.scaleMix = tcData.scaleMix; + tc.shearMix = tcData.shearMix; + break; + + // Path Constraint + case TimelineType.PathConstraintPosition: + pc = skeleton.pathConstraints.Items[i]; + pc.position = pc.data.position; + break; + case TimelineType.PathConstraintSpacing: + pc = skeleton.pathConstraints.Items[i]; + pc.spacing = pc.data.spacing; + break; + case TimelineType.PathConstraintMix: + pc = skeleton.pathConstraints.Items[i]; + pc.rotateMix = pc.data.rotateMix; + pc.translateMix = pc.data.translateMix; + break; + } + } + /// Resets the DrawOrder to the Setup Pose's draw order public static void SetDrawOrderToSetupPose (this Skeleton skeleton) { var slotsItems = skeleton.slots.Items; @@ -315,5 +401,19 @@ namespace Spine { animation.Apply(skeleton, 0, 0, false, null, 0, true, true); } #endregion + + #region Skins + /// + public static void FindNamesForSlot (this Skin skin, string slotName, SkeletonData skeletonData, List results) { + int slotIndex = skeletonData.FindSlotIndex(slotName); + skin.FindNamesForSlot(slotIndex, results); + } + + /// + public static void FindAttachmentsForSlot (this Skin skin, string slotName, SkeletonData skeletonData, List results) { + int slotIndex = skeletonData.FindSlotIndex(slotName); + skin.FindAttachmentsForSlot(slotIndex, results); + } + #endregion } }