Scripting a Sprite Animator


You can easily access the tk2dSpriteAnimator behaviour from code to control various parameters and to make it play various clips. In this example, we will be making the sprite react to various key inputs. Create a C# script in your project and name it TutorialAnimController. Paste the following code block into the script.

TutorialAnimController.cs
using UnityEngine;
using System.Collections;

public class TutorialAnimController : MonoBehaviour {

    // Link to the animated sprite
    private tk2dSpriteAnimator anim;

    // State variable to see if the character is walking.
    private bool walking = false;

    // Use this for initialization
    void Start () {
        // This script must be attached to the sprite to work.
        anim = GetComponent<tk2dSpriteAnimator>();
    }

    // This is called once the hit animation has compelted playing
    // It returns to playing whatever animation was active before hit
    // was playing.
    void HitCompleteDelegate(tk2dSpriteAnimator sprite, tk2dSpriteAnimationClip clip) {
        if (walking) {
            anim.Play("walk");
        } 
        else {
            anim.Play("idle");
        }
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKey(KeyCode.A)) {
            // Only play the clip if it is not already playing.
            // Calling play will restart the clip if it is already playing.
            if (!anim.IsPlaying("hit")) {
                anim.Play("hit");

                // The delegate is used here to return to the previously
                // playing clip after the "hit" animation is done playing.
                anim.AnimationCompleted = HitCompleteDelegate;
            }
        }

        if (Input.GetKey(KeyCode.D)) {
            if (!anim.IsPlaying("walk")) {

                // Walk is a looping animation
                // A looping animation never completes...
                anim.Play("walk");

                // We dont have any reason for detecting when it completes
                anim.AnimationCompleted = null;
                walking = true;
            }
        }

        if (Input.GetKey(KeyCode.W)) {
            if (!anim.IsPlaying("idle")) {
                anim.Play("idle");
                // We dont have any reason for detecting when it completes
                anim.AnimationCompleted = null;
                walking = false;
            }
        }
    }
}
TutorialAnimController.js
#pragma strict

// Link to the animated sprite
private var anim : tk2dSpriteAnimator;

// This script must be attached to the sprite to work.
anim = GetComponent(tk2dSpriteAnimator);

// State variable to see if the character is walking.
private var walking = false;

// This is called once the hit animation has compelted playing
// It returns to playing whatever animation was active before hit
// was playing.
function HitCompleteDelegate(sprite : tk2dSpriteAnimator, clip : tk2dSpriteAnimationClip) {
    if (walking) {
        anim.Play("walk");
    } 
    else {
        anim.Play("idle");
    }
}

// Update is called once per frame
function Update() {
    if (Input.GetKey(KeyCode.A)) {
        // Only play the clip if it is not already playing.
        // Calling play will restart the clip if it is already playing.
        if (!anim.IsPlaying("hit")) {
            anim.Play("hit");

            // The delegate is used here to return to the previously
            // playing clip after the "hit" animation is done playing.
            anim.AnimationCompleted = HitCompleteDelegate;
        }
    }

    if (Input.GetKey(KeyCode.D)) {
        if (!anim.IsPlaying("walk")) {

            // Walk is a looping animation
            // A looping animation never completes...
            anim.Play("walk");

            // We dont have any reason for detecting when it completes
            anim.AnimationCompleted = null;
            walking = true;
        }
    }

    if (Input.GetKey(KeyCode.W)) {
        if (!anim.IsPlaying("idle")) {
            anim.Play("idle");
            // We dont have any reason for detecting when it completes
            anim.AnimationCompleted = null;
            walking = false;
        }
    }
}

Attach this script to the animated sprite, and press play to start the game. Observe that you can use the A, D & W keys to make it perform various actions. The animation completed delegate reacts differently to the state of the animation prior to triggering the hit clip.

Hint: You can use anim.Sprite to efficiently get the sprite connected to the animator. You don't have to cache this property.

Common use

   anim.Library = Resources.Load("LoadableAnimation", typeof(tk2dSpriteAnimation)) as tk2dSpriteAnimation;
   tk2dSpriteAnimationClip currentPlayingClip = anim.CurrentClip;
    tk2dSpriteAnimationClip myClip = new tk2dSpriteAnimationClip( currentPlayingClip );
    myClip.fps = 20.0f;
    anim.Play(myClip); // You own myClip - you can modify as required, including frames.
   anim.ClipFps = PlayerSpeedToClipFps( playerSpeed );
   anim.AnimationEventTriggered = HandleAnimationEvent;

    void HandleAnimationEvent(tk2dSpriteAnimator animator, tk2dSpriteAnimationClip clip, int frameNo) {
        tk2dSpriteAnimationFrame frame = clip.GetFrame( frameNo );
        Debug.Log( frame.eventInt );
    }