1
\$\begingroup\$

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()?

\$\endgroup\$
2
  • \$\begingroup\$ Are you talking about an Editor script to help you author the game, or an in-game script running during play? \$\endgroup\$
    – DMGregory
    Feb 2, 2018 at 14:30
  • \$\begingroup\$ An in-game script \$\endgroup\$ Feb 5, 2018 at 12:56

1 Answer 1

1
\$\begingroup\$

"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.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .