How to make Sprite sheet exchange at Unity without having to do more animations?

Asked

Viewed 2,273 times

1

Hello,

I’m playing a game where it will be possible to switch characters in Unity. The game will be in 2d, for android and I’m facing problems to make the exchange of characters.

I made a functional system of exchange, however it is very extensive and will be extremely tiring to do for all characters. I made a file to save an int of character selection. So far so good, but for each character I have to do all your animations again. With an animatorpra each one.

I wonder if there’s a way I can use the same Animator and animations for all the characters, just changing the Sprite sheet that it will last?

from now on thank you!

2 answers

1

I’m not sure, but it would be easier to create a type of Asset for each animation, like this:

[CreateAssetMenu(menuName = "Custom Assets/Animation 2D")]
public class Animation2D : ScriptableObject
{
   public Sprite[] Frames;
   public float FrameSpeed = 0.1f;
   public Sprite this[int index]{get{return Frames[index];}}
}

And then a class that would serve to perform this animation internally, like this:

[Serializable]
public class Animation2DInstance
{
 public SpriteRender Render;
 public Animation2D Animation;
 public int CurrentFrame;
 public void RunFrame()
 {
   Render.sprite = Animation[CurrentFrame];
   CurrentFrame++;
 }
}

Then in the class that performs these animations, do the following methods:

public void Play(Animation2D anim){StartCoroutine(execute_anim(anim));}
public IEnumerator execute_anim(Animation2D anim)
{
 Animation2DInstance instance = new Animation2DInstance(anim);
 float time = anim.FrameSpeed;
 int l = anim.Frames.Lenght;
 while(instance.CurrentFrame < l)
 {
   instance.RunFrame();
   yield return new WaitForSeconds(time);
 }
}

0

What you can do is create a Script like the following:

public int spriteAtual;
public Sprite[] sprites;
private SpriteRenderer img;

void Start
{
    img = gameObject.getComponent<SpriteRenderer>();
}
void Update
{
    img.Sprite = sprites[spriteAtual];
}

And in the animation you only change the value of the int spriteAtual, so the code will update the Sprite according to the value of the int, then just change the sprites of the matrix "sprites" in the inspector and reuse the code and animation without needing anything else. If you still wanted to change the sprites by code you can also create several matrices and use the Switch function as below

public int animacaoX;
public int spriteAtual;
public Sprite[] spritesDaAnimacaoX;
public Sprite[] spritesDaAnimacaoX2;
public Sprite[] spritesDaAnimacaoX3;
private SpriteRenderer img;

void Start
{
    img = gameObject.getComponent<SpriteRenderer>();
}

void Update
{
    img.Sprite = animacaoAtual(animacaoX)[spriteAtual];
}

Sprite[] AnimacaoAtual()
{
    Switch qualAnimacao
        case 1:
            return spritesDaAnimacaoX[];
            break;

        case 2:
            return spritesDaAnimacaoX2[];
            break;

        case 3:
            return spritesDaAnimacaoX3[];
            break;
}

If you use the same Animator for more than one character it may be that everyone gets the same synchronized animation, in which case you have to duplicate the Animator for each character to be independent

NOTE: I created the codes by cell phone, so there may be errors, (mainly pq do not know if it is possible a method that returns an array) but the important thing is to understand the idea and reproduce yourself, I hope to have helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.