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

[Free Open Source] Unity 2D Destruction

Discussion in 'Assets and Asset Store' started by TheValar, Jan 28, 2016.

  1. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Unity 2D Destruction
    Unity 2D Destruction is a basic tool for breaking 2D sprites into fragments for awesome destruction effects!!! it's totally free and Open Source so feel free to use it for whatever or contribute to it :D If you find this useful or have any questions/requests let me know!

    https://github.com/mjholtzem/Unity-2D-Destruction


     
    Last edited: Jan 28, 2016
  2. b4c5p4c3

    b4c5p4c3

    Joined:
    Jan 4, 2013
    Posts:
    537
    Nice job
     
    TheValar likes this.
  3. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Recently made a small update to this so that the source object correctly passes it's material to the generated fragments and normals are calculated. This allows you to use a diffuse sprite material if you so desire.

    Also fixed a bug where it did not work correctly when sprites had custom pivots
     
  4. majman

    majman

    Joined:
    Feb 9, 2013
    Posts:
    7
  5. mohannedkandil

    mohannedkandil

    Joined:
    Sep 12, 2016
    Posts:
    4
    Plzzz I want help, when I imported the package I got 3 errors in the console in sprite exp-loader. Can you help me??
     
  6. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    What were the errors? and what version of Unity are you on?
     
    Mattstg likes this.
  7. mohannedkandil

    mohannedkandil

    Joined:
    Sep 12, 2016
    Posts:
    4
  8. mohannedkandil

    mohannedkandil

    Joined:
    Sep 12, 2016
    Posts:
    4
    can you answer me plzz????
     
  9. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Looks like you probably need to update your Unity version
     
    Mattstg likes this.
  10. Deleted User

    Deleted User

    Guest

    This is great !

    I was going to do the same thing before finding solution on the net.
    You should make it into UNITY Asset store package.

    Anyway, unity should add this to their source code too ! :D
     
    TheValar likes this.
  11. zhihmeng

    zhihmeng

    Joined:
    Oct 2, 2016
    Posts:
    1
    It's cool, but I have two questions need your suggestions if possible. ^_^

    The first is I use some object pooling mechanism to improve the performance, and do you have any suggestion about how can I apply your 2D Destruction package with the object pooling mechanism ?

    The second is I want to blow my sprite objects in the air and let it become pieces and spray out with 360 degree. In a short word is that there is no gravity down ground on my game objects.

    Sorry for my bad english, and you just done a great job !

    Thank you in advanced.
     
  12. Lastone17

    Lastone17

    Joined:
    Feb 16, 2017
    Posts:
    1
    Hey,
    Very good asset I like it a lot.
    But I have one Problem if i generate the pieces via your script and try to make a Prefab out of these pieces, it can't save the meshfilter/meshrenderer because its created in your script..

    Do you have any workaround that I can save the Sprite with the fragments as prefab.

    Thanks for your reply!
     
  13. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    I think I replied to an email from you the other day, but maybe it didn't go through (or maybe it was someone else asking the same question). At any rate here was the response I gave

    I think the way to get that to work would be to save the mesh created in the script as an actual mesh asset in your project using AssetDatabase.CreateAsset (https://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html). Obviously you would want to be discerning about which objects you do this for since you could potentially end up with a large number of tiny mesh objects in your project.

    The altenative would be to save the vert/uv data in serialized fields and then re-create the procedural meshes in OnStart or something. Obviously this would have some sort of performance cost, but no where near as high as the initial calculation and creation of all the fragments.

    I'd probably go with option A though.
     
    Mattstg and shivampip like this.
  14. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Sorry never got around to replying to this one.
    Pooling:
    Are you doing the fragmentation at runtime? If so then yes you could probably implement pooling for the spawning of the fragments themselves without too much modification of the package, but in it's current state the actual calculations of the fragmentation might still cause lag depending on their complexity.

    If your doing the fragmentation before-hand I don't see how pooling would help, since no objects are being instantiated/destroyed, they are just being activated.

    Gravity:
    should be able to just set use gravity to false on the fragment rigidbodies
     
    Mattstg likes this.
  15. KamiRonin

    KamiRonin

    Joined:
    Dec 18, 2013
    Posts:
    7
    Hi! Good job!
    I wish to ask - and it is possible to create spriteRenderer pieces, instead of meshFilter+meshRenderer?
    It would be simple to apply fine all cycle repeatedly to a separate piece of an initial rock... Or to make smooth disappearance of pieces after explosion...
    Thanks.
    (Forgive for my English.)
     
  16. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Unfortunately, unless I am mistaken, there is no way to control the Mesh of a SpriteRenderer, so it is not possible to replace the meshRenderer with a SpriteRenderer.

    That being said you should still be able to make pieces fade out (smooth disappearance) by changing the material properties

    Not sure what "apply fine all cycle repeatedly to a separate piece of an initial rock" means
     
    Mattstg likes this.
  17. KamiRonin

    KamiRonin

    Joined:
    Dec 18, 2013
    Posts:
    7
    hmm, sorry my language..
    I meant that having received after the first cutting a piece, to have possibility to cut it once again already as independent "the initial object"
     
  18. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Ok so you want to explode the pieces resulting from the first explosion. There is a script that does that in the Demo folder called ExplodableFragments.cs

    Just attach that to your original object when you generate your fragments
     
    Mattstg likes this.
  19. KamiRonin

    KamiRonin

    Joined:
    Dec 18, 2013
    Posts:
    7
    Fine! Thanks. I was inattentive. Has thought that for a slice it is necessary to repeat simply the main sequence of operations (as with the initial sprite)...
    Good luck to you!!
     
  20. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Nice package dude ! Thanks !

    It would deserve a few more functionalities though ;)

    Like being able to define the "explosion" force if we want the fragments to have an impulse to emulate an "explosion" type of effect.Also it would be could to have an option to destroy the fragments after some seconds !
     
    TheValar likes this.
  21. abdelaziz-benyahia

    abdelaziz-benyahia

    Joined:
    May 28, 2015
    Posts:
    7
    Really nice work , though weird thing happened to me when applying it to a prefab , the prefab got fragments too eventually but in game they didn't shatter at all , I tried generating fragments manually in every element that i usually applied any modification through their prefab and it shattered , so it works for me but I am curious if there is a way to make those fragmentation apply to each copy of my prefab !
    my guess would be that it's a problem with how you name the fragments and then call them !
    Thank you
     
  22. abdelaziz-benyahia

    abdelaziz-benyahia

    Joined:
    May 28, 2015
    Posts:
    7
    And I would love to know how to destroy those fragments after explosion !
     
  23. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    For destroying fragments after an explosion. You can make a script that destroys an object after a certain amount of time, and then make a custom ExplodableAddon to add that component to each generated fragment.

    I'm guessing it's not applying to the prefabs because the private list of fragments is not getting serialized. Should be easy to fix once I get the time
     
    Mattstg likes this.
  24. abdelaziz-benyahia

    abdelaziz-benyahia

    Joined:
    May 28, 2015
    Posts:
    7
    Can you tell me how to make it serialized or where in the script please? it is kinda bad for the project , each time I wanna do a change or add a prefab in any scene , I will have to regenerate and all ...
     
  25. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Just looked a bit more into the problem and my initial thought was wrong. The problem is that the mesh object being set in the fragments is being lost when it is saved as a prefab. Not sure off the top of my head a good way to fix this. I'm sure it's just a matter of me doing something incorrectly, but at this time I don't really have much time to figure it out so I can't promise a fix anytime soon.
     
    Mattstg likes this.
  26. IsmaelHuico

    IsmaelHuico

    Joined:
    Nov 20, 2017
    Posts:
    1
    Hi! This Script is awesome. I´m making a game Angry Birds style where yo launch a character and it hits an sprite. I want to use your package but I don´t know how to make it work for it to destroy when an object collides with the fragmented sprite. I would be very happy if you can give a clue to where to start.
     
  27. coldkings

    coldkings

    Joined:
    Aug 7, 2013
    Posts:
    1
    Great script. I have a nested gameobject that doesn't seem to grab the whole sprite when created pieces when using Shatter Type Voronoi. Only the bottom left fourth is use. When I generate with type Triangular the whole sprite is used. Nothing much going on with the gameobject. It's nested in empty gameobjects, everything is scale 1, the BoxCollider2D fits exactly on the sprite. Thanks for the script.

    edit: issue seems to be in the sprite pivot. Fragments don't match when not using center pivot.
     
    Last edited: Dec 5, 2017
  28. shivampip

    shivampip

    Joined:
    Jul 23, 2017
    Posts:
    3
    Really Great asset. and Thanks for making it Open Source
     
    TheValar likes this.
  29. hashcoder123

    hashcoder123

    Joined:
    May 15, 2017
    Posts:
    1
    i have a problem can anybody help me ?
    when i create a distractable object and make it a prefab.. and then use that prefab, the pieces loss the shader or sprite component and shows empty pieces.

    can you help me ?
     
  30. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    @hashcoder123 this happens because the meshes generated from the script are never saved as assets so they don't get saved with the prefab.

    Basically with the way this asset currently works you can't really save pre-destroyed objects as prefabs and spawn them in later.
     
    Mattstg likes this.
  31. GengarGames777

    GengarGames777

    Joined:
    Jul 8, 2017
    Posts:
    16
    very nice, you should maybe look into that circlecollider2d merge request
     
    Thrower1405 likes this.
  32. leetmz123

    leetmz123

    Joined:
    Mar 14, 2018
    Posts:
    1
    Olá, eu quero saber como fazer para destruir o obj quando ele trava com outro obj, eu tentei erro
    private void OnTriggerEnter2D (Collider2D col)
    {

    if (col.gameObject.CompareTag ("bala"))
    {
    _explodable.explode ();
    ExplosionForce ef = GameObject.FindObjectOfType <ExplosionForce> ();
    ef.doExplosion (transform.position);
    }

    },

    mas não funcionou.
     
  33. imgodot

    imgodot

    Joined:
    Nov 29, 2013
    Posts:
    212
    TheValar, Just found your project and really like it although, at this point, I can't use it.
    I have a prefab ship. I explode it into fragments, but when the fragments are created they have no mesh so they are not displayed.

    Per your previous answer, you code doesn't work on a prefab.
    Is there any workaround, short of reworking all my prefabs?
    Thanks.
    -- Paul
     
  34. dnwsaa58

    dnwsaa58

    Joined:
    Dec 23, 2017
    Posts:
    1
    awesome!That't what I am looking for.And it's FREE!!!
     
  35. Lukie1988

    Lukie1988

    Joined:
    Sep 26, 2015
    Posts:
    3
    Did you find a solution for this?
     
  36. Guijeogi

    Guijeogi

    Joined:
    Apr 5, 2017
    Posts:
    2

    Hi ! i fixed prefab missing

    https://github.com/KimYeonmu/Unity-2D-Destruction-_Fixed

    I solved this problem by saving a mesh!

    Thank you for making a good asset



    If there is a problem, I will delete it.
     
  37. pyroton

    pyroton

    Joined:
    Oct 2, 2014
    Posts:
    1
    @TheValar Thanks for a great asset!

    I'm trying to implement a lots of 2D jars that can be breaked in groups. What I need is a "breaking jar pieces" object (normal jar is a different object and it simply replaces with jar fractures when needed).

    I managed to create a prefab with
    generateFragments()
    in the Start method. Then I created a pool of those prefabs and they are all generated on game start.

    But my problem is how to re-use once fractured and destroyed pieces? Is there is a method to re-construct those pieces all back and re-explode them?
     
  38. SylviaCheng

    SylviaCheng

    Joined:
    Sep 26, 2017
    Posts:
    1
    Great job! I think I can implement a "breaking into pieces" effect based on your work.
    However, I find the pieces will make up a distorted image which looks like just a part of the original sprite. This problem happens to some sprites. I guess this may caused by uv coordinates.
     
  39. INDAX

    INDAX

    Joined:
    Jun 29, 2014
    Posts:
    6
    There's a bug in Voronoi shatter type caused by the pivot of sprite not setting to the center.
    The generated fragments are out of position.
    To fix this, add the following code in SpriteExploder.cs after the line
    Rect rect = getRect(source);
    (around line 194):
    Code (CSharp):
    1. Vector2 centerPivotDiff = Vector2.zero;
    2. SpriteRenderer sr = source.GetComponent<SpriteRenderer>();
    3. if (sr != null)
    4. {
    5.     Vector2 srCenter = sr.bounds.center;
    6.     Vector2 pivot = source.transform.position;
    7.     centerPivotDiff = srCenter - pivot;
    8. }
    9. rect.center += centerPivotDiff;
    Also, add the
    centerPivotDiff
    to the Vector2 in the for loop slightly below the line (at around line 198).
    That is, change
    Code (CSharp):
    1. points.Add(new Vector2(Random.Range(rect.width / -2, rect.width / 2), Random.Range(rect.height / -2, rect.height / 2)));
    to
    Code (CSharp):
    1. points.Add(new Vector2(Random.Range(rect.width / -2, rect.width / 2), Random.Range(rect.height / -2, rect.height / 2)) + centerPivotDiff);
     
    Last edited: Sep 30, 2019
  40. theunsigned

    theunsigned

    Joined:
    Nov 15, 2018
    Posts:
    33
    Hi, this is a great asset but I'm getting a lot of null reference exceptions when items are destroyed and they seem to be triggered by collision events with other fragments. Any ideas on how to fix this?
     
  41. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    I absolutely love your asset! Great work. However, I'm having one problem. I want the fragments to have their alpha fade out after they've been created. Usually I do this with an animation that brings the Sprite Renderer's color's alpha value down to zero. Unfortunately, Unity 2D Destruction generates pieces with Mesh Filters and Mesh Renderers attached, which means I can't do that method. Any ideas on getting this to work? I'm using the Universal Render Pipeline's Sprite-Lit-Default material. Thanks in advance, your asset is awesome! :D
     
    Last edited: Jan 29, 2020
  42. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Okay, it turns out that this isn't a problem with your asset, but with the Universal Render Pipeline. Using
    SetColor
    through script doesn't work with Sprite-Lit-Default, while switching to the older Sprites-Default material fixes the issue, but that of course removes my 2D lighting support. :( Hopefully I can figure this out! Again, thanks for making this fantastic asset!
     
  43. Pulkitguglani1011

    Pulkitguglani1011

    Joined:
    Nov 24, 2018
    Posts:
    9
    Is it possible by any means or any idea by which I can implement that we can give the custom shape of the shattered pieces like if I want that a sprite should only be destroyed in 10 rectangular pieces of different sizes?
    For example, I want a 4 X 4 square sprite to be destroyed on 16 small 0.5 X 0.5 sized pieces with your asset by little modification .
    Any help would be great like any way by which I can implement it or any other asset or if you can do it by yourself
     
  44. starfoxy

    starfoxy

    Joined:
    Apr 24, 2016
    Posts:
    183
    I have the same need. Did you ever solve this?
     
  45. Pulkitguglani1011

    Pulkitguglani1011

    Joined:
    Nov 24, 2018
    Posts:
    9
  46. Thorax-

    Thorax-

    Joined:
    Feb 10, 2021
    Posts:
    22
    I encountered the following problem which appears only on some Android devices (e.g., Lenovo K9). The Purple objects are using the Explodable component.



    On most devices it appears as expected:


    I am using SpriteRenderer with the Sprites-Default unity material and textures from a texture atlas. Here are the settings of the Sprite Renderer and the Texture atlas i am using:



    The destroyable objects are made in prefabs with the mesh information and texture information stored in the Resources folder. Each piece gets its information regarding the mesh and the texture from the Resources folder. But that should not be a problem since the initial sprite texture(of the undestroyed sprite) shows purple (before it turns into pieces). And that is a simple sprite with a Sprites-Default material.

    Any suggestions are welcome. I can provide additional information if required.

    Edit: Problem Solved. Texture size is too big and not supported by older devices. switching to 4096x4096 fixed it.
     
    Last edited: Jun 23, 2022
  47. Ozility10

    Ozility10

    Joined:
    Aug 4, 2021
    Posts:
    1
    Great asset.. just want to know if we can break into circular fragments?
     
  48. Mikuson

    Mikuson

    Joined:
    Apr 20, 2021
    Posts:
    2
    Hey do anyone know how to change the number of pieces generated?
     
  49. Mikuson

    Mikuson

    Joined:
    Apr 20, 2021
    Posts:
    2
    Nevermind, I just deleted some points in Polygon Collider
     
  50. NilsLeijdekker

    NilsLeijdekker

    Joined:
    Dec 19, 2018
    Posts:
    10
    Is there a way to make it slowly like chip away at the object instead of just exploding?