You can do this with the Animator in Unity but doing with code gives you more control. Coroutines is one of the best ways to do this.
1.Create a script called SpriteAnimator. Copy everything from below to it.
using UnityEngine;
using System.Collections;
public class SpriteAnimator : MonoBehaviour
{
bool continueAnimation = false;
SpriteRenderer displaySprite;
Sprite sprite1;
Sprite sprite2;
public void setupSprites(SpriteRenderer displaySprite, string animSprite1, string animSprite2)
{
//Set where the animated sprite will be updated
this.displaySprite = displaySprite;
//Load Sprite 1
Texture2D tex = Resources.Load<Texture2D>(animSprite1) as Texture2D;
sprite1 = Sprite.Create(tex, new Rect(0, 0, 250, 150), new Vector2(0.5f, 0.5f), 40);
//Load Sprite 2
Texture2D tex2 = Resources.Load<Texture2D>(animSprite2) as Texture2D;
sprite2 = Sprite.Create(tex2, new Rect(0, 0, 250, 150), new Vector2(0.5f, 0.5f), 40);
}
private IEnumerator startAnimationCRT(float time)
{
if (continueAnimation)
{
yield break;
}
continueAnimation = true;
WaitForSeconds waitTime = new WaitForSeconds(time);
while (continueAnimation)
{
//Change to Sprite1
displaySprite.sprite = sprite1;
//Wait for `time` amount
yield return waitTime;
//Change Sprite
displaySprite.sprite = sprite2;
//Wait for `time` amount
yield return waitTime;
}
continueAnimation = false;
}
public void startAnimation(float time)
{
StartCoroutine(startAnimationCRT(time));
}
public void stopAnimation()
{
continueAnimation = false;
}
public void removeAnimation()
{
Destroy(this.gameObject);
}
}
2.Create another script called SpriteAnimation. Copy everything from below to it. Also remove ": MonoBehaviour" after the class name. You must do this! It can't derive from MonoBehaviour so remove it.
using UnityEngine;
using System.Collections;
public class SpriteAnimation
{
SpriteAnimator spriteAnimator;
GameObject gameObj;
public void setupSprites(string animationName, SpriteRenderer displaySprite, string animSprite1, string animSprite2)
{
gameObj = new GameObject(animationName);
gameObj.AddComponent<SpriteAnimator>();
spriteAnimator = gameObj.GetComponent<SpriteAnimator>();
spriteAnimator.setupSprites(displaySprite, animSprite1, animSprite2);
}
public void startAnimation(float time = 0.5f)
{
spriteAnimator.startAnimation(time);
}
public void stopAnimation()
{
spriteAnimator.stopAnimation();
}
public void Destroy()
{
spriteAnimator.removeAnimation();
}
}
Done. How to use:
public SpriteRenderer SR;
SpriteAnimation dogAnimation;
void Start()
{
dogAnimation = new SpriteAnimation();
dogAnimation.setupSprites("DogAnimation", SR, "dog", "dog2");
dogAnimation.startAnimation(0.2f);
}
Call dogAnimation.stopAnimation(); to stop the animation.
You can create many SpriteAnimations as you want. The line of code dogAnimation.setupSprites("DogAnimation", SR, "dog", "dog2"); will setup animation. It will create a new GameObject named DogAnimation. It will then load two images called dog and dog2 from the Resources folder so make sure those exist. SR is the texture on the screen that will be changed with dog2 and dog images. dogAnimation.startAnimation(0.2f); will start the animation and you can pass in the animation transition time inside.