nicmar

I'm trying to adapt the SpineboyTargetController from the "Getting Started" example to use SkeletonMecanim instead of SkeletonAnimation. The original script gets the mouse position and sets a "Shoot target" bone at that position.

However I can't figure out how to get a Bone from a SkeletonMecanim, to then be able to change it's transform, and also get the transform of the animation.

What I tried skeletonMecanim.skeletonDataAsset.GetAnimationStateData().skeletonData.FindBone(boneName); returns a BoneData object, and not a Bone.

Also i can't find the equivalent of skeletonAnimation.transform in a SkeletonMecanim.

Here's the original code:
public class SpineboyTargetController : MonoBehaviour {

public SkeletonAnimation skeletonAnimation;

[SpineBone(dataField:"skeletonAnimation")]
public string boneName;
public new Camera camera;

Bone bone;

void OnValidate () {
if (skeletonAnimation == null) skeletonAnimation = GetComponent<SkeletonAnimation>();
}

void Start () {
bone = skeletonAnimation.Skeleton.FindBone(boneName);
}

void Update () {
var mousePosition = Input.mousePosition;
var worldMousePosition = camera.ScreenToWorldPoint(mousePosition);
var skeletonSpacePoint = skeletonAnimation.transform.InverseTransformPoint(worldMousePosition);
skeletonSpacePoint.x *= skeletonAnimation.Skeleton.ScaleX;
skeletonSpacePoint.y *= skeletonAnimation.Skeleton.ScaleY;
bone.SetLocalPosition(skeletonSpacePoint);
}
}
Here's my code. I've marked my issues with "ISSUE".
public class SpineBoneTargetController : MonoBehaviour {

public SkeletonMecanim skeletonMecanim;

[SpineBone(dataField:"skeletonMecanim")]
public string boneName;
public new Camera camera;

Bone bone;

void OnValidate () {
if (skeletonMecanim == null) skeletonMecanim = GetComponent<SkeletonMecanim>();
}

void Start () {

// Original code
//bone = skeletonAnimation.Skeleton.FindBone(boneName);

// ISSUE - I've tried this, but it returns a BoneData object
bone = skeletonMecanim.skeletonDataAsset.GetAnimationStateData().skeletonData.FindBone(boneName);
}

void Update () {
var mousePosition = Input.mousePosition;
var worldMousePosition = camera.ScreenToWorldPoint(mousePosition);

// ISSUE - How to get the transform of the current animation? (skeletonAnimation doesn't exist here)
var skeletonSpacePoint = skeletonAnimation.transform.InverseTransformPoint(worldMousePosition);
skeletonSpacePoint.x *= skeletonAnimation.Skeleton.ScaleX;
skeletonSpacePoint.y *= skeletonAnimation.Skeleton.ScaleY;
bone.SetLocalPosition(skeletonSpacePoint);
}
}
Thankful for any help! :)
nicmar
  • Сообщения: 24

Harald

Why are you changing any of the code to access skeletonDataAsset instead of Skeleton?

Simply use the existing code and replace skeletonAnimation with skeletonMecanim, they both are derived from the same SkeletonRenderer base class.
Аватара пользователя
Harald

Harri
  • Сообщения: 1028

nicmar

I'm not sure, I'm very new to the structure of the Spine runtime, but according to your comments on other posts, I think I will go for skeletonAnimation instead, and skip the Mecanim altogether, seems skeletonAnimation seems to be more flexible. :)

This video was extremely helpful on Spine/Unity scripting: RGD Workshop : Using Spine Assets in Unity - YouTube

Thanks for your help! :)
nicmar
  • Сообщения: 24

Harald

You're welcome!
Аватара пользователя
Harald

Harri
  • Сообщения: 1028


Вернуться в Runtimes