Ok, this has been driving me crazy. I have a sprite sheet that I’ve split into a series of sprites, and now I want to make them into an animation. I know I’m able to drag and drop them onto a game object and it’ll automatically create an animation, but I don’t have a game object to do that on right now. Is there any more straightforward way to create a 2D animation from sprites?
Just make a sprite with the first of your animation sprites. Open your animation window and click on your sprite in the history (important). Now mark all your sprites for the animation in your project window and drag it to the animation window. Unity now adds the animation to the sprite gameobject. That’s it.
You can bypass Unity Animations and control the sprite of the SpriteRenderer via scripting. Though the approach may seem a little odd and I do not recommend it unless you have a lot of sprites that are displayed in a linear order.
First you must place the sprites you sliced into a folder titled “Resources” in the Assets folder (if it’s not already there, create it)

Once you have done this, you can load those sprites and hold references to them at run-time:
private SpriteRenderer spr;
private Sprite [] sprites;
void Start () {
spr = GetComponent<SpriteRenderer> ();
sprites = Resources.LoadAll<Sprite> ("Bars");
}
void update(){
spr.sprite = sprites[frameToSelect]
}
If you are wanting a linear animation where it goes from frame 0 to x then you can use the following approach:
using UnityEngine;
public class AnimationScript : MonoBehaviour {
public bool loop;
public float frameSeconds = 1;
//The file location of the sprites within the resources folder
public string location;
private SpriteRenderer spr;
private Sprite [] sprites;
private int frame = 0;
private float deltaTime = 0;
// Use this for initialization
void Start () {
spr = GetComponent<SpriteRenderer> ();
sprites = Resources.LoadAll<Sprite> (location);
}
// Update is called once per frame
void Update () {
//Keep track of the time that has passed
deltaTime += Time.deltaTime;
/*Loop to allow for multiple sprite frame
jumps in a single update call if needed
Useful if frameSeconds is very small*/
while (deltaTime >= frameSeconds) {
deltaTime -= frameSeconds;
frame++;
if(loop)
frame %= sprites.Length;
//Max limit
else if(frame >= sprites.Length)
frame = sprites.Length - 1;
}
//Animate sprite with selected frame
spr.sprite = sprites [frame];
}
}
Once done, attach the AnimationScript to the GameObject to be animated, and edit the properties of the attached script.
If the linear approach does not suit your particular animation you will have to manually code the frames to select each frame yourself, which I highly recommend against.
For 2D animation with sprites, I highly suggest to AVOID Mecanim. Not that it’s wrong per say, but more like it’s like doing 20 miles for something that shouldn’t take more than 2 miles.
A really simple way of doing animation in a easy way is this :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SpriteAnim : MonoBehaviour
{
//Put the sprite 2D game object in the inspector here.
public GameObject AnimatedGameObject;
//Put all the animation sprites. in this array in the inspector.
public Sprite[] Anim_Sprites;
//This will allow the script to keep track of the last displayed sprite.
public int[] First_SpriteID;
private int Cur_SpriteID;
public int[] Last_SpriteID;
private float SecsPerFrame = 0.25f;
void Awake ()
{
Cur_SpriteID = First_SpriteID = 0;
Last_SpriteID = 4;
PlayAnimation (0, 0.25f);
}
public void PlayAnimation (int ID, float secPerFrame)
{
SecsPerFrame = secPerFrame;
StopCoroutine ("AnimateSprite");
//Add as much ID as necessary. Each is a different animation.
switch (ID) {
default:
Cur_SpriteID = First_SpriteID;
StartCoroutine ("AnimateSprite", ID);
break;
case 3:
//Example if you want an animation to something specifique
//Other than just passing through the sprites.
Cur_SpriteID = First_SpriteID;
StartCoroutine ("AnimateSprite", ID);
break;
}
}
IEnumerator AnimateSprite (int ID)
{
switch (ID) {
default:
yield return new WaitForSeconds (SecsPerFrame);
AnimatedGameObject.GetComponent<SpriteRenderer> ().sprite
= Anim_Sprites [Cur_SpriteID];
Cur_SpriteID++;
if (Cur_SpriteID > Last_SpriteID[ID]) {
Cur_SpriteID = First_SpriteID[ID];
}
StartCoroutine ("AnimateSprite", ID);
break;
//This is an exemple of an animation that only play once, but needs to play fully.
//Like attacks, being hit, jumps and landings, etc.
case 7:
yield return new WaitForSeconds (SecsPerFrame);
AnimatedGameObject.GetComponent<SpriteRenderer> ().sprite
= Anim_Sprites [Cur_SpriteID];
Cur_SpriteID++;
if (Cur_SpriteID > Last_SpriteID[ID]) {
//This will return the animation to the default animation
//once the set amount of sprite has been fully displayed.
PlayAnimation(0, 0.25f);
}
else{
StartCoroutine ("AnimateSprite", ID);
}
break;
}
}
}
The initial StartCoroutine in the Awake is mostly the Idle animation. which I suggest putting as the first animation in the array (easier to remember).
The function PlayAnimation(int, float) can be easily accessed by other script if there’s a need. (Like if you put a “damage” script on a bullet, you can easily affect the animation of the target that is hit.) Its Int allow you to controls predetermined animations. (You can easily set types of enemies with either layers or tags and make sure each of those types have the same animations layout.)
It does have the inconvenient of forcing you into managing your sprite with a real precision and with numbers instead of names, but at the same time you’re supposed to know what sprites you have put int there. At worse, you can replace them at all time.
But, unlike with the Mecanim, you can easily set special animation effects without making complex call system (which gets things complicated if there’s 2 possible receivers in the scene).
The Switch() in PlayAnimation() allows you to easily create a 1-time effect that happens whenever you want. For example, throwing a item like a grenade. This allows you to time the grenade throw with the 2D animation of the character so that the grenade is instantiated as the sprites’ animation is launched. The Switch in the Corountine allows you to have special effects during the animation… be it so that the animation plays only once then it returns to idles (or something else) or make something happens at some specific point during a specific sprite (by using the ID parameter).
I suggest to not make the SecsPerFrame a fixed value and instead recalling it as much as needed so that you can actually have a smaller or bigger amount of time between each sprite change. It make you easily manage something like the running/walking animation speed. It will act the same way as exposing the animation speed in the Mecanim, but a LOT more easier to setup. ![]()
And, yeah… if you want, you could put all the public variables ( public Sprite Anim_Sprites; / private int First_SpriteID; / private int Cur_SpriteID; / private int Last_SpriteID;) into a [serializable] and just calling that one when you set up the arrays and it will work as much.
For classic animations you can use free asset GitHub - seedvalue/SimpleSpriteAnimator: Simple sprites animation Unity 3d