Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

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

  1. Malarhak

    Malarhak

    Joined:
    Apr 17, 2011
    Posts:
    21
    Hello,

    I just bought 2D Toolkit yesterday and it seems pretty cool.

    But I am kind of new to Unity, and even more to this kind of library.

    My goal is to make a 2D tile based game for android tablets.

    I made some tests but I have problems with keeping a pixel perfect effect (I mean, like pixel art) with multiple resolution.

    Could someone explain me the "easy way" to manage the multiple resolutions Android tablets can have with 2D Toolkit ?

    Thanks a lot :)
     
  2. unikronsoftware

    unikronsoftware

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

    This really depends on what you want to do.
    Are you happy having the screen zoom out to be always pixel perfect? I.e. if your native resolution is 320x240 and you run on a screen with 640x480, you will see 2x more data?
     
  3. Malarhak

    Malarhak

    Joined:
    Apr 17, 2011
    Posts:
    21
    Well I didn't notice de tkcamera at the bottom of the doc, which is what I need (this thing is cool) basically if the resolution is bigger I want to be able to see more things (and less if the resolution is lower) to preserve the pixels.

    While I'm on it, I need to have a dynamic cases system in my game (it's a clickable case-based map...) so, how can I get the size of an image in the code ? I didn't see this parameter accessible in the doc. With this I would be able to know how much distance I have to keep between two sprites (not sure I explained well what I want but...)
     
  4. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    You can use tk2dSprite.GetUntrimmedBounds to get the bounds in local space, and then work out the distance to the next sprite. The anchor can be positioned at an arbitrary location, so this is slightly more complex than how it would normally be if the anchors were all at the same location.
     
  5. mobilegamelabs

    mobilegamelabs

    Joined:
    Apr 11, 2012
    Posts:
    26
    Sprites that are put very far away (large position-z) from camera are shown as a "X". What is the problem?
    I have tried to
    - turn on/off Point Sampled in Sprite Collection Editor
    - set Texture to Point/Bilinear

    Thanks.
     
  6. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Can you send a screenshot to support at unikronsoftware dot com demonstrating the problem please? Im not really sure what's going on.
     
  7. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
  8. spartan

    spartan

    Joined:
    Mar 10, 2010
    Posts:
    174
    I'm trying to generate a new sprite collection, when I click on Open Sprite Editor, it opens a new window with a box "Drop sprites here", but how I can drop them if it is on another window and when I go to Project files it lose focus.
     
  9. spartan

    spartan

    Joined:
    Mar 10, 2010
    Posts:
    174
    I've dragged two sprites and it's only showing one. It's really weird, I think that I will rollback to the previous version of 2dtoolkit with the easy original method of dragging sprites to the textures field.
     
  10. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    You can dock the window
     
  11. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    There are a few other advantages to using the new version, mainly being able to multi edit and delete textures directly. If you don't need that, then feel free to roll back to any older version. There is an archive of all old versions on our forums.
     
  12. Konash

    Konash

    Joined:
    Apr 17, 2012
    Posts:
    7
    Hello all.

    I've been using 2d toolkit for a while now and I like it very much. I'm facing a strange problems from a while now.

    Attached is an image of how my games use to look, and how it looks now.

    $mulled-sample.jpg

    As you might have noticed, It's very blurry right now and looks ugly on the mobile.

    I don't know what happened. I'm using unity 3.5.1 right now. But it happened also on 3.5.0 version.

    Any ideas what might be the problem?

    I'm not using "Point Sampled" and I don't want to use it either.
     
  13. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    It looks like the texture has been downsampled / compressed.

    Can you check the Sprite Collection "Compression" settings, and also the Texture ("Max texture size" and "Format")
    If you double click the atlas texture, you should be able to open it in your favorite image editor, and compare the size with the Max texture size parameter.

    Lastly, in Edit > Project Settings > Quality, check that your selected quality level has Texture Quality set to "Full Res".

    If none of those fix it, drop me an email at support at unikronsoftware dot com, and I will be happy to discuss this further.
     
  14. Konash

    Konash

    Joined:
    Apr 17, 2012
    Posts:
    7
    Compression: Uncompressed

    Max Texture Size: 1024
    Format: Truecolor

    Actual Atlas size in an image editor: 1024 Same as the max size parameter.

    Texture Quality: "Full Res"

    All checked, yet the problem is still there. I noticed there in the preview, the textures get different when I start building. It's close to the time I the message "Building Asset0" appears. I'll send you a picture.

    I'll be sending you an email. Thank you for your support.
     
  15. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    This problem was resolved - one of the platform override settings (lowest quality) somehow got changed to half res. Unsure how this has happened, but would have affected anything using textures in Unity.

    Has anyone else has experienced anything like this before?
     
  16. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    I think it happened to me as well. I was testing various 2d plugins the other day, and find out that the sprite created from toolkit2d is blurry unlike the others. At first I thought it's because of my raw asset files, so I change them to psd. Once switched to psd, the other plugins are noticeably sharper, while toolkit2d sprite was still blurry.
     
  17. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Do you still have the source files for this?
     
  18. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    Not anymore, but I can just start another simple project and send it to you. I used FG2D 0.3 and TK2D from the asset store. Do you want me to test with this version or the latest one from TK2D forum?

    EDITED : Never mind, I found it. I'll send it to you right away.
     
    Last edited: Apr 20, 2012
  19. Mikhail

    Mikhail

    Joined:
    Apr 20, 2012
    Posts:
    4
    Hi! I have a problem with my build. I create sprites and sprite animations with script and if I click play button in unity everything is ok. But then I build my project sprites and animations dont work. I try different ways to create
    GameObject go = new GameObject();
    tk2dSprite sprite = go.AddComponent<tk2dSprite>();
    sprite.SwitchCollectionAndSprite(data, 0);
    sprite.Build();

    I try to load collection from Resources, I try to instantiate from a prefab, and in game view everything is ok, but after building it doesnt work. Target platform is PC.
     
  20. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    Hello there I'm loving 2D ToolKit but I'm having a small issue, hopefully one which has a simple solution that I'm overlooking. I'm trying to get my sprite to raise and lower having corresponding animations play respectively. The trouble is I can get them to change to the new clip but they aren't looping through the frames when holding down the left mouse button. Here is my code any ideas?

    Code (csharp):
    1. tk2dAnimatedSprite anim;
    2.     float balloonX = 0;
    3.     public float HighUp = 0.02f;
    4.     public float HighDown = 0.01f;
    5.    
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.         anim = GetComponent<tk2dAnimatedSprite>();
    10.         anim.Play("High Acceleration 1");
    11.        
    12.     }
    13. void Update () {
    14.        
    15.             if (Input.GetMouseButton(0))
    16.             {
    17.                 transform.position = new Vector3(transform.position.x, transform.position.y + HighUp, transform.position.z);
    18.                 anim.Play("High Acceleration 1");
    19.                
    20.                 //accelerating = true;
    21.             }
    22.            
    23.             else
    24.             {
    25.                
    26.                 if (anim.isPlaying() == true)
    27.                 {
    28.                    
    29.                     anim.Play("High Idle Full Health");
    30.                     transform.position = new Vector3(transform.position.x + balloonX, transform.position.y - HighDown, transform.position.z);
    31.                     //accelerating = false;
    32.                 }
    33.             }
    34.              
    35.         }
     
  21. Mikhail

    Mikhail

    Joined:
    Apr 20, 2012
    Posts:
    4
    You play animation from the begining while you holding down the left mouse button that's why you see the first frame.

    if(!anim.isPlaying || anim.CurrentClip.name != "High Acceleration 1")
    anim.Play("High Acceleration 1");
     
  22. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287

    How do you get data? Also, what file do you store in the resources folder?
     
    Last edited: Apr 20, 2012
  23. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Thanks for the source files to reproduce this - As explained in the email, this is caused by a discrepancy with the ortho size and the sprite position you picked, ended up not being on a pixel center.

    A simple solution to this is to use the tk2dCamera, where every round number in world space is guaranteed to be a pixel boundary.
     
  24. Mikhail

    Mikhail

    Joined:
    Apr 20, 2012
    Posts:
    4
    I solved the problem. First of all I used sprite.spriteId = sprite.collection.GetSpriteIdByName("spriteName"); instead sprite.spriteId = sprite.GetSpriteIdByName("spriteName"); I have two prefabs for sprite and sprite animation and both of them for default used collection which not included in build thats why arisen exeption. I switch default collection and animation. Now everything fine. Thanks.
    P.S. I think it would be greate idea to make SpriteCollection without reference to source textures and work only with atlas. For example I have a huge number of images and when switch between platforms Unity crashes. I have to create new project for each platform and initialize animation and sprite collections from the begining.
     
  25. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    That is an interesting request, but a lot of the features would not be usable, which is why I haven't implemented it. One example is sprite dicing. I have had some need for this though, so I am considering it.

    I was thinking of some optimizations to make the SpriteCollection build require significantly less resources, and remove all references to the source textures themselves - would that help in your case?
     
  26. spartan

    spartan

    Joined:
    Mar 10, 2010
    Posts:
    174
    @unikron is there a way to avoid/hide the "divisions" shown of a sprite with dicing when scaling?
     
  27. mobilegamelabs

    mobilegamelabs

    Joined:
    Apr 11, 2012
    Posts:
    26
    Thanks for your response.
    I apologize it was my problem. The images are actually rendered correctly as crosses (my sample placeholders) when they become small. Just because they are rendered as crosses I thought they rendered incorrectly.

    Is there any plan to add more features to the button class? e.g.
    - support Time.timeScale = 0
    - a method to change the shown image

    If you have no time doing so, I may help adding the second feature and releasing a modified one here. I don't know how to solve the first problem.
    Thanks very much for your time.
     
  28. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    When scaling up there should be no seams.

    When scaling down - you can get better results by increasing padding, or setting point filtering. Both can and will fail in various different ways, though.This is inevitable due to how graphics hardware works.Start with some really large padding amount and scale down from there to see what works best for you.
     
  29. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Time.timeScale = 0, that will be fixed in the next release. Drop me an email at support at unikronsoftware dot com to get this script sooner if you want.

    with the second one, do you mean changing it from script?
     
  30. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    242
    Hi, I couldn't find anything in the docs about adding many fonts to a single Sprite Collection. After some trial and error, I came up with the following workflow. Does this sound correct? If so, please feel free to add these steps to the 2DTK docs.

    1. create a new Sprite Collection
    2. open the sprite collection editor
    3. click Create and then select Font
    4. drag the exported fonts .fnt file into the BM Font slot (note I made my font with Glyph designer)
    5. drag the exported fonts .png into the Font Texture slot*
    6. click the Create button in the area that says "A data object is required to build a font."
    7. select a folder for the .prefab
    8. click Commit in the Sprite Collector and now the font should be available on a Text Mesh.

    *Note at this point an additional field appeared in the Sprite Collector that says "the texture importer needs to be reconfigured to be used as a font texture source. Please not that this will globally change this texture importer" along with a button that says "Set up".
     
    Last edited: Apr 22, 2012
  31. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Thats pretty much it - sorry about the lack of docs on that, its been one hectic month. Thanks for the steps, I'll get that added to the docs (and the other undocumented features) as soon as I can together with some screengrabs.

    Edit: did you have any trouble figuring it out? If so, where? I spent a great deal of time working out a clear workflow in the new sprite collection editor, and the idea is there shouldn't be many places things could go wrong, so if something wasn't clear, etc. please let me know so I'll know what to address.
     
    Last edited: Apr 22, 2012
  32. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    242
    Yeah figuring out the correct order was a bit trial and error - like you have to click the Create button before you click the Commit button. First time I tried I did it the other way around which didn't work. On a side note I sometimes forget to press the Commit button at all because it's kind of small and not very obvious.

    I had originally made all my fonts using the old method and I tried to recreate them with the Sprite Collection editor but they weren't being created properly - everything looked fine with the files but the glyphs were solid white in the Atlas. So my workaround was to re-export from Glyph Designer and then make them fresh with the Sprite Collection editor.
     
  33. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Good point about the create button - perhaps I should add some inline messagebox stating that it won't work until the object has been created. I'll get the docs done before revisiting this.

    Thanks for the feedback, its much appreciated.

    Did you get any messages in the console when the fonts ended up solid white? It could be because they weren't marked "Read/Write Enabled" - the "Set up" button sorts that out for you.
     
    Last edited: Apr 22, 2012
  34. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    242
    I did get some messages in the console, but didn't copy them down. But now that you mention it, the "set up" button didn't appear in the cases where the font ended up being solid white. An easy test to reproduce on your end would be to make a font the old way, then delete the data and prefab files that get created with that process (since they get created again in the next step), and then try to add that same .fnt and .png to a sprite collection.
     
  35. mobilegamelabs

    mobilegamelabs

    Joined:
    Apr 11, 2012
    Posts:
    26
    Thanks very much for your prompt reply and solving the problem.
    Yes, I mean changing from script. Moreover, I hope you will add a feature (add a few more public string variables for sprite names of a toggled state) to toggle the state of the button that, for example, when a "Pause" button is pressed (stays as pressed until clicked again), it's shown as "Play" and when it's released it's shown as "Pause", so that I don't need to change the shown image from script.

    Does global changing in Prefab not work (propagate) for the parameters in AnimatedSprite in Inspector?

    Thanks very much for your time!
     
  36. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    That worked with the fonts I tested, but those were pngs from BMFont - I'll test this using GlyphDesigner next time I'm on my mac. Thanks for taking the time to report this.
     
  37. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    I'll put that in the TODO, but I don't think it'll be done in time for 1.70.
     
  38. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    Oooooh I see now thanks heaps :D
     
  39. Uttpd

    Uttpd

    Joined:
    Feb 27, 2010
    Posts:
    114
    Hi, I´m trying to decide wish framework to go with. I´v made a thread on the gossip part, wish got buried, explaining my needs

    Basically I want to stack images (finished) on top of each other, aplly colliders where do, and work out the logic (dont want to mess with shaders and animation inside unity, at list not for now)

    Don´t want to clutter the thread so after the jump...

    would you say 2dToolkit is for me? Do you foresee any shortcomings?
    thanks
     
  40. sp00ks222

    sp00ks222

    Joined:
    May 12, 2011
    Posts:
    37
    How do you add in multiline text from code? If I have a string "First line\nSecond Line" and add it to the text mesh it doesn't seem to work :(
     
  41. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    I think 2D Toolkit could work for you, but I'll go through each of your points to tell you at least what to expect.

    2D Toolkit can't use TexturePacker spritesheets, so you'll need to import from individual sprites. Regularly spaced tiles work too. PNG transparencies should just work - you can test this by simply dragging your texture into unity, if the transparency appears correctly in there (it should), then 2D Toolkit will read and use it fine.

    I don't understand your question about colors/transparencies changing. The example you've shown is predominantly in black and white, so where do the colors / alpha come in? Our built in shaders allow you to tint instances of sprites so you can say make the hat 50% visible no problem at all without messing about with the atlas.

    Colliders can be generated automatically per sprite, and then should follow the animation, but the only type of collider which you can animate like this are boxcolliders to avoid creating and deleting components dynamically. In the current version you can't rotate box colliders relative to the sprite so that limits what you can do.

    The environment layering should just work, can't see any problems with that. You obviously understand that you'll need multiple layers for these textures / shadows / etc. Shouldn't be any problem. You simply position these at different z layers, and it'll just work fine. Its probably best to avoid perspective cameras, and favour the parallax script in our private support forum should you need that. It looks like you may not from the images though.

    One thing you didn't mention was what platform you're targetting. The number of layers you're describing there should be fine on PC, but I'd be very cautious trying anything like that on any mobile platform.

    Hope that helps, if you have any further questions, feel free to ask.
     
  42. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Can you try this: In tk2dDemoAnimController, line 27 - change it to
    popupTextMesh.text = "First line\nSecond Line";
    and run the demo (5 - animation).

    I've tried this and it definitely works. I'm just trying to narrow down what could've gone wrong on yours.
     
    Last edited: Apr 23, 2012
  43. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Depending on where you are pulling the "Firstline\nSecond Line" from in code you may need to use a different value than "\n". For example, I pull a lot of my string values from XML and they are auto-escaped. Instead if "First line\nSecond line" I use:

    "First Line Second Line" when pulling from XML.

    Sorry, the text editor is not agreeing with me.

    # 10 ;
    ^^ remove the spaces and you've got yourself the XML New Line
    # 13 ;
    ^^ remove the spaces and you've got yourself the XML Carriage Return

    If that doesn't help, mind showing us exactly how it's being used?
     
  44. unikronsoftware

    unikronsoftware

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

    I've tried to reproduce the problem, but have been unable to so far. I tried this with a font generated from Glyph Designer, and I definitely don't get any errors at all. Please keep an eye out for if this happens again - I would very much appreciate if you could send me a broken test case if at all possible.

    Cheers,
    unikron
     
  45. Uttpd

    Uttpd

    Joined:
    Feb 27, 2010
    Posts:
    114
    Thanks, following
    Targeting PC Win, but ill want to translate to mobile devices as part of my learning path
    It will be orthographic top down camera only, no parallax.

    The ability to work with sprite sheets would really help my workflow, have to see if i can work around it.
    anyway leaving my suggestion to look in to it http://www.texturepacker.com/texturepackertutorials/
    http://www.rocket5studios.com/tutorials/make-a-2d-game-with-unity3d-using-only-free-tools-part-2/

    Colliders
    rectangular only i can live with.
    If i want a collider to follow my sword swing movement I must set it manually per sprite, correct?
    On the example below using the automatic generation: sprite1 would translate to sprite 2 and when rotated to sprite 3 or 4?



    Will leave crash testing stacking layers on mobile for later.

    Thanks for your time
     
  46. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    So are these sprites rotated in 3D or do you have sprites for all the different angles you want to rotate to? If its just rotated in Unity, the box would just work as expected (#3). If you have custom rotated sprites, what you'd get now is not exactly #4, but an axis aligned bounding box covering the entire sword.

    In most cases, simply setting it to automatically generate trimmed box colliders will do the job for these. Its only when you want to get more specific where it becomes a problem.
     
  47. spartan

    spartan

    Joined:
    Mar 10, 2010
    Posts:
    174
    @unikron is it possible to change anchor point of a sprite in runtime from the script?
     
  48. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    No it isn't and that is intentional. If you need to change anchor points at runtime, I suggest simply attaching a GameObject as a child of the sprite at the correct point and moving that, or if you need to change the origin of the sprite itself, making the sprite a child of the gameobject.

    The reason for this is that sprites can be diced (i.e. are constructed of lots of tiny bits, and as of version 1.70, you will be able to draw custom sprite shapes too, not restricted to rectangles) - there won't be enough information at runtime to do this for all the supported use-cases, and due to the number of triangles, could be quite expensive too.

    Out of curiosity, what were you planning on doing by moving anchor points dynamically?
     
  49. spartan

    spartan

    Joined:
    Mar 10, 2010
    Posts:
    174
    Thanks!, I suppose to use an additional GameObject :). I have to duplicate the same sprite a lot of times but each sprite should be rotate dynamically with their own anchor point (think in long rectangles like hands of a watch).
     
  50. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    I should also mention that you can add a sprite multiple times to a sprite collection (and have different settings on each) - the texture gets put in the atlas only once. May not suit your intentions, but just a FYI.