Hi, is there a way to select a specific bone and its children in Unity? For example, I have two hip bones: one for Side A of my character and another for Side B. I'd like to fade out Side B during runtime in Unity. Can you guide me on how to do this or point me to some references? Thank you!
Select a bone and its children
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 onAdd Skeleton Utility
.
- Select your
Spawn Hierarchy:
- Click on
Spawn Hierarchy
and chooseOverride all bones
. This will create a GameObject hierarchy that mirrors your skeleton's bone structure.
- Click on
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 yourSkeletonAnimation
component. - Set the
Bone Name
property to the name of the bone you want to follow.
- Add a
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.