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. Mikhail

    Mikhail

    Joined:
    Apr 20, 2012
    Posts:
    4
    Hello! I have animation and trigger on one of it frame. I attach a function to animationEventDelegate, but sometimes it doesnt launch. Why does it happen?
    Code (csharp):
    1.  
    2. public void PlayAnimation(string animationName){
    3.     animatedSprite.animationEventDelegate = CreateProjectileEvent;
    4.     animatedSprite.Play(animationName);
    5. }
    6. void CreateProjectileEvent(tk2dAnimatedSprite sprite, tk2dSpriteAnimationClip clip, tk2dSpriteAnimationFrame frame, int frameNum){
    7.        
    8.         Projectile projectile = (Instantiate(projectileObject, transform.position, Quaternion.identity) as GameObject).GetComponent<Projectile>();
    9.        
    10. }
    11.  
    Sometimes game object instantiates, but sometimes doesn't.
     
  2. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    This is a tricky one and I think I'm going to have to think of a proper way to solve it. So basically, the event delegate only gets triggered when the particular frame is displayed, but if the game is running slightly slowly, and the frame doesn't display or gets skipped, it never triggers.

    The obvious solution is to walk the frames to see if anything should have been triggered, but this introduces more problems -

    - process the update in FixedUpdate, which could still miss frames at additional cost.

    - what happens when the framerate is so fast that the animation has cycled 10x in one visual frame? Easy solution, minimum fps of animation = fps of game, but the fps of the game could be 30 / 60 or even dynamic.

    - ideally, the delegate target should know when in time the animation event occurred too, so if in one frame you get 4 events (i.e. 4 frames have passed, and we'ere currently displaying the 5th), could happen even when the constraint above is enforced when frame rate drops, for instance, the target should be told when that would have occured, i.e. X secs in the past or something like that.

    if any of these would work for you let me know, or if you have any other suggestions, I'll be happy to hear em!
     
  3. Uttpd

    Uttpd

    Joined:
    Feb 27, 2010
    Posts:
    114
    update, got it.
    Regarding the sprite sheet importing from Texture Packer, got it working after reading on the docs:

    "Sprite Sheet Import introduced support for automatically splitting regularly spaced sprite-sheets. 2D Toolkit will split these up and atlas them like any other texture. These textures do NOT have to be power of two, but the dimension MUST divide perfectly with the number of divisions. To set these up, simply add entries to the SpriteSheets section of the Sprite Collection set up."

    since Texture packer can build a sheet without trimming or cropping, I got it working, have to further text if everything is running with precision. But hopefully you can had to the features a sprite-sheet import ready from Texture Packer.
     
  4. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    That document is slightly out of date - the textures dimension doesn't need to be perfectly divisible by number of divisions - indeed you now enter exact tile sizes in now and you can also set up offsets, etc.

    I have added native TexturePacker import to the backlog, but the priority is low - it'll be quite a while before I get around to doing it. I can reprioritize if there is more interest though, so anyone else who would like this feature, please let me know.
     
  5. jiowchern

    jiowchern

    Joined:
    Apr 27, 2012
    Posts:
    1
    Hello
    Excuse me, I want to adjust the button inside my project, I found that the size of the button seems to only be adjusted from the Sprite Scale, is not the same as UnityGUI a Button can be directly set the width with the height practice.

    I Camera related settings are as follows
    Projection = Orthographic
    Size = 480
     
  6. Uttpd

    Uttpd

    Joined:
    Feb 27, 2010
    Posts:
    114
    It would be nice but no real priority as it works with the present build.
    As long all your individual sprites are the same size, 2Dtoolkit can extract and build an atlas from the TexturePacker spreadsheet.

    Workflow:
    on Texture Packer: just unchek "trim" "rotation", and set "Border" and "Shape"padding to 0, publish for "cocos2d" default ".png" -- you should get a clean spreadsheet with all the images align,

    2DToolkit will cut it perfectly, and rename them as original-name/0x0, /0x1 etc following the position on the spreadsheet

    The autofill function did not work do. Is there any naming convention I should follow?
     
  7. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    The 1.70 versions now name the imported textures as name/0..N-1, and will be compatible with the autofill feature. That was an oversight on my part :(
     
  8. Konash

    Konash

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

    I've been experiencing this many times already.

    I'm using the button scripts that came with the toolkit.

    It is working good so far. Though there is a strange problem. Sometimes, the button refuse to get clicked (where it was clickable before). And this issues doesn't happen at run time, it becomes permanent.

    To fix this, I actually have to remove the component, and re add it again. I tried resetting, but it didn't work. This happened to me at least 3 times already.
     
  9. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Thats really strange, whatever state should reset when the game gets restarted. Would it be possible to send a repro case when this is broken to the support email add (support at unikronsoftware dot com)
     
  10. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
  11. Konash

    Konash

    Joined:
    Apr 17, 2012
    Posts:
    7
    When this happens, I will. Thanks.
     
  12. Malarhak

    Malarhak

    Joined:
    Apr 17, 2011
    Posts:
    21
    Hello,

    I've seen some events with the tk2dbutton component. But I don't know how to use them (new to C# and event Unity in general) and can't find any informations about how to handle these events on google. Could anyone explain me basically how I can define the onClick behaviour of my buttons ?
     
  13. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    282
    If you want to make your own buttons, then the simplest form is with OnMouseDown:
    http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown
     
  14. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Events use delegates. Here is some docs on msdn explaining how delegates are used

    http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx

    Also, check out the code in the animation sample - the animation complete and event delegates are used there.
     
  15. _Alan

    _Alan

    Joined:
    Jun 17, 2011
    Posts:
    20
    Hello, I'm with a doubt about this asset. I can use to make somethink like 2d car physics? I would like to link the wheels on another sprite, but that this wheels could rotate normally.
     
  16. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Yes, this is certainly possible. All you need to do is create the sprites independently, and then attach the wheel sprites to the car sprite and you'll be good to go. How you implement the car physics is up to you, but you can either use the built in Unity physics, or do some stuff with raycasts, etc.
     
  17. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    I'm using the tk2dCamera as my camera and got everything setup. I've made my colliders for my sprites, however I've added a rigidbody to one of them and it falls VERY slowly. I've tried to turn up the mass to make it "heavier" but that doesn't really do anything. Any idea's how to fix this?
     
  18. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    You'll need to change gravity. This is set up in physics settings
     
  19. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    Is this a common problem, or is there something wrong with my setup?
     
  20. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    It depends on unit sizes and tile sizes. With default gravity and a scene res of 1024x768 the scene will be 768 meters high. Changing the gravity will scale this.
     
  21. _Alan

    _Alan

    Joined:
    Jun 17, 2011
    Posts:
    20
    I see, thx unikron.
     
  22. _Alan

    _Alan

    Joined:
    Jun 17, 2011
    Posts:
    20
    I'm having a little problem.
    If I use the camera 2d toolkit, creating from menu, I can set the absolute position x,y of all objects on the scene, and apparently looks pixel perfect. But the physics is horrible, cause the sprites is more big to adjust on screen, he don't use the real physics.

    But if I set my own camera how ortographic mode, I can use the sprite on your real size, with a real gravity physics, but is not pixel perfect. And I can't position with a real x.y in relation to the screen.

    How this work? I'm bit confusing about that.
     
  23. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Unfortunately the physics world in Unity can't be scaled relative to the graphics. This isn't all that bad though. So with tk2dCamera, the sprites are big - so simply compensate by increasing the scale of gravity, etc in Edit > Project Settings > Physics. That way you get to keep the ease of use of the tk2dCamera, and physics also works.

    The alternative is to simply use an ortho camera. As long as you create your ortho camera in a fixed size, and use the same size on the sprite collection (untick tk2dCamera and enter the values) they will be pixel perfect (at the right resolution). You can position with real x and y by using the following method - Camera.ScreenToWorldPoint to get the correct world position to fit the camera.

    If you need auto scaling to be pixel perfect on other resolutions I will be releasing some sample code on the forum soon. (www.unikronsoftware.com/2dtoolkit/forum)
     
  24. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
  25. quangtu89

    quangtu89

    Joined:
    May 3, 2012
    Posts:
    1
    I have got a problem with anim .
    I have 2 anim is "run" and "walk" .
    anim run is default .
    but when i play anim "walk" postion of sprite is moved .
    I don't script to it .
    T_T .
     
  26. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Are the sprite anchors for these sprites at the same position?
     
  27. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Hi Unikron - just got your plug and have been enjoying it so far. I have an easy feature request.

    I have say 30 sprites, named alien1, alien2, 3, etc. up to 30. Say I only want to add #2, 4, 8, 14, 20 to my sprite collection and then make an animation out of it. When I create my SpriteAnimation, the autofill doesn't want to work. It just repeats the first sprite x number of frames. Could autofill please be smart enough to just go to the next highest number of the same name?
     
  28. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Here's one more: could Sprite Collections honor manual texture import settings applied to atlases, or give users some preferences for their generation? For example, I'd rather my atlases not have anisotropic filtering, so at the moment it seems I have to manually change the generated atlas import filter to Point every single time I commit a sprite collection.

    EDIT: And one more! When I remove a sprite from a collection, could associated Sprite Animations remove that sprite and resize their frames array? At the moment I get the ugly purple texture, and if I change number of frames myself, I get an index out of bounds exception.
     
    Last edited: May 3, 2012
  29. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    To the first request - this will be addressed in a future update.

    You can select point sampling in the sprite collection properties. I fact, thats the reason it messes about with that setting. The reason it's there is that the amount of padding changes when point sampling is selected. In a lot of cases you won't really need any padding at all.

    The third is tricky. A sprite may be used in one or more animation, and removing itself isn't as straightforward as it sounds. One case off the top of my head - what happens when all sprites used by a clip are deleted? What happens to the clip?
    I will be improving the interface on the animation editor so I'll certainly consider gracefully fixing when sprites don't exist. It just can't be done automatically on everything from what I can see.
     
  30. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    Hi unikron,

    Is future update free?

    Thanks in advance
     
  31. unikronsoftware

    unikronsoftware

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

    All updates so far have been free, and all future updates to the product will be free too. We might do paid add-ons which do other non-core features though, no plans just yet, but just keeping that option open :)

    Cheers,
    unikron
     
  32. kalvinlyle

    kalvinlyle

    Joined:
    May 31, 2011
    Posts:
    19
    Just released my first mobile game last week. It uses 2D Tool Kit.

    Tango Chase
     
  33. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Thanks for your incredibly rapid responses Unikron! Pleased to hear about the upcoming features and the ones I overlooked.
     
  34. dead1ock

    dead1ock

    Joined:
    Jan 4, 2011
    Posts:
    31
    Congrats!
     
  35. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    Thanks. :D

    Congrats!:D
     
  36. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Fantastic stuff. The game is brilliant, and looks great! Congratulations on releasing it.
     
  37. Petrarch

    Petrarch

    Joined:
    May 4, 2012
    Posts:
    1
    Hey i have a silly question. Is there anyway to import a spritesheet containing a column of sprites, as a bunch of separate sprites? Or should I just cut it up and import the individual images?
     
  38. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Just import it as a sprite sheet (new spritesheet in the sprite collection editor). You should be able to set the split sizes there.
     
  39. _Alan

    _Alan

    Joined:
    Jun 17, 2011
    Posts:
    20
    Hey unikron, do you have some idea, how I can create a ilumination like this:
    http://asantee.net/wp-content/uploads/ss6.jpg
    http://asantee.net/wp-content/uploads/ss1.jpg

    In reality, I just would like to create a penumbra ilumination. The player will have a lantern on the your head, and it'll follow the head rotation. So, I want that the player just see what the lantern is showing.

    I though about to use a full black image on the screen, and in this image, use a alpha channel, with a lantern light silhuet, but I don't know how I'll control this silhuet in real time, I don't know if this is just possible.
     
  40. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    That looks like real lights on real 3d geometry. If you want to try 2D version of the same thing, there are some lit shaders on our support forum, you can use them (and they will light correctly, as long as you build your sprite collections with normal generation), but keep in mind that performance will drop and unless you're very careful, it would be slow on mobile.
     
  41. unikronsoftware

    unikronsoftware

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

    There is a bug (unfortunate typo) in the currently released (1.70 final + patch 1), which causes textures to get trimmed incorrectly. You can rectify this by opening tk2dSpriteCollectionBuilder.cs, and changing line 343 to read:

    Code (csharp):
    1.  
    2. y1 = Mathf.Min(y1, th - 1);
    3.  
    If you don't wish to get this, simply grab version 1.70 final from our forum - we will be updating the released version on the Asset Store, and releasing a new version on our forum with this fix applied very shortly.

    Sorry for any inconvenience this may have caused.
     
  42. TTkJonas

    TTkJonas

    Joined:
    Mar 21, 2012
    Posts:
    28
    Hello unikron,
    when i open the Asset Store, the price for the 2d Toolkit is now 81,25$. Is this right? I thought the price was 65$ :)
    Thanks
     
  43. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    It is still $65, and that will not change for the forseeable future. In my Asset Store window I see the price as €61.75.

    However, depending where you are in the world, Unity may add appropriate sales tax / VAT / other ways governments rob you blind here. I don't know where you are in the world, so I can't comment on that.
     
  44. TTkJonas

    TTkJonas

    Joined:
    Mar 21, 2012
    Posts:
    28
    Thanks for the quick answer. I am from germany. Okay when i click "buy", I see it is the Vat 16,25$. That makes the high price for me.
     
  45. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Sorry about that, I don't have any control over what Unity adds for VAT. Actually its not even up to them, its a legal requirement to charge VAT in EU countries if your company is VAT registered, and Unity Technologies are.
     
  46. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
  47. chillypacman

    chillypacman

    Joined:
    Oct 1, 2011
    Posts:
    71
    I'm finding that when I change a script in Unity 3D at runtime (any script) and all scripts end up reloading toolkit throws an exception:

    DivideByZeroException: Division by zero
    tk2dAnimatedSprite.Update () (at Assets/TK2DROOT/tk2d/Sprites/tk2dAnimatedSprite.cs:352)

    Amu ideas why and is it possible to fix?
     
  48. Marwan88

    Marwan88

    Joined:
    May 6, 2012
    Posts:
    3
    Amazing ..
     
  49. unikronsoftware

    unikronsoftware

    Joined:
    May 21, 2011
    Posts:
    1,287
    Do you have sprites animating at that time? Also what version of 2D Toolkit are you using? I just need a bit of info so I go ahead and investigate this. Can't think of any reason of the top of my head, but I will investigate the next time I get a chance.
     
  50. eriihine

    eriihine

    Joined:
    Nov 27, 2011
    Posts:
    13
    Updated to 1.7.1 patch over the weekend, but I'm getting following errors after the import has been completed.

    With the new version I can no longer add sprites to my atlases. I'll always get the same errors. 1.6.1 patch 1 is working normally for me.

    No other changes were done to the project.

    Is this a known problem to be fixed by 1.7.1 patch 2 ?


    Texture has out of range widht / height
    UnityEngine.Texture2D:.ctor(Int32, Int32)
    tk2dSpriteCollectionBuilder:processTexture(tk2dSpriteCollection, Boolean, Boolean, Texture2D, Int32, Int32, Int32, Int32, SpriteLut, Int32) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionBuilder.cs:356)
    tk2dSpriteCollectionBuilder:Rebuild(tk2dSpriteCollection) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionBuilder.cs:743)
    tk2dSpriteCollectionBuilder:RebuildOutOfDate(String[]) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionBuilder.cs:133)
    tk2dSpriteCollectionTextureWatcher:OnPostprocessAllAssets(String[], String[], String[], String[]) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionTextureWatcher.cs:24)
    UnityEditor.AssetPostprocessingInternal:postprocessAllAssets(String[], String[], String[], String[], String[])



    UnityException: Failed to create texture because of invalid parameters.
    UnityEngine.Texture2D..ctor (Int32 width, Int32 height)
    tk2dSpriteCollectionBuilder.ProcessTexture (.tk2dSpriteCollection settings, Boolean additive, Boolean stretchPad, UnityEngine.Texture2D srcTex, Int32 sx, Int32 sy, Int32 tw, Int32 th, .SpriteLut spriteLut, Int32 padAmount) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionBuilder.cs:356)
    tk2dSpriteCollectionBuilder.Rebuild (.tk2dSpriteCollection gen) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionBuilder.cs:743)
    tk2dSpriteCollectionBuilder.RebuildOutOfDate (System.String[] changedPaths) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionBuilder.cs:133)
    tk2dSpriteCollectionTextureWatcher.OnPostprocessAllAssets (System.String[] importedAssets, System.String[] deletedAssets, System.String[] movedAssets, System.String[] movedFromAssetPaths) (at Assets/TK2DROOT/tk2d/Sprites/Editor/tk2dSpriteCollectionTextureWatcher.cs:24)
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
    System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters)
    UnityEditor.AssetPostprocessingInternal.PostprocessAllAssets (System.String[] importedAssets, System.String[] addedAssets, System.String[] deletedAssets, System.String[] movedAssets, System.String[] movedFromPathAssets)