2
votes

I am creating a gameObject from an image("Resources/dog") by this:

tex = Resources.Load<Texture2D>("dog") as Texture2D;
Sprite sprite = new Sprite();
sprite = Sprite.Create(tex, new Rect(0, 0, 250, 150), new Vector2(Random.Range(-1.5f, 1.5f), Random.Range(-1.5f, 1.5f)));
GameObject newSprite = new GameObject();
newSprite.AddComponent<Rigidbody2D>(); 
newSprite.GetComponent<Rigidbody2D>().gravityScale = 0f;    
newSprite.AddComponent<ObjectMovement>();
newSprite.AddComponent<SpriteRenderer>();  
SR = newSprite.GetComponent<SpriteRenderer>();
SR.sprite = sprite;

I have another image("Resources/dog1") and I want to create an animation switching the images, I can create another sprite for the other image and switch between them every time update() is called but is it the best way? it will take a lot of memory.

Also, if you drag two images to the scene it creates an animated sprite, I think attaching the animation will be better then switching sprites every update for every gameobject I have(max 10). How can I achieve it?

2
Only two two images or more? Also where is the animation code? This is not complete. - Programmer
@Programmer Only two, but I have 10 objects, some are pigs some are dogs etc.. each object has two images to animate while idling.. - DAVIDBALAS1
Ok. How many seconds do you switch each two sprites? - Programmer
@Programmer I just want it to look animated, I guess 0.5sec is fine, it's just a dog waving his tail.. - DAVIDBALAS1

2 Answers

1
votes

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.