6
\$\begingroup\$

I have a GameObject that I want to have multiple sprites. I tried adding an extra Sprite Renderer, but it tells me I can't add the same component multiple times.

I'd really rather not have to have four GameObjects just to display this thing correctly.

For example, I would want to have a tile background, a tile artwork, a tile character, and a nice shine on the top. If I were to make that all a single image, I would have to export nearly a thousand images to get all possible combinations.

Tile Background: Tile Background

Tile Artwork: Tile Artwork

Tile Character:Tile Character

Shine (Barely visible): Tile Shine

Everything put together how I want it in-game: Final Image

\$\endgroup\$
  • \$\begingroup\$ Is a prefab using the built in layering of objects out of the question? \$\endgroup\$ – SpartanDonut Mar 27 '14 at 20:54
  • \$\begingroup\$ I don't think so. The tile object is currently a prefab but I'm not sure how to get multiple sprites in there to be layered. \$\endgroup\$ – Califer Mar 27 '14 at 20:55
  • \$\begingroup\$ To clarify, I'm not quite sure what you just said. \$\endgroup\$ – Califer Mar 27 '14 at 20:59
  • \$\begingroup\$ I'll put together a more complete answer later tonight if no one beats me to it, but in short to get you going, using the list of game objects you can just drag and drop them onto each other to arrange a hierarchy of the objects. When you have it all set up you can create a prefab from the root object and all the child objects become part of the prefab as well. \$\endgroup\$ – SpartanDonut Mar 27 '14 at 20:59
  • \$\begingroup\$ So, in the end each tile would actually be 4 game objects? \$\endgroup\$ – Califer Mar 27 '14 at 21:01
8
\$\begingroup\$

Yes, you'll need to have a single game object for each sprite, but you'll only have as many game objects as you have layers.

So you'd create a game object with the following children (and save it as a prefab): Icon game object

You can then have a script that changes the sprite for each layer:

public class Icon : MonoBehaviour
{
    public SpriteRenderer artwork;

    public void SetArtworkSprite(Sprite art)
    {
        artwork.sprite = art;
    }
}

The Icon script would be in the Icon game object and would have all its fields linked with the child game objects. So then you can have an icon generator script as so:

public IconGenerator : MonoBehaviour
{
    public Icon iconPrefab;

    public void CreateIcon(...)
    {
          Icon newIcon = Instantiate(iconPrefab) as Icon;
          newIcon.SetArtworkSprite(...);
          newIcon.SetBackgroundSprite(...); 
          [...]
    }
}

If you REALLY need everything to be a single game object, than I believe you can achieve this by creating a shader that takes X textures and renders them. But I'm not sure if it's worth it.

\$\endgroup\$

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.