In the import settings for my sprite image I can select a dropdown box titled "Sprite Mode" and I can change it from Single to Multiple.
Is there any way to select "Multiple" if I'm creating a Sprite within a C# script with Sprite.Create()
?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityIn the import settings for my sprite image I can select a dropdown box titled "Sprite Mode" and I can change it from Single to Multiple.
Is there any way to select "Multiple" if I'm creating a Sprite within a C# script with Sprite.Create()
?
"Multiple" as a mode doesn't really mean anything at game runtime. It makes sense in the editor for asset management purposes, being able to group sprites from the same source asset together in the project folder & inspectors.
At runtime though, you're only working with the individual sprites. So you'll have more than one sprite referencing the same source texture, but each of those sprites doesn't need to know about its neighbours. They're individually created the same as if they were the only sprite on that texture.
So, we can create as many Sprites as we want out of a single spritesheet texture using Sprite.Create:
public static Sprite Create(Texture2D texture, Rect rect, Vector2pivot, float pixelsPerUnit = 100.0f, uint extrude = 0, SpriteMeshType meshType = SpriteMeshType.Tight, Vector4border = Vector4.zero);
Something like this:
public Sprite[] MakeMultiSprite(
Texture2D spritesheet,
float pixelsPerUnit,
params Rect[] regions)
{
var sprites = new Sprite[regions.Length];
for(int i = 0; i < output.Length; i++)
sprites[i] = Sprite.Create(
spritesheet,
regions[i],
new Vector2(0.5, 0.5),
pixelsPerUnit);
return sprites;
}
This generates any number of Sprites from a collection of rectangular regions on a single texture — effectively making that texture act like a "Multiple" mode sprite.