To select a specific bone and its children in Unity, you can use the SkeletonUtility
component provided by Spine. Here are the steps to achieve this:
Add Skeleton Utility:
- Select your
SkeletonAnimation
component in the Inspector.
- Unfold the
Advanced
section and click on Add Skeleton Utility
.
Spawn Hierarchy:
- Click on
Spawn Hierarchy
and choose Override all bones
. This will create a GameObject hierarchy that mirrors your skeleton's bone structure.
Select the Bone:
- In the newly created hierarchy, find the bone you want to manipulate (e.g., the hip bone for Side B).
Add Bone Follower:
- Add a
BoneFollower
component to the selected bone.
- Set the
SkeletonRenderer
property to your SkeletonAnimation
component.
- Set the
Bone Name
property to the name of the bone you want to follow.
Fade Out Side B:
- To fade out Side B, you can manipulate the visibility or alpha of the attachments under the selected bone and its children. This can be done by iterating through the attachments and adjusting their properties.
Here is a simple script to help you fade out the attachments:
using Spine.Unity;
using UnityEngine;
public class FadeOutBone : MonoBehaviour
{
public SkeletonAnimation skeletonAnimation;
public string boneName;
public float fadeDuration = 1f;
private void Start()
{
StartCoroutine(FadeOutBoneAndChildren());
}
private IEnumerator FadeOutBoneAndChildren()
{
var skeleton = skeletonAnimation.Skeleton;
var bone = skeleton.FindBone(boneName);
if (bone == null) yield break;
float elapsedTime = 0f;
while (elapsedTime < fadeDuration)
{
float alpha = Mathf.Lerp(1f, 0f, elapsedTime / fadeDuration);
SetBoneAlpha(bone, alpha);
elapsedTime += Time.deltaTime;
yield return null;
}
SetBoneAlpha(bone, 0f);
}
private void SetBoneAlpha(Spine.Bone bone, float alpha)
{
foreach (var slot in skeletonAnimation.Skeleton.Slots)
{
if (slot.Bone == bone || slot.Bone.Parent == bone)
{
var color = slot.GetColor();
color.a = alpha;
slot.SetColor(color);
}
}
}
}
Attach this script to a GameObject in your scene, set the SkeletonAnimation
, boneName
, and fadeDuration
in the Inspector, and it will fade out the specified bone and its children over the given duration.