Slice, dice and explode any sprite in your game! Sprite Slicer allows you to quickly and easily cut any physics-enabled sprite into little pieces, each of which will then have its own realistic physics behaviour.

User Guide

Overview


Sprite Slicer 2D provides a set of static C# script functions to quickly and easily slice sprites along a given vector. There is no need to create any new game objects or managers – simply call the functions from your script and Sprite Slicer 2D will handle the rest.

For each sprite that is sliced, two or more new sprites will be created, each with the correct 2D polygon collider and physics behaviour according to the slice location and slice type. Sliced sprites will share the same render material whenever possible in order to preserve batching and reduce draw calls.

Check out the demo video below!



Slicing Functions


void SliceAllSprites(Vector3 worldStartPoint
                     Vector3 worldEndPoint)

void SliceAllSprites(Vector3 worldStartPoint
                     Vector3 worldEndPoint
                     bool destroySlicedObjects
                     ref List<SpriteSlicer2DSliceInfoslicedObjectInfo)

void SliceAllSprites(Vector3 worldStartPoint
                     Vector3 worldEndPoint
                     bool destroySlicedObjects
                     int maxCutDepth
                     ref List<SpriteSlicer2DSliceInfoslicedObjectInfo)


These functions will cut any non-static, non-kinematic, physics-enabled sprite that is intersected by the given vector. The code performs a raycast along the vector, identifying any colliders that the ray both enters and exits, and then splits the object in half at the appropriate points. If the ray terminates within a particular collider, then it is not treated as being cut.

The destroySlicedObjects parameter determines whether or not the sliced object should be destroyed (this is the default behaviour). If set to false, the the sliced object will only be disabled, so will still exist in the world.

maxCutDepth allows you to control how many times each sprite can be subdivided. Each fragment created as part of a slice will store a numerical value tracking how many divisions away from the original parent object it is. Any fragment that has been cut more than maxCutDepth times will be excluded from future collisions. This value can be set to -1 to allow infinite subdivisions.

For information on the slicedObjectInfo parameter, see the 'Advanced Usage' section below.



void SliceSprite(Vector3 worldStartPoint
                 Vector3 worldEndPoint
                 GameObject sprite)

void SliceSprite(Vector3 worldStartPoint
                 Vector3 worldEndPoint
                 GameObject sprite
                 bool destroySlicedObjects
                 ref List<SpriteSlicer2DSliceInfoslicedObjectInfo)

void SliceSprite(Vector3 worldStartPoint
                 Vector3 worldEndPoint
                 GameObject sprite
                 bool destroySlicedObjects
                 int maxCutDepth
                 ref List<SpriteSlicer2DSliceInfoslicedObjectInfo)

The SliceSprite functions behave in the same way as SliceAllSprites, but allow you to specify a particular GameObject that you want to cut. The GameObject must be either a Unity or 2D Toolkit sprite that is non-static, non-kinematic and physics-enabled.



void ExplodeSprite(GameObject sprite
                   int numCuts
                   float explosionForce)

void ExplodeSprite(GameObject sprite
                   int numCuts
                   float explosionForce
                   bool destroySlicedObjects
                   ref List<SpriteSlicer2DSliceInfoslicedObjectInfo)


ExplodeSprite cuts a given GameObject multiple times through the centre, and then applies an optional outward explosive force to each fragment.



void ShatterSprite(GameObject spriteObject
                   float explosionForce)

void ShatterSprite(GameObject spriteObject
                   float explosionForce
                   bool destroySlicedObjects
                 ref List<SpriteSlicer2DSliceInfoslicedObjectInfo)

ShatterSprite decomposes the given sprite into its constituent polygons, then applies a force (optionally) to the resulting pieces. The visual effect will vary from sprite to sprite, but in general the more individual triangles there are in the collider, the better this will look. A simple quad mesh with two triangles will simply split in half, whereas a complex polygon with many triangles will shatter in many small pieces.

For all slicing functions, vector points should be given in world coordinates.

Advanced Usage


All SpriteSlicer2D functions can optionally generate a list of SpriteSlicer2DSliceInfo objects, which provide the calling function with information about objects that were sliced, the enter and exit points of those slices, and which child objects were generated as a result of the slice. This could be useful if, eg. you wanted to add a particle effect at the entry and exit point of each slice. To make use of this functionality, pass in an empty List<SpriteSlicer2DSliceInfo> to the slicing functions, and it will be filled out with the appropriate information.

Sprite Slicer will send a OnSpriteSliced event to any GameObject once it has been sliced. To make use of this, simply implement the following function:

void OnSpriteSliced(SpriteSlicer2DSliceInfo sliceInfo)

in any script attached to the sliced GameObject, and it will trigger if/when the sprite is sliced.

2D ToolKit Support


2D Toolkit sprites are fully supported by Sprite Slider 2D – simply open up SpriteSlicer2D.cs and uncomment the first line that says 

//#define TK2D_SLICING_ENABLED

(this is disabled by default to prevent compile errors for users that do not own 2D Toolkit)