Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

2D Toolkit - 2D in Unity made simple [RELEASED]

Discussion in 'Assets and Asset Store' started by unikronsoftware, Jun 16, 2011.

  1. woko

    woko

    Joined:
    Oct 11, 2011
    Posts:
    23
    Hi,
    first let me thank you for this wonderful tool, bought it today and it's simply great! I'm mostly using it for GUIs, and would like to ask if you're considering adding 9-slice scaling for individual sprites. I have some huge backgrounds which look pretty bad when compressed, and are pretty huge when uncompressed.
    I believe 9-slice scaling should be pretty straightforward to implement and everyone doing some sort of GUI would benefit from it.
     
  2. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Hi,

    Some form of this will be in a later version - what we have in mind (and is half implemented) will be useful for far more than GUIs. I do not have a date for this though.

    Unikron
     
  3. woko

    woko

    Joined:
    Oct 11, 2011
    Posts:
    23
    Thanks for the heads up! Looking forward to it :)
     
  4. gamefools

    gamefools

    Joined:
    Apr 9, 2011
    Posts:
    35
    Hi,

    I've run into a bit of an issue using the Animation Editor. I'm creating a game that has a lot of frames per animation (50-100). So I build a collection with all the animation frames for a character which ends up being about 400 frames over 4 atlases. Now the problem is when I try to create an animation, tk2dEditorUtility.SpriteSelectorPopup() is loading up these 400 frame options into each frame of the of the Animation Editor, which slows everything to a crawl. I've been able to bypass it, by editing the tk2dSpriteAnimationEditor to only call SpriteSelectorPopup on the first frame, and just show a IntField with the spriteId for the rest of them. That way I can select the first frame and click Auto Fill, and not have SpriteSelectorPopup() called on all frames. Just wanted to let you know about this issue.

    Thanks.
     
  5. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    What version are you using? The latest version (1.51) has a preferences menu under the "2d toolkit" in the main menu. In there you can turn off the SpriteThumbnails, which turns off the thumbnails but simply displays the text name of the others - this should be massively faster for you. If you aren't on this version, there was also something else done to massively speed up the search as well.

    unikron

     
  6. gamefools

    gamefools

    Joined:
    Apr 9, 2011
    Posts:
    35
    That did fix the issue.

    Thanks!
     
  7. gamefools

    gamefools

    Joined:
    Apr 9, 2011
    Posts:
    35
    Is there an easy way to switch the sprite collection from code?
     
  8. theHost

    theHost

    Joined:
    Nov 30, 2009
    Posts:
    37
    I've searched but couldn't find anything on this. Is there a way to play around with a sprite's texture coords through script? I am trying to do a parallax effect and would like to 'move' the background by manipulating the texture coords. Note that the background sprites are in a collection.
     
  9. bigdaddio

    bigdaddio

    Joined:
    May 18, 2009
    Posts:
    219
    I add some sprites and get the message some things will not fit try the multiple atlas. I select the multiple atlas box and it never creates the second atlas. I get an error cannot find /path/atlas1.png
     
  10. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Hi,

    You can use
    public void SwitchCollectionAndSprite(tk2dSpriteCollectionData newCollection, int newSpriteId)
    The sprite is an Id for now, but you can convert name -> id in the collection.

    Be aware that changing the collection and sprite can end up allocating memory if the new sprite has more or less polygons.

    If you'd like to do all of this visually, you can use an SpriteAnimation, with clips for each of the sprites you're intending to switch to. For the clip type, set it to Single, and all you'll be doing then is changing to that new sprite. This is much better than the other method, as nothing is hardcoded and everything is set up in the interface.

    unikron
     
  11. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    There isn't a way to change texture coordinates, and there is a very good reason for that - The sprites end up in an atlas, shifting texture coordinates means it will end up sampling the sprite adjacent to it. A quick way to get what you want, is to actually move the sprite, then teleport it to the opposite end.

    unikron
     
  12. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    That is interesting - certainly not seen this one before. Some questions to help me work out what has gone wrong:
    1. Do you have any diced sprites in the atlas? Dicing isn't supported with multiple atlases...
    2. Can you try adding another random sprite and see if it creates the second atlas then?
    3. Can you try increasing the size of your atlas and see if that works?
    4. If all this doesn't help narrow it down, will you be OK to send a test case over? Otherwise I'll have to add debugging code, send it over to you and iterate like that.

    unikron
     
  13. gabidof

    gabidof

    Joined:
    Jun 15, 2011
    Posts:
    14
    You know if we can modify the generated atlas in photoshop as a PVR Texture and then use it in Unity? Because we are having problem with the performance in our game.

    Or why if i put the PVR Texture in the Sprites, the image pixelated so much. I don't know if it's the Sprite generated with the plugin, or the size of the png.

    Thanks!
     
  14. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    I don't think you can edit PVR textures directly in photoshop; I don't believe unity provides any means to reuse them. Perhaps someone else has done more with PVR textures in Unity and could care to comment?

    I do expect to get a lot of pixelation and or artefacts when using PVR though, that is the nature of the compression. In our cases, we usually pack textures with acceptable quality loss in the same Sprite collection, and put the ones which don't compress properly In an uncompressed or 16 bit texture.

    I'm sorry if this isn't the answer youre looking for, but this is how the compression works.
     
  15. oliverdb

    oliverdb

    Joined:
    Jan 30, 2010
    Posts:
    165
    Hey

    Wondering if you have any experience with asset bundles and tk2d. I am a bit worried about adding assets that refeeres to sprites not in the main build?
     
  16. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Hi Oliver,

    I have not had any experience with asset bundles tk2d - with the iOS projects we have done, none of them have required the feature. I will look into it for you, but I am quite busy now, it might be a while before I get around to it. If you do try it and find out any best practices before I get around to it, I would appreciate it if you could post in the thread for the benefit of others...


    Cheers,
    unikron
     
  17. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    Hey I am just wondering about it, every time I use animated sprites with a collider the collider will be totally off with z position at -100000, any reason why that is? Is this required or possible to fix somehow? I worked around it using child objects and adding a collider to them. Shouldn't be this way, should it?
     
  18. AdamSurfari

    AdamSurfari

    Joined:
    Oct 22, 2011
    Posts:
    5
    How is the performance in iOS and Android?

    Can you send me example or apk files for me to run?
     
  19. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    How have you set up colliders in this case? The collider appearing at -100000 should only happen when a sequence includes frames with and without colliders, colliders are moved far back when they are inactive.
     
  20. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Performance has been more than adequate for us on iPhone, even iPhone 3G - you run into limits with GameObjects much sooner than with the sprites. The cost of drawing the sprites is largely limited by the dynamic batching performance in unity. The static sprite batcher let's you get even more performance out of it by building composite meshes for backgrounds - this ends up being very efficient.

    I will look into creating an example project to run, still unsure of which demo to build to demonstrate performance - all the demos included in the toolkit should run really really well on all iOS devices, I can't say for certain how well it'll perform on android as we haven't tested on enough devices yet but from what I've seen the performance is comparable.
     
  21. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    hey thanks unikron, I will ask our level designer to check it out :) thanks a lot
     
  22. oliverdb

    oliverdb

    Joined:
    Jan 30, 2010
    Posts:
    165
    This is probably a stupid question. But what is sprite dicing, cant find any explanation :confused:
     
  23. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Sprite dicing is primarily for backgrounds. When enabled, instead of generating one poly per sprite, the sprite is diced up into smaller parts. At this point, all the empty parts of the texture are discarded. It often saves massive amounts of texture space and fillrate when your sprites have large amounts of blank space (0 alpha) in them.

    Check out demo #1 and select the grass sprite in the hierarchy - thats an example of a diced sprite.
     
  24. AdamSurfari

    AdamSurfari

    Joined:
    Oct 22, 2011
    Posts:
    5
    Thanks for your reply. If there are games on the market/appstore made with this tool that I can test that would be enough for me. My only requirement is that I need to have multiple sprites on screen at the same time, 30+ all animated, plus GUI elements and I would like to pick a tool that can handle that without having to worry about it.
     
  25. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    I don't know about other games on the AppStore made with this, and ours are contract based, and we're not allowed to name names. I'll build you a quick demo with a bunch of animated sprites the next time I get a chance and send you the xcode project if you're interested in testing it out. Drop me a pm with your email address if you are.
     
  26. AdamSurfari

    AdamSurfari

    Joined:
    Oct 22, 2011
    Posts:
    5
    Hi,

    Just the packages for Android or iPhone would be fine, so I can test the performance. Thanks! Ill PM you
     
  27. namoricoo

    namoricoo

    Joined:
    Apr 14, 2011
    Posts:
    534
    Can Anybody let me know what's the difference between UIToolkit and 2DToolkit. How easy would it be to transition from UIToolkit to 2DToolkit. I'm looking to improve my workflow so that I don't have to keep switching to texture Packer and putting everything on one file. The Videos on the first page have no sound.
     
  28. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    Heya, I have checked it now. I do add the Collider to the Sprite which I created using the component menu -> physics -> Box Collider
    the collider is applied correctly in the scene, but as soon as I run the game the z position will be set to -100000 and all other values for position and scaling will be 0.
    Do you know how I can fix that? Thanks in advance!
     
  29. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    You can simply add a Box collider to the sprite in the sprite collection editor - everything should then work as expected.
     
  30. cyro_349

    cyro_349

    Joined:
    Sep 29, 2011
    Posts:
    6
    Hey Unikron,

    Random question, are the Sprite Collections setup to remove their image dependencies when building? I'm working on a game and RAM has become an issue, and looking at the Sprite Collections they have dependencies to all of the images which they are using.

    This is a issue as Unity copies all referenced files into the Resources folder on build, and that folder is loaded into memory when loading (So it's loading the normal images the atlases into memory). Just wondering if I'm doing something wrong on my end, or if this is a known behavior?
     
  31. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    thanks unikron, that fixed it
     
  32. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    221
    Just some questions about what are the "best practices" for using this toolkit. Let's assume we have a tile-based, top-down game that we might call "The Epic of Grizelda" that resembles the screenshot attached below. Now, just based on what we have below:

    a) Would you set up this game such that there's an internal representation of the game map, and then update the screen to reflect that internal representation? That would be my first inclination, but if it's possible to let Unity do some of the work for me, maybe...

    b) Would it be sufficient to use Unity's collision boxes to determine when collisions happen between enemies and the hero? How about between the hero and the raised blocks onscreen?

    c) If we layout the level in Unity's editor, what would be the best way to export the level data into data that the game can use? If one doesn't exist, it seems like it would be really good to have a tile map generator component, so that a meaningful set of data can be parsed from the layout of sprites on screen based on whatever respective components they have.
     

    Attached Files:

  33. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Hi,

    This should certainly not happen. The Sprite Collection does indeed contain references to the sprites. However, the SpriteCollection class isn't used by the game - it simple creates the "data" object. The data object doesn't contain any references to the source sprites, and this is what is used while running the game. You can find this by following the data link on the Sprite Collection.

    Are you putting the SpriteCollection into a Resources folder by any chance?

    unikron
     
  34. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Hi,

    a) You can do something like this, yes, though you should use the Static Sprite Batcher, as it will "bake" your entire mesh down and not bog you down with large numbers of game objects. It should be very efficient to do this. The hero and enemy can simply be tk2dSprites.

    b) As above, if you're using a Static Sprite Batcher, or indeed loose sprites, simply set up your collision information on the sprite - this could be a box or polygon. This will all get merged correctly, and everything should just work.

    c) As of now, you'll still have to manually position the tiles, and use the snap to vertex feature to help align them. This will change in the next version, though :)

    unikron
     
  35. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    Is there any way to import a sprite sheet that has empty tiles so that only non-blank tiles are imported. The following 16x16 tile spitesheet has several holes and all the holes show up as valid tiles.


    Alternatively... does anyone know of a good program for slicing up spritesheets into individual images?
     
    Last edited: Oct 30, 2011
  36. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Make sure the sprite sheet has 0 alpha for the blank areas. While 2d toolkit will import them as tiles, they will not occupy any space in the final atlas. Each sprite is tightly packed to the contents.
     
  37. Dabeh

    Dabeh

    Joined:
    Oct 26, 2011
    Posts:
    1,611
    Hey, i've been experimenting with this for the past couple days and it's great.

    My only problem is that if i'm not using my really wide but small resolution in the editor things become extremely zoomed in. As this is a side-scrolling fast-paced dodging experiment it needs a wide view else people will not see whats coming up in time.

    I've thought about offsetting the camera but this is a cheap fix and not as aesthetic.Is there anything I can do to get a more wide view? (I have done some experiments, but objects become to small and thus the background is visible.)
    Is there anything I can do short of having to resize each and every sprite based on the screen resolution?


    Also, each line will be commented. I may post a link to the mini-game and source. It'll have to be polished a little more though. Though it has physics, scrolling background(not animated, but rather it moves as it goes out of camera). Following camera. Powerups, debug, etc. Though I'm not sure how to do this. I will remove the t2dk part of the source code and have them manually add it if they own it?
     
    Last edited: Oct 30, 2011
  38. surfacedstudio

    surfacedstudio

    Joined:
    Oct 30, 2011
    Posts:
    3
    Hello,

    I've worked as a games dev for a while but am fairly new to Unity itself. I'm considering getting the 2D toolkit as it would save me all the trouble of implementing proper 2D sprite support myself - and the tool set does looks pretty neat!
    Just wondering if it's possible to set up custom shaders/materials for the sprites for alpha/additive/subtractive/custom blending modes I might want :) Also, as far as I can tell the source code comes with the toolkit so I could perform custom modifications myself if need be?

    Thank you :)
     
  39. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Yes you can use custom materials and shaders. You also get full source, so you are free to change anything you like.

    Unikron
     
  40. surfacedstudio

    surfacedstudio

    Joined:
    Oct 30, 2011
    Posts:
    3
    Thank you for the fast reply :)

    That all sounds great, can't wait to try it out!
     
  41. Dabeh

    Dabeh

    Joined:
    Oct 26, 2011
    Posts:
    1,611
    Hey, since the sprites are drawn in a bunch and I'm using a sprite collection for a box and I use Sprite.isvisible to see if it's visible. Is there any other way to do this or a feature possibly?(Good suggestion would be that actualyl)
    Something like
    Sprite.isvisible(camera)
    All it would have to do is get the screen height and width and get the camera used and see if it's out of bounds.
    I'd write up a script but most of my stuff is static so I have no need for it. But I'm sure others would.
     
  42. cyro_349

    cyro_349

    Joined:
    Sep 29, 2011
    Posts:
    6
    Nah, I was running a script which changed the compression of the SpriteCollection and built the scene in to an Asset Bundle and it looks like it was having issues performing the compression, texture assignment an build in such a quick succession. I managed to get around it at the moment buy having a script to swap to compressed/uncompressed Sprite Collections then another one to build the atlas bundles. Although it would be nice to set up a build script which would automatically compress and create the asset bundles and build the project to the different platforms I'm targeting.

    But meh, I'm sure I'll figure something out.

    Thanks,

    Cyro
     
  43. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    Hey,

    just a quick question, is there any callback for animated sprites if I don't want them to loop? I'd need some delegate to add a callback method once the animation is possible. Thanks in advance.

    -zem
     
  44. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    You can use this to find out if an object is visible from a camera:
    http://www.unifycommunity.com/wiki/index.php?title=IsVisibleFrom

    Ideally, you should cache the camera frustum planes and not use this function as is, though
     
  45. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    You can use the animation event delegate to get a callback on a particular frame. Check tk2dDemoAnimController.cs, AnimationEventDelegate for a demo. This callback is set up in the animation editor, and is called on a particular frame when Trigger is set.

    unikron
     
  46. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    thanks found it. :)
     
  47. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,064
    Some questions:

    1.

    Hi! say I have textures for 960*640 and 480*320 (retina and non retina iOS), from what I understand at the very beginning one must check and then load a different atlas using

    however, won't that mean that the scale of the sprite will also change, so if I initially have textures for 480*320 I'll have to double the size of the object. Any way to avoid this, or any better way of doing this?

    2.
    also noticed something weird- I have a Toolkit2d button and after a SetActiveRecursively(false) and setActiveRecursively(true) it stops working for some reason

    3. so, say I want a 960x640 and 480x320 atlas, if I just copy the toolkit2d sprite collection and modify the size of the atlas texture directly to be half of the original, will the change stick (until I click comit again, that is)

    4.will copying the atlas and changing the target height to 320 accomplish what I initially wanted in question 3?

    EDIT: Never mind, will post the solution in the coming days! (question 2 still remains)
     
    Last edited: Nov 1, 2011
  48. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    221
    Yeah, that's what I was afraid of. It would be neat if there were an importer for a tile map program like Tiled, such that you can lay out a tile map where the tiles have metadata associated with them. That way you can have your non-visual internal grid-based data created already, then write an editor extention that generates the tk2d StaticSpriteBatcher + Sprites based on the external data...

    Or if you *really* want to keep it all inside Unity, maybe it's possible to make a tile map editor extention for Unity itself, but it would really need to have the ability to assign metadata to the tiles such that we can assign values to the tiles such as "eventTile" or "triggerTile" or "inhabitant" or "treasure"... things like that.
     
  49. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,064
    new question- if the tk2dSpriteCollectionData of a scene is defined in a variable (and the Atlas is in Resources), will the atlas image be loaded into memory because of that?
     
  50. dead1ock

    dead1ock

    Joined:
    Jan 4, 2011
    Posts:
    31
    I seem to be having some issues with the collision boxes. I have made sure the collision boxes look correct in the editor and that they are committed, but I get objects overlapping each other. http://imgur.com/Kdct5

    Both the player and that very top block are rigidbodys and when they fall there is overlap.
     
unityunity