Wednesday, June 22, 2016

Getting Up and Running with Spriter

Spriter is a 2D character animation tool that was recommended during the last Devgame class.

When I was an apprentice clown, my master taught me that one of the first rules of clowning is "Stay green."  It means always work something new into your performance.  Let it grow and evolve.  Far be it from me to disregard the teachings of my master.  I got Spriter and gave it a whirl.

I've talked my code-monkeys into trying the Unity plugin it because it simplifies my work for me. To give them a leg up, I animated a treasure chest opening, added the plugin, and put the chest in Unity.

This gif is unable to do the animation justice, alas. In Spriter and Unity it's quick and fluid.

For code monkeys who wish to play with it, here's what I learned:

  • The plugin automagically generates a Unity prefab for each spriter object, which you may then use as you please.
  • You will, of course, please to put a script on the prefab to control the animation.
  • Make sure you head up your script with using SpriterDotNetUnity; as this is where all of Spriter's magic lives.
  • A class level UnityAnimator is useful. For the purpose of this tutorial, I shall call mine animator instead of something silly and aggravating.
  • You get the Animator as follows:
void Update() {
   if (animator == null) {
      animator = gameObject.GetComponent<SpriterDotNetBehavior>().Animator;
   // ... and stuff.

  • Do not, repeat, do not try to fetch animator in Start(). When Start() is run, the SpriterDotNetBehavior exists, but it's Animator is null. This is the source of 100% of the bugs I experienced.
  • After that, things are pretty self explanatory. animator.Play("Walk"); plays the animation your artist has named "Walk". animator.CurrentAnimation.Name returns the name of the playing animation. animator.Progress is a number from 0 to 1 with 0 being the start of the animation and 1 being the end.
Here's an official example script showing how to use the plugin. Here's official instructions on how to use the animator.

Mainly I just wanted to warn you not to get the animation in Start(). It's the logical place to initialize such things, but nope: I got null. Gotta grab it on your first Update cycle.

1 comment: