Sprite Sheet swapping for characters and backgrounds

I’m trying to figure out a system for quickly swapping sprite sheets to change what a character or tiled background would look like similar to RPG maker. This would make reusing animations and certain code much easier.

For characters I made a system that takes in 12 sprites and then switches them accordingly for walking frames, but every time I want to change what the character looks like, I have to change all 12 sprites. The split up sprite file becomes a Texture2D, is there any way to reference the individual sprites on this split up sheet/Texture2D from code? something like spriteSheet.sprites[0] or something like that. That way for an animation you could just set the spriteRenderer.sprite = spriteSheet.sprites[1], then swap out the sprite sheet with a different file if you wanted the character to change look.

1 Like

So I created a simple script that will cut a texture sheet into a grid whenever a new Texture2D is placed down. I’m not sure how optimal or expensive this is on memory and processing or how it would affect draw calls. Also this solution would only work with a system where files were cut into grids. I feel like there has to be a way to access the cut sprites from the Texture2D or generated atlas from code…

#pragma strict

@script ExecuteInEditMode

var sheet : Texture2D;
var spriteCount : Vector2 = Vector2(3,4);
var spriteSize : Vector2 = Vector2(32,64);
var pivot : Vector2 = Vector2(.5,1);//Bottom middle;
var sprites : Sprite[];
var density = 32;

private var oldSheet : Texture2D;


function CutSheet()
{
	if(sheet!=null)
	{
		sprites = new Sprite[spriteCount.x*spriteCount.y];

		var i = 0;

		for(var y = 0;y<spriteCount.y;y++)
		{
			for(var x = 0;x<spriteCount.x;x++)
			{
				sprites[i] = Sprite.Create(sheet, Rect(x*spriteSize.x, (spriteSize.y*spriteCount.y)-(y*spriteSize.y), spriteSize.x, -spriteSize.y), pivot, density);
				sprites[i].name = i+"";
				i++;
			}
		}
		oldSheet = sheet;

	}
}

function Update () 
{

	if(oldSheet!=sheet)
	{
		CutSheet();
	}

}
1 Like

Hey Rstorm, here is my two cents. I think I have the same problem as you. But I’m sort of wary about posting this reply under your title because you’re request “Sprite Sheet swapping for characters and backgrounds” is not very practical and yet misleading. Let me explain.

In this hypothetical game I’m implementing characters in isometric view with eight-way movement. So that means that our character art needs to be created according to certain standards. For eight-way motion we need to create eight animations for each action. North, North-West, West, South-West, South, South-East, East, and North-East, anti-clockwise, in that order. Now you’re talking about maybe 5 sets of animations with 8 different directions for each character. That’s already 40 animations for only one avatar. Now suppose someone wants to change his identity with different clothes weapons etc. You’re talking hundreds of sprite sheets that could be downloaded dynamically, but the graphics workload just keeps increasing with every tiny adjustment in appearance, color and detail. Really it just doesn’t make any sense to go that route. Instead a non-sprite sheet atlas animation is a beautiful solution. The advantage of using a 2D bone skeleton with Unity is that there is much more freedom to script. Disadvantages are the draw calls. The Unity Smooth Moves - 2D Skeletal Animation Asset developed by Layne Bryant\echo17 is an excellent tool, and could potentially be a solution to our problem. There are plenty of YouTube tutorials that show exactly how to equip and swap sprites from a 2D character. That has the potential for a character customization workflow like the one you see here. This is a screenshot from a 2D Online RPG called Dragon Fable. It’s a nicely detailed illustration of what an avatar character customization panel could look like inside Unity.

Anyway, I have been scouring the internet for a few weeks and have found bits and pieces of information on this topic. It seems that the concept of 2D Character customization functionality is a bit foreign to unity developers. There’s very little support on the forums assets and other resources to get this job done. Plus Unity is not designed for this and you need to be a sharp nugget to find a loophole. Here are a few fragments of information related to the subject that I’ve found. True, this is a slightly complex concept that’s not native to the average developer, rather a flash animator. You see, Adobe Flash CS6 could pull this off easy with some AS3 (Action Script 3) required. You don’t use a sprite sheet with hundreds unnecessary images that’s condensed someway using code. In flash you use the power of the 2D bone Skeleton tool and inverse kinematics. This mature technique is an awesome tool to position and rotate parts of the body much like Smooth Moves. The avatar graphics are flexible. Different directions animations and each part are put into different layers. Each layer has its own animation timeline for each part of the avatar. All eight directions are placed into the same place with overlap shown in the following.
1453532--78734--$Untitled.jpg
Then we display each direction by hiding the others using Script. This setup makes the direction switch as simple as turning on and off the visibility instead of replacing a million sprite sheet images. Bones and texture swaps allow more flexibility with Script and are more practical for the game player who wants to change the appearance of his character.

I wonder is there new possibilities in the newer version of Unity since then for 2d clothing and appearance?

Hi, everyone I I’m creating a 2D game where there are different backgrounds so I need to know about a feature where the different backgrounds can repeat randomly.

1 Like