Hi, I came across the Speech Event Tokens feature in the manual, and it looks like an amazing way to trigger animations through dialogue! I was wondering if it's possible to use it with Spine 2D animations as well.
I tried modifying the script as shown below, and while the console correctly displays a list of all available Spine animations, the specific animation I’m trying to play doesn’t seem to run.
using UnityEngine;
using AC;
using Spine.Unity;
public class SpeechTokenHandlerSpine : MonoBehaviour
{
private void OnEnable()
{
// Register tokens (e.g., "anim" for triggering Spine animations)
KickStarter.dialog.SpeechEventTokenKeys = new string[] { "anim" };
// Subscribe to the event
EventManager.OnSpeechToken += OnSpeechToken;
}
private void OnDisable()
{
EventManager.OnSpeechToken -= OnSpeechToken;
}
private void OnSpeechToken(AC.Char speakingCharacter, int lineID, string tokenKey, string tokenValue)
{
if (tokenKey == "anim")
{
Debug.Log($"{speakingCharacter.name} triggered Spine animation: {tokenValue}");
// Find the SkeletonAnimation component on the speaking character
SkeletonAnimation skeletonAnim = speakingCharacter.GetComponentInChildren<SkeletonAnimation>();
if (skeletonAnim != null)
{
// Debug: Print all available animations in this character's Spine skeleton
foreach (var anim in skeletonAnim.skeleton.Data.Animations)
{
Debug.Log("Available Spine animation: " + anim.Name);
}
// Check if the requested animation exists
Spine.Animation animation = skeletonAnim.skeleton.Data.FindAnimation(tokenValue);
if (animation == null)
{
Debug.LogError($"Animation '{tokenValue}' not found on {speakingCharacter.name}'s Spine skeleton.");
return;
}
// Clear previous animation and play the new one
skeletonAnim.AnimationState.SetEmptyAnimation(0, 0.1f);
skeletonAnim.AnimationState.SetAnimation(0, tokenValue, false);
}
else
{
Debug.LogWarning($"No SkeletonAnimation component found on {speakingCharacter.name}");
}
}
}
}
If Speech Event Tokens are compatible with Spine 2D, I’d really appreciate some guidance on how to set it up correctly. Thanks so much for your help.
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Speech event tokens will run no matter the animation engine - but the animation engine will determine what code you can run in such an event.
Spine isn't an official integration, but if its controlling the character's skeleton each frame then there'll be a conflict with your own code that attempts to do the same thing.
AC's built-in animation engines are set to ignore their standard update code if the character's charState variable is set to Custom. I don't know if the integration is set up to cater for this, however.
Thank you so much for the information and the link! I'll dig into it from there.