Skip to content

Instantly share code, notes, and snippets.

@pharan
Last active August 11, 2020 22:20
Show Gist options
  • Save pharan/a910e129337b71f57a92 to your computer and use it in GitHub Desktop.
Save pharan/a910e129337b71f57a92 to your computer and use it in GitHub Desktop.
An autoresetting script for Spine-Unity that prevents animations from inheriting changes from previously played animations, by resetting the skeleton back to its setup pose before playing a new animation.
using UnityEngine;
using System.Collections;
public class SkeletonAutoreset : MonoBehaviour {
void Start () {
var skeletonAnimation = GetComponent<SkeletonAnimation>();
if (skeletonAnimation == null) {
Debug.Log("SkeletonAutoreset uses GetComponent and needs to be attached on the same GameObject as the SkeletonAnimation component.");
return;
}
// Skeleton and AnimationState objects are not guaranteed to exist on Awake, so we get them in Start at the earliest.
// This still checks for null in case the skeletonAnimation didn't have valid SkeletonData and thus wasn't initialized.
var skeleton = skeletonAnimation.skeleton; if (skeleton == null) return;
var animationState = skeletonAnimation.state; if (animationState == null) return;
// Spine.AnimationState has predefined C# events.
// We can subscribe to them and reset the skeleton at key points to mimic the behavior of Spine editor.
animationState.Start += delegate { skeleton.SetToSetupPose(); };
}
}
@pharan
Copy link
Author

pharan commented Aug 17, 2016

The Start event doesn't have an Action signature, so it would not be correct to pass it as a handler.
You're right about the RequireComponent part.

Though this script is really for demonstration purposes (hence the heavy comments), and is only useful for people who don't use mixing, so I've stopped recommending actually using it as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment