Change Animation Sprite

Hey all,

I just have a (hopefully) simple question.

Let’s say i’m creating a 2D Character animation where the character gets damage, and i wan’t to squint it’s eyes, is there a way i can exchange the head sprite with another one on the fly?

Maybe this is useful information, but my character sprites are saved to a png file that has the limbs and body parts exploded over a square grid, so i can create my animations in Unity.

Cheers!

Assuming your head is a separate sprite object (perhaps connected with bones or something to the body) you could just change the sprite to a squint face sprite through code when he is attacked, then switch back after a moment. Get the sprite renderer and set its sprite to the other like so: _SpriteRenderer.sprite = someOtherSprite;

My player model png looks somewhat like this:

Then i import the png as multiple sprites, and unity automatically detects them.

Does this work with the method you provided?
Let’s say this image is called character and the one with squinted eyes squintedcharacter.

Then i do something like:
_SpriteRenderer.sprite = squintedcharacter?

It works if you don’t have the sprite property of the sprite renderer in the animation.

You could change the sprite of the head in late update with something like this

public Sprite squintedcharacter;

void LateUpdate()
    {
        foreach (var renderer in GetComponents<SpriteRenderer>())
        {
            string spriteName = renderer.sprite.name; //finds the name of the sprite to be rendered
                if (sprite.name == "head") //if the sprite has the same name as one you're trying to replace than replace it, head as an example
                {
                    renderer.sprite = squintedcharacter;
                }
            }
        }
    }

Will this replace just the head? or the entire character?

Either way it’ll fix it, just wondering, thanks :smile:

That’s just to replace one sprite, to reskin the whole animator:

using UnityEngine;

public class ReskinAnimator : MonoBehaviour
{
    [SerializeField]
    private string spriteSheetName; //this will be the name of your spritesheet, no file extension

    void LateUpdate()
    {

        foreach (var renderer in GetComponents<SpriteRenderer>())
        {
            string spriteName = renderer.sprite.name; //finds the name of the sprite to be rendered
            var subSprites = Resources.LoadAll<Sprite>(spriteSheetName); //loads all the sprites in your new sprite sheet
            foreach (var sprite in subSprites)
            {

                if (sprite.name == spriteName) //if the sprite has the same name as one you're trying to replace than replace it
                {
                    renderer.sprite = sprite;
                }
            }
        }
    }
}