Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Combine x sprites into one

Discussion in '2D' started by sly_88, Jun 5, 2018.

  1. sly_88

    sly_88

    Joined:
    May 8, 2018
    Posts:
    23
    hi guys,
    do you have any idea how combine x sprites (Sprite Renderer) into one big sprite?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,606
    Well, that depends on what you mean by that.

    The easiest (if it works for your needs) is to just make an empty GameObject, and then drag your several sprites into it. Then forget about the child objects, and only manipulate the parent GameObject.
     
  3. sly_88

    sly_88

    Joined:
    May 8, 2018
    Posts:
    23
    I would like to combine all sprites (for example all grounds tiles) into one big, you know what I mean?
     
  4. sly_88

    sly_88

    Joined:
    May 8, 2018
    Posts:
    23
    I saw code to create a mesh from sprites but works only in the editor I would like to find a solution to create it in real-time
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,606
    Why is it important that it be a single mesh instead of a GameObject that contains children? What is it you're actually trying to achieve?

    If you explain your purpose, we can probably suggest a good solution. If you keep us in the dark, then not so much.
     
  6. sly_88

    sly_88

    Joined:
    May 8, 2018
    Posts:
    23
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,606
    Oh, haha, it so happens I'm in that thread too. :) No, combining your sprites into one big image will not help with that at all.
     
  8. NitromeSteed

    NitromeSteed

    Joined:
    Jul 28, 2017
    Posts:
    17
    I myself need a single sprite that is created from many small sprites. 1000s of sprites that will not move. Merging them into one is the actual solution to this problem. Many of them are layered in confusing ways. This is for a port of a game, redoing the artwork is non-negotiable.

    I really wish someone would have answered this question instead of frustratingly idiotic: "why would you want to do that?"

    Who cares? Just answer the godamned question for the sake of science. It doesn't matter that it's the wrong approach, at least people visiting this thread in the future will learn something. Instead of getting somewhere I've just wasted my time.
     
    sebastianmol1996 likes this.
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,606
    I did that in my first reply.

    If the question isn't specific enough, you shouldn't be surprised if the answer doesn't fit your needs. And if the people trying to help try even harder, by asking for more specifics (this is where "why" often matters), that's a good thing — cursing at them is hardly going to make things better.

    And in this case, the OP was mistaken about what he needed, as we quickly uncovered. You're the one who chose to necro an old thread with a different need (that might happen to match what what you think the OP was asking for, even if it wasn't what he actually needed). That doesn't change the fact that this thread served its purpose, and the OP was steered in the right direction.

    Even in your case, I doubt that your belief about the correct solution is actually the best one. If you have 1000s of sprites that don't move, then if you simply (1) mark them static, and (2) make sure their textures are available for atlasing, then Unity will take care of the rest for you. But if you really want to work hard at it for reasons you haven't told us, then read up on the Texture2D and Sprite classes so you can do your own manual atlasing or draw entire layers into one giant texture or whatever it is you want to do.
     
    vakabaka likes this.
  10. NitromeSteed

    NitromeSteed

    Joined:
    Jul 28, 2017
    Posts:
    17
    How?

    I cannot mark the textures static because they are fetched dynamically at runtime. I cannot atlas at runtime. This is not a 3D scene that I'm statically loading. It is a complex layer of sprites that are assembled according to rules within a json file from a Flash game. I have to deploy to mobile and several old systems so I wary of shader solutions.

    I've looked on the internet and all I've found is this thread. I was forced to write my own pixel copying method:

    https://forum.unity.com/threads/merging-multiple-textures-sprites-into-one-texture.583393/
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,606
    Ah, you didn't mention that the textures were loaded at runtime. That does indeed narrow your options. And I was also not clear that the task was flattening overlapping sprites; I was picturing something like a bunch of sprites spread out over a large area, which you wanted to combine into one giant sprite.

    So yeah, pixel-by-pixel copying is basically what you want here, assuming none of the layers are scaled or rotated, so that a pixelwise copy is both easiest and best fidelity. (There are some off-the-shelf utilities that make this easier — my PixelSurface asset is one — but as you found, the code for it isn't too hard to write.)

    If any of the layers are rotated or scaled, then the manual pixel blitting gets complicated fast. At that point I would probably instead use a camera and a RenderTexture. It's a bit of a pain to set up, but works very quickly (as it uses the GPU) and supports all the same transformations as drawing to the screen.
     
unityunity