I'm creating NPCs in Unity 2D using C#, and I have 16x16 sprites for body, helmet, clothes and weapon. All these have a tile set which has been split in unity. I'd like to be able to render one of each part together, so I can drag the sprite to the field, and then it will be rendered on the NPC. IE, I drag a helmet sprite into the helmet field and that is the rendered helmet.

I intend on having multiple NPCs, so I'd prefer to minimise the child objects. Multiple sprites will need rendering, however there's only one sprite renderer. Is it possible for me to combine the sprites (in code), and then render the final sprite? Or is there perhaps a better way?

Thanks in advance!


Yes, you are stuck with needing a child object for each individual sprite that makes up your characters.

It's not as tricky as it sounds though.

You can create one generic "Humanoid Character" prefab with child objects like "Body", "Helmet", "Weapon", etc. You can then use this prefab for both PC and NPCs.

Pay attention to Sprites layering. Almost certainly you'll want the "Helmet" sprite to be rendered on top of "Body" sprite.

There are two levels of ordering:

My suggestion is to have the whole Character on the same Sorting Layer. You can then use Order in Layer to sort the individual parts which make up the character.

  • \$\begingroup\$ Thanks for the help, one more thing: Due to the nature of the level, the sprite must be '6' in 'Order in Layer'. How can I order the separate sprites (helmet, body, etc) in the correct order, whilst adhering to the original order in layer number? Thanks. \$\endgroup\$ – Jacob Morris Jun 25 '15 at 12:21
  • \$\begingroup\$ I'm not sure I'd say "stuck" - there are ways to either combine multiple component textures in a shader (though then each character ends up using a separate material & draw call) or dynamically generate a sprite atlas for the character combinations you're using. These solutions are both going to be fussier and less flexible than just setting up a prefab as Mihai-Andrei recommends above. \$\endgroup\$ – DMGregory Jun 25 '15 at 12:32
  • \$\begingroup\$ @Jacob check my updated answer. \$\endgroup\$ – Mihai-Andrei Dinculescu Jun 25 '15 at 12:33

If you create a single material for the sprites, and they use a spritesheet, they should be batched into a single draw call automatically. :)

From the Unity3D answer hub (kindly answered by ivomarel):

Yes, make sure that the sprites are in the same atlas. When you select a sprite, (Texture type should be sprite), look at the inspector and choose sprite mode 'Multiple'. Then, make sure you give the sprite a packing tag. Sprites with the same tag will be packed and batched together as soon as you run it in the Editor.

Note: You also have to set Edit > Project Settings > Editor > Sprite Packer Mode to 'Always Enabled'

  • 1
    \$\begingroup\$ Asker's question doesn't mention draw calls - the concern seems to be the number of child objects in each character, and whether it's possible to render multiple superimposed sprites with a single GameObject. \$\endgroup\$ – DMGregory Jun 25 '15 at 12:27
  • \$\begingroup\$ Ah, I am sorry, you are right. I got tunnel vision when I saw the word "renderer". :) \$\endgroup\$ – Laszlo Fuleki Jun 25 '15 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.