How do you change sprite sheet in unity without having to do more animations?

1

Hello,

I'm making a game where you can make a character change in unity. The game will be in 2d, for android and I'm having trouble doing character swapping.

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

I would like to know if I can use the same animator and animations for all the characters, just changing the sprite sheet that it will use?

Thank you so much!

    
asked by anonymous 21.02.2017 / 01:40

2 answers

1

I'm not sure, but it would be easier to create an asset type 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 run 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 executes 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);
 }
}
    
31.12.2017 / 20:27
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 int spriteAtual, so the code will update the Sprite according to the value of int, then just change the sprites of the array "sprites" in the inspector and reuse code and animation without needing anything else. If you still want to change the sprites by code you can still create multiple arrays 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 has the same animation synchronized, in which case you will have to duplicate the Animator for each character to be independent

I've created the codes on the phone, so there may be errors, (mainly because I do not know if a method that returns an array is possible) but the important thing is to understand the idea and reproduce it yourself, p>     

30.08.2017 / 04:28