Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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(); };
}
}
@ZoranRavic

This comment has been minimized.

Copy link

@ZoranRavic ZoranRavic commented Aug 11, 2016

Notes:
If you use animationState.Start += skeleton.SetToSetupPose(); instead then you will avoid creating a needless anonymous function.
Or if you want to reset bones only then you can do animationState.Start += skeleton.SetBonesToSetupPose();
Also if you put [RequireComponent(typeof(SkeletonAnimation))] at the top of the function you will avoid the null check after GetComponent.

@pharan

This comment has been minimized.

Copy link
Owner Author

@pharan 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
You can’t perform that action at this time.