Search Unity

Sprite Packer

Discussion in 'Assets and Asset Store' started by Darkcoder, Dec 14, 2013.

  1. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Sprite Packer is already fully compatible with Unity 5. But just in case, I submitted a new version using Unity 5. It should probably be online in a week :)
     
  2. rafaellop

    rafaellop

    Joined:
    Feb 4, 2015
    Posts:
    26
    Great, thanks for quick answer. By the way, one more question:

    I've got most of my images in the Resources folder and I load them when needed using the method: ...GetComponent<SpriteRenderer>().sprite = Resources.Load( ... , typeof(Sprite)) as Sprite;

    Will your spritepacker be useful for me in that case? I'd like to limit the drawcalls I've got but still benefit from the Resources folder and not modify my code a lot. Any ideas? :)
     
  3. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    You can still use Resources with an atlas, it just requires you to use Resources.LoadAll, and manually go through the returned array to find the specific resource you want. However, in your scenario I think it's best to make a simple component that stores references to your sprites, and you simply access/copy them from there when needed, as I'm pretty sure Unity will only copy the actual texture data when you go to render it.
     
  4. febix

    febix

    Joined:
    Jun 2, 2013
    Posts:
    12
    Hello! Tool is very helpful for me and very simple to use. Thank you very much! :)
    I have only one problem. When I link sprites, all sprites replaced by Atlas’s sprites in SpriteRender components of each Gameobject in my scene. But when animation clip has sprites (single type, not multiple) in its timeline - it still uses the old sprites, not from the Altas. How can I replace my sprites in animation? Only manually?
     
  5. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Yes, I forgot that AnimationClips can store sprite references, so you'll have to replace them manually. If you don't mind waiting then I can add this feature tomorrow.
     
  6. febix

    febix

    Joined:
    Jun 2, 2013
    Posts:
    12
    It will be great! Of course, I'll wait until the next update. Thank you for quick answer. :)
     
  7. MaxEden

    MaxEden

    Joined:
    Jun 22, 2013
    Posts:
    82
    Hi! Just checked new atlas system, looks like padding settings are applying only to new added sprites.
     
  8. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I'm not sure I understand, can you give me a step-by-step guide to reproduce this issue?
     
  9. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    Purchased. Just after 10 minutes, I can understand its settings and how the system works. I must say, it's much better than the original Unity Sprite Packer :)
     
  10. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Thanks! The built-in packer feels like I don't have much control over how things are packed, so I'm happy with how I implemented this :)
     
    ikemen_blueD likes this.
  11. MaxEden

    MaxEden

    Joined:
    Jun 22, 2013
    Posts:
    82
    Sorry for bother, just mixed up two atlases :)
     
  12. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    Hi @Darkcoder ! this work with TGA files? or only with PNG?
     
  13. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    The input textures can be any format (as Unity converts them all to Texture2D). Though the output texture is always .png, but again Unity converts that to Texture 2D so it doesn't matter anyway.
     
  14. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
  15. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    Hi @Darkcoder,
    I buy the SpritePacker asset but, Im a little new with this of the Draw calls. I use in my actual project(all image & textures) and this is the result:


    But i dont see to much difference if you can explain me.
    The draw calls is the "Batches" or the "SetPass"?.
    I also noticed that after applying the SpritePacker my images reduced their quality, so do not know if applied correctly
     
    Last edited: Jul 2, 2015
  16. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    A SetPass call means the current shader was changed when drawing the next mesh. So if you have a scene with 100 meshes that share the same shader, you should see 1 SetPass call, and between 1 to 100 draw calls and batches (depending on triangle count). If you have a scene with 100 meshes that have their own materials or shaders then you will see 100 batches and 100 SetPass calls.

    Your scene has 28 batches and 28 SetPass calls, this means each of your sprites has its own material and/or shader, so it's not possible to batch between them. To lower the amount of batches you need to share some materials where possible.

    If you look at the Sprite Packer example scene you can see it's 1 draw call, because all the sprites in the scene share the same material and shader.
     
  17. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    Hi @Darkcoder, but i create the big atlas and I link too in that test,
    Look this is my big atlas:
     
  18. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Did you click the Link button in the atlas to replace your old sprites with the new atlas ones?
     
  19. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    yes I did that too
     
  20. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    I have test moving all the old sprite out of my project too.
    Also I have the same problem if I use the new atlas at the scene demo "One Draw Call", look:
    http://i.imgur.com/z8IWPNV.png

     
    Last edited: Jul 2, 2015
  21. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Can you e-mail or PM me your project or Unity package with an included demo scene that demonstrate this issue?
     
  22. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
  23. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Yes, please.
     
  24. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    I have already sent you :)
     
  25. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Thanks. I checked the demo scene and it's working as expected.

    The reason why you get so many batches is because you're mixing sprites from two atlases (the demo sprites, and your sprites). In your example scene you placed all your sprites on top of the demo sprites, giving them the same z position, but due to z-depth precision, the final draw order of these is essentially random, so Unity is constantly switching between drawing from different atlases, which breaks batching. To fix the demo scene just move all of your sprites toward the camera, and you'll see the batches quickly goes down to 4 or so.
     
  26. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    but what about the UI animation @Darkcoder ? in the uUI the elements are in the z position I mean in the same level..
     
  27. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    The UI doesn't use z position for draw order so it's not an issue. Also, as I said this issue only happens when mixing atlases, if you group your UI elements together, or try to use the same atlas for everything then it will work fine.
     
  28. Mr-Stein

    Mr-Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    Hi @Darkcoder, I found my problem with the Draw Calls.
    I noticed that the UI->Text I had inside of the UI->Image(like button) are generating many draw call in my scene.
    I made an example look:



    Maybe can be a good feature in the future for Sprite Packer if can find all the UI->Text in the current Scene and convert-replace that UI->Text to UI->Image or directly generate to the big Atlas
     
  29. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    That would only work for specific font sizes, with specific characters, and for specific font styles. By default the Unity text uses a dynamic font, so you can change the text size, style, etc, and use any supported Unicode character. Baking that into an atlas would be very limiting, or take up a lot of space, so it's not something I plan to include.
     
  30. shebangers

    shebangers

    Joined:
    Jun 12, 2012
    Posts:
    12
    Can this tool do extruding like TexturePacker or similar programs? I need such a process for 2d tiles.

    Edit: Just watched your video the whole way, looks like you have this covered with the clamp setting. Is there a way to do that to all the tiles in a set, or easily select a bunch of them and apply that setting to them? I have a lot of tiles that would need it.
     
    Last edited: Jul 21, 2015
  31. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Yes, the Pad Size setting allows you to set the extrusion. This is set on a per-image/sprite basis, but you can also change the default pad size so any new images/sprites you add automatically get your desired setting.
     
  32. shebangers

    shebangers

    Joined:
    Jun 12, 2012
    Posts:
    12
    Do you have any intention, or would you be willing to add a feature to change the output filename? Kind of trivial I suppose. I was using GridEditor2d (http://forum.unity3d.com/threads/re...-innovative-and-flexible-level-editor.319723/) and it seems to expect a .png file extension on the sprite sheets.

    Luckily your plugin comes with source and it was easy enough to just change it in your code and reimport, but would be a nice feature for situations like that.
     
  33. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Haha, I have no idea why I set the file extension to bmp. The image itself is encoded to png, so changing the ".bmp" to ".png" on SP_Atlas.cs line 150 will work fine. I'll submit an update now, thanks!

    NOTE: If you already have atlases that have this .bmp extension, then you need to rename that to png, as well as the .meta file, and it should import fine.
     
    Last edited: Jul 22, 2015
  34. Ortath

    Ortath

    Joined:
    May 22, 2014
    Posts:
    7
    You have a conflict with the asset "Pixel Perfect". You both define the same global named "HSBColor". Please make it a unique name if its global.
     
  35. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    There is no 'HSBColor' type defined in Paint in 3D. Plus, all types defined in Paint in 3D are prefixed with P3D_, so doubt there would be any such conflicts.

    If this 'Pixel Perfect' asset defines some type in the global namespace that matches a type in one of the Unity namespaces that I use, then it's possible my code could be referencing the wrong one. But in either case, I've never heard of any HSBColor type, so I doubt it's due to my code.
     
  36. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    101
    Hey this looks great! Does this works with Unity Indie version(using 4.6.7p2) or requires pro?
     
  37. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    It works with the free version.
     
  38. Yangsheng-Cheng

    Yangsheng-Cheng

    Joined:
    Apr 3, 2015
    Posts:
    7
    Hey @Darkcoder , is not Custom Border supported? I read the source code and found the variable has no reference.

    Someone had asked you to implement keeping not only the pivot but also the borders of the sprite. As I understand, Keeping the borders of the sprite means that sprites keep original size after trimmed and packed. But actually it is not. I can not use anchors to stretch UI sprites, sprites always become bigger. This problem is hurting me so much.

    I used Texture Packer with Cocos2d-x, it works as I expected. Sprites keep same size after trimmed and packed. Could you please support the feature? Sorry for my bad English.
     
  39. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    You're right, I forgot to implement the Custom Pivot & Border settings. This has now been added and will be included in the next version.

    However, the sprite pivot and border settings currently do transfer over as expected. For example:



    At the top you can see an unpacked UI sprite with a border of 10,10,10,10 set.
    At the bottom you can see the packed result in Sprite Packer. As you can see, it maintains the 10,10,10,10 sprite border and will work as expected in your UI.

    The Custom Pivot & Border settings are only designed for situations where your input textures are not sprites. For example: If you add a normal Texture2D to Sprite Packer, it will have a default pivot of 0.5,0.5 and a default border of 0,0,0,0, because Texture 2D has no pivot or border data. So this setting was designed to allow you to customize these default settings. For UI images and such you should be making sprites, so this setting (while currently broken) is of no use anyway.
     
  40. Yangsheng-Cheng

    Yangsheng-Cheng

    Joined:
    Apr 3, 2015
    Posts:
    7
    Thanks for your quick reply.

    I misunderstand the Custom Border. The border settings(9-sliced sprite) of sprites for UI work fine.

    My problem is about sprite size. For example, a 100x100 sprite may be 74x74 after trimmed, but I want the sprite size is still 100x100. Sprite Packer re-calculated pivot to keep the sprite be placed at a proper position after trimmed. Mostly this is enough. If use anchor (0,0,1,1) to scale sprite, the trimmed sprite will become bigger, see the attached images.

    Is it possible to keep sprite size be same after trimmed it? As I know Texture Packer for Unity does not support the feature too though It supports in Cocos2d-x.
     

    Attached Files:

    Last edited: Sep 14, 2015
  41. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I see, in this situation you have to disable trimming in Sprite Packer. This is because the new UI system doesn't know how big the sprite originally was (there's no way to set it in the SpriteMetaData). If you need trimming then you should place the button icon as a child of your button and set it to its native size. However, even without trimming Unity should automatically 'trim' the sprite so it doesn't render the transparent edges, but it will still take up space in the atlas.
     
  42. Yangsheng-Cheng

    Yangsheng-Cheng

    Joined:
    Apr 3, 2015
    Posts:
    7
    I found a workaround. Original size is useful to align the UI elements, especially for multi-resolution. I calculate the localScale of sprites from script, and it works ok.

    Thanks again, Sprite Packer is a great tool, it saved my project from the hopeless situation. The built-in sprite packer of Unity is impratical, IMO.
     
  43. febix

    febix

    Joined:
    Jun 2, 2013
    Posts:
    12
    Hello Darkcoder! Could you please fix one sprite link problem. When I press "Link" in SpriteAtlas - everything link except "Pressed Sprite" in UI Button. Maybe this bug also take place in other UI elements with image inputs...

     
  44. Gozdek

    Gozdek

    Joined:
    Jun 21, 2015
    Posts:
    356
    Nice job.
     
  45. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Unity seems to like inventing new ways to reference objects :D Each new way requires special code of course. I'll see what I can do!
     
    febix likes this.
  46. rollingdice

    rollingdice

    Joined:
    Jun 8, 2015
    Posts:
    2
    Hi. Whether it is possible to add a setting to set the maximum size of the atlas? 8k too much for mobile devices.
     
  47. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    The atlas will automatically resize based on your source textures. So if you're seeing an 8k texture then you have too many source textures, or they're just too big. To fix this consider using multiple atlases, or remove your very large source textures (e.g. background images).
     
  48. rollingdice

    rollingdice

    Joined:
    Jun 8, 2015
    Posts:
    2
    I understand, but if the amount exceeds the 8k atlas Sprite Pasker automatically splits the result of several atlases. It is very convenient, but I would like to set the maximum size of the generated atlas. For example, I have a directory with many sprites. I would like to get some atlases "Sprites 1", "Sprites 2" ... etc, each with a size of no more than 4K.
     
  49. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Divide your sprites evenly into two atlases then. This could be made automatic, but doing so will probably give you more draw calls than you need, as there's no way to know how you're going to be using each sprite, which scenes they're going to be used in, which sprites appear with which others, etc.
     
  50. steddyman

    steddyman

    Joined:
    Apr 10, 2009
    Posts:
    253
    This is an almost perfect asset that does a great job. However, I have found one big issue that caused it to completely break my game and I don't know if there is anything you could do to fix it.

    The issue is if your sprites have different Pixels Per Unit settings to allow you to scale them without changing the transform, then after you Link the sprites everything reverts to the same size and all your scaling and settings is completely lost.

    Would it be possible to somehow compensate for you? Two options I can think of, neither of which is ideal:
    1. Use the scale factor to scale the size of the bitmap in the atlas proportionally.
    2. Set the transform on linked objects and prefabs to a corresponding value to recover the lost Pixels Per Unit.

    Neither option is an ideal solution. For the following reasons:
    1. You would lose the pixel perfect nature of the sprites and take up more space in the Atlas than you strictly need to.
    2. The transform scale of prefabs is not use on Instantiate.

    Thanks.