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.
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;
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;
}
}
}
}
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;
}
}
}
}
}