Search Unity

Destructible 2D ☄️ Dynamic Sprite Destruction

Discussion in 'Assets and Asset Store' started by Darkcoder, May 29, 2014.

  1. alecie

    alecie

    Joined:
    Sep 2, 2013
    Posts:
    73
  2. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    The standard.shader isn't compatible with sprites though, which is why I'd need to start with one that is.
     
  3. alecie

    alecie

    Joined:
    Sep 2, 2013
    Posts:
    73
    But it works when I drag it to sprite, if it weren't compatible it wouldn't, or?
     
  4. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    It renders, but it's not designed for it. As I said, Unity only comes with Sprites-Default and Sprites-Diffuse. If you can find a standard shader version for sprites then I can modify it to work with D2D.
     
  5. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    Hi DarkCoder

    im in the process of manually upgrade to the new version , i have to do it manually cause i have modified your code so much so i can't just import the new version as i will destroy my changes, so the idea is to make a list of the files marked as NEW in the asset store import window, import these into a blank project and then compare these with the ones i have and write the modifications corresponding to the new version manually, but this would be much easier if i could have a unmodified copy of PREVIOUS version of D2D so i can find the changes much more easier.

    so , question is, do you still have a clean copy of previous version ? can i have it?
     
  6. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Yes, I still have copies of most D2D releases. If you tell me the exact version you're going from then I can send you that.

    If you're using version control then an easy way to check what's been changed is to commit the old version of D2D to some repo, then upgrade to the latest version, then you can diff the files against the old revision, and it will tell you exactly which lines have been modified.
     
    DrKucho likes this.
  7. CoderPro

    CoderPro

    Joined:
    Feb 21, 2014
    Posts:
    327
    @Darkcoder How can i make outline for explosion stamp only ? I dont want to using outline for my sprite level. I want for stamp sprite only.
     
  8. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    awesome, i just opened the ChangeLog file and the last input is Version 1.1.4 so i guess that's what i have , please PM me a link to it

    version control? never heard of it, is that an external software? do you know if there is mac version?
     
  9. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    If you mean the outline shader that comes with D2D, just only apply the outline material to your stamp sprite?


    I sent you it.

    Version control such as Git or SVN. I'm sure you've heard of them.
     
  10. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    thanks for the PM

    about version control, i'm afraid i don't know them, i used to be amateur programmer in 8 / 16 bit age, but in the new days i'm a newbie, just got into unity year and a half ago, don't worry ill google those you mentioned
     
  11. alecie

    alecie

    Joined:
    Sep 2, 2013
    Posts:
    73
    Is it impossibile to modify Standard.shader or just not recommended? I really want to have normal maps in my game.
     
  12. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    If you just want normal mapping then you can probably find a modified Sprites-Diffuse shader somewhere that supports it, and I could modify it to work with D2D. The standard.shader is very complex though, and I don't want to spend a lot of time getting it working with sprites when I'm not so familiar with surface shaders.
     
  13. CoderPro

    CoderPro

    Joined:
    Feb 21, 2014
    Posts:
    327
    @Darkcoder But "Stamp" is a Texture. And could not using materials for stamp ! Or maybe i don't known have a way to using stamp with material ? I am using D2D_ClickToStamp.cs to make stamp.
     
  14. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    What do you mean by outline then? Maybe some pictures would help.
     
  15. CoderPro

    CoderPro

    Joined:
    Feb 21, 2014
    Posts:
    327
    @Darkcoder This is screenshot from a game. The image of level not using outline. But when explosion will have outline at that stamp

     
  16. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Did you try the outline effect that comes with D2D? You can get results similar to that if you use a smooth stamp texture.
     
  17. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    For everyone that's bought it, thanks a lot! You can expect new tutorial videos real soon, as well as some changes and new demo scenes.
     
    Last edited: Jun 24, 2015
  18. FlipJam

    FlipJam

    Joined:
    Jun 3, 2014
    Posts:
    9
    Is it currently possible to get the world bounds(size/offset) of the generated D2D_Polygon_Sprite_Collider after it has been fractured for each new piece?
     
  19. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    If you mean the exact size, then no, it's not stored. But you can get the size of the AlphaTex (which the collider is generated from), via D2D_Destructible.AlphaX/Y/Width/Height. If you want the exact size then you'll have to modify the collider generation code to calculate it and store it in the sprite collider component.
     
  20. NAiLz

    NAiLz

    Joined:
    Apr 17, 2013
    Posts:
    88
    Darkcoder,

    Awesome asset! I'm a noob when it comes to coding though and I wondered about which pieces of code I need to look at to make your system work with my projectiles/beam weapons. I've looked at the demo scenes including the space shooter demo scene and tried to copy your setup for projectiles/asteroids but for some reason the effect isn't working.
     
  21. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Look at the D2D_ClickToStamp script to see a very basic setup. Basically, you just need to call D2D_Destructible.StampAll with the desired parameters to damage stuff in your scene. Most of the demo scenes use the D2D_ExplosionStamp script, which only fires once when a prefab or something is spawned.
     
  22. NAiLz

    NAiLz

    Joined:
    Apr 17, 2013
    Posts:
    88

    Awesome. thanks Darkcoder. Got it working now.
     
    Darkcoder likes this.
  23. jocyf

    jocyf

    Joined:
    Jan 30, 2007
    Posts:
    288
    Hi,

    I'm splitting a sprite into a hundred pieces using a complicated stamp. But every single piece has their pivot centered in the original sprite pivot coordinates. It's posible to get the 'centered' pivot of each piece (witch should depend on the resulting piece of sprite after the split has been made)?
     
  24. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Not without modifying the D2D_SplitBuilder code. The way D2D works when splitting a sprite is that it keeps the same sprite across all split pieces, but the destruction data is unique to each one, and is rendered using a separate material.

    The reason for this is so that you can place destroyed parts into prefabs without issue, as prefabs can only store references to assets, and if a new sprite was created every time a sprite was destroyed then the sprite object would have to exist in the scene, thus be incompatible with prefabs.

    If you don't care about making prefabs from your destroyed parts then take a look at D2D_SplitBuilder.cs line 251. This method is called every time every time a sprite is split, or its size changes from destruction. In this method you can add some code to duplicate the sprite from the SpriteRenderer, and assign your desired pivot point.
     
  25. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    78
    Hi, loving the look of this asset.

    before purchasing, I'm interested to know how the asset handles spritesheets, more specifically, the slicing aspect.

    If i were to have a character using a running animation, and then slice them, would that be possible? If so, would they continue to animate?

    Thank you.
     
  26. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I just tried it and it doesn't behave well. Basically, when you make a destructible sprite it converts the alpha channel into an 'AlphaTex', and then trims the edges and changes the shader so it can render the alpha using this 'AlphaTex' as the alpha source. However, if you change the sprite then any changes to the size/trimming/etc causes it to render incorrectly. Maybe if your animation sprites maintain the same size then it would work as expected, but it might not be so easy to do that. I could change it so the alpha always fits to the current sprite size, but that means the alpha shape might change around as the sprite is animated, thus also not looking good.

    So I guess the conclusion is that making animated sprites destructible isn't a good idea. But if you want animation to stop upon destruction then you could easily detect a collision normally, make the sprite destructible, then slice it, and finally stop the animation on the sliced parts.
     
  27. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    78
    I had a feeling something like that might be the case. Not a massive problem. When you say the same size, do you mean the same scale? None of the sprites would be changing their scale at all I believe.

    Thank you for such an in-depth and fast response.
     
  28. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I mean the size of the actual sprite data. When you create sprites in Unity it automatically trims the transparent edges, even if you don't click trim it will trim the data internally. This means that if your animated object changes shape at all, it's likely the sprite data will change size, thus the AlphaTex will be drawn in the wrong place, and look strange.
     
  29. PaulBeenis

    PaulBeenis

    Joined:
    Aug 6, 2015
    Posts:
    1
    Hi, I love this asset! It's really helping me out with my game. I'm having trouble with one area, though, which is movement (pretty big area, haha). In this platformer, the character is continuously running on portions of the ground that have been destroyed, and when he does this, he sometimes clips through the edges of the stamp. Can you recommend a specific type of character controller that is good at dealing with terrain of many angles?

    Edit: I'm hoping for an engine that is kinematic so I don't have to deal with Unity's crazy physics!
     
    Last edited: Aug 6, 2015
  30. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Unfortunately I have no idea, as most of my game experiments using D2D used spaceships in zero gravity. I was in the process of writing some sort of character controller for animals running around using a PolygonCollider2D, but there wasn't really anything special with the code. Maybe you'd have to get the balance between player mass and scenery mass right? Or write some code for foot IK so it doesn't feel like the player is sliding over everything, but I'm not too sure.
     
  31. _MGB_

    _MGB_

    Joined:
    Apr 24, 2010
    Posts:
    74
    Hi,

    How would I rebuild a destructible sprite from script at runtime?
    I managed to do it by changing 'DestroyAlphaTex' to public and calling that, but is there an official way to do it?

    If I set the sprite renderer sprite to none and clear the destructible sprite alpha in the inspector then it crashes Unity (presume it's trying to build a new alpha from a null texture?)
     
  32. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    You should set the AlphaTex to null to rebuild the alpha data from the sprite. Changing the sprite shouldn't cause any crashes though, I'll check the code now!
     
  33. _MGB_

    _MGB_

    Joined:
    Apr 24, 2010
    Posts:
    74
    Well the alphaTex property is read-only, which made me look for another way...
     
  34. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    You're right, I meant call 'ReplaceAlphaWith' and pass a null texture or null sprite. This will cause the alpha from the current sprite to be copied during the next update. If you want it to happen instantly then just pass the current sprite to 'ReplaceAlphaWith'.

    I'll modify the code to make this process easier from code and add some more documentation for it.
     
  35. _MGB_

    _MGB_

    Joined:
    Apr 24, 2010
    Posts:
    74
    Ah thanks, that does the job :)
     
  36. Nimdanet

    Nimdanet

    Joined:
    Jan 19, 2013
    Posts:
    38
    Hi!

    First of all, thanks for the great tool. I've already purchased and doing some good stuff.

    So, here's my questions (I'm only using D2D_DestructibleSprite and D2D_PolygonSpriteCollider):

    1 - Do you have any event/action to: "OnObjectStamp"? Like, whenever I exploded some part of it, it creates a new explosion.
    2 - When I use irregulars StampText, sometimes it creates some mini collidable pixels that might bug the user. Is there some part I can set: if some part is smaller than x, erase it?

    Thanks for your time :)
     
  37. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    1 - Yes, there is the OnAlphaTexModified(D2D_Rect rect) message which gets called after a sprite has been stamped.

    2 - This requires using the D2D_Splittable component and the 'Min Pixels' setting, which will discard any pixel 'islands' that fall below this threshold. If you don't want your sprite to split at all then you could take a look at how the code works and modify it, but it's a bit advanced.
     
    Nimdanet likes this.
  38. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    84
    Hi, I have a quick question: Would it be possible to use Spine Animations (a 2D bone-animation tool that can export to Unity) in conjunction with Destructible2D? Sorry if this question was asked before, thanks for your time! :)
     
  39. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    If spine works by just moving unity's built-in SpriteRenderer then it should work, else no.
     
  40. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    84
    It uses a Mesh Renderer with a Custom Shader which can be viewed here: https://github.com/EsotericSoftware...ty/Assets/spine-unity/Shaders/Skeleton.shader

    Would it maybe be possible to extend that shader with the Destructible functionality or is there no chance?
    Thanks for the super fast answer!
     
  41. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    It can be done, but it would require a lot of code modification, because right now all the code is designed to work with sprites.
     
  42. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    84
    Alright, thanks a lot!
     
  43. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Just a heads up: the current version of Destructible 2D (1.1.8) has a very silly bug that causes destruction to not actually work. I've already submitted an update (1.1.9) that fixes this issue, but it will probably be a few days before it goes live. So I would recommend you stick to the previous version (1.1.7) for now, or contact me by PM/Email if you want a copy of the latest version.

    [edit] 1.1.9 should now be live!
     
    Last edited: Sep 1, 2015
  44. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Did you update to version 1.1.9 that was just released a few hours ago?
     
  45. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Hey guys, I'm currently working on a large update to Destructible 2D that will increase the rendering performance a lot, increase the destruction performance a lot, increase the splitting performance a lot, and also simplify the architecture and usage of the asset as a whole.

    I'll probably have it ready for release in under a week, so stay tuned!

    Here's a gif of one of the demo scenes I'm working on improving:

    http://i.imgur.com/C4iIGEu.gifv
     
    docsavage, abatcat and Ostwind like this.
  46. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Version 2.0.0 has been submitted for approval!

    I'm sure it will take a while to go live, so in the meantime you can check out the new WebPlayer Demo.

    It took a bit longer than expected because I decided to add in some extra cool features, and made some changes to the splitting system so it's much higher quality than before :)
     
  47. abatcat

    abatcat

    Joined:
    Feb 8, 2015
    Posts:
    70
    Awesome! thank you for not abandoning this asset.
     
    Darkcoder likes this.
  48. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Well that was quicker than expected, Destructible 2D Version 2.0.0 is now live!



    Make sure to read the Upgrade Guide.pdf if your project is currently using 1.X.X, because this isn't just an incremental update.
     
    _MGB_ likes this.
  49. _MGB_

    _MGB_

    Joined:
    Apr 24, 2010
    Posts:
    74
    Hey,

    Updated to v2, now I can't seem to update the alpha/collision from script any more.

    Was calling ReplaceAlphaWith(), now presume I call ReplaceWith(), which replaces the sprite but leaves the old alpha/collision there.
    I see the ReplaceWith code calls ReplaceAlphaWith and UpdateMesh... do I need to call something else?

    Ta,
    M
     
  50. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    ReplaceWith should replace both the main sprite and the alpha data.

    Does it work if you drag and drop your texture/sprite into the ReplaceWith field in the inspector?

    If you still can't get it working then I'd need some more information about your setup. Like: are you trying to ReplaceWith using a Texture2D or a Sprite? Are they marked as readable? Are they any console errors? If not, then maybe you can send me a scene with just your destructible that doesn't get reset, and the texture/sprite you're trying to replace it with.