[RELEASED] Sprite Slicer - 'Fruit Ninja'-style sprite cutting system

Hi folks,

I’m pleased to announce the release of Sprite Slicer on the Unity Asset Store

Web Player Demo

Tutorial Video

Sprite Slicer allows you to cut any physics enabled sprite into little pieces, each of which then behaves as its own independent physics object. Its perfect for bringing your 2D game to life with destructible objects and environments, or for creating a Fruit-Ninja style cutting game.

* Works on both Unity and 2D Toolkit sprites.
* Simple static script functions allow you to slice sprites along a given line, or explode them into multiple parts.
* Optimised to preserve dynamic batching and reduce draw calls.
* Slices any sprite with a 2D box, circle or convex polygon collider
* Full C# source code and example scene included

The code has been designed to be as transparent as possible - there are no new gameobjects to create; you simply pass a cut start and cut end point to one of the static Sprite Slicer functions, and the code will handle the rest. Once cut, each child sprite behaves as its own separate physics body, retaining all the physics and rendering properties of the parent object, and can itself be cut into further child objects. If required, the slicing functions can return a list of all the cut objects along with their child objects and cut positions, so that the user’s code can perform additional processing once the cut has taken place. Equally, you can delay destruction of the parent object in the event that you need to access variables from it before it is destroyed.

Screenshots


I hope this proves helpful to some people out there! Please feel free to reply in this thread or PM me if you have any questions :slight_smile:

1 Like

Some guys over on Reddit were asking for some example of the interface, so this might be of interest to some folk here too:

static void SliceAllSprites(Vector3 worldStartPoint, Vector3 worldEndPoint);
static void SliceAllSprites(Vector3 worldStartPoint, Vector3 worldEndPoint, bool destroySlicedObjects, ref List<SpriteSlicer2DSliceInfo> slicedObjectInfo);
static void SliceSprite(Vector3 worldStartPoint, Vector3 worldEndPoint, GameObject sprite);
static void SliceSprite(Vector3 worldStartPoint, Vector3 worldEndPoint, GameObject sprite, bool destroySlicedObjects, ref List<SpriteSlicer2DSliceInfo> slicedObjectInfo);

So you have the option of slicing any sprite that intersects the vector or you can pass through a specific gameobject if you know exactly what it is that you want to cut. The demo is using the first option, which is why you can get multiple things getting cut if you move the mouse fast enough.

SpriteSlicer2DSliceInfo is an optional parameter, which will get filled out with information on which slice generated which child objects from which parent object, so you can do any extra processing as required (eg. add particle effects at the cut positions, increment a score depending on what type of object the parent was)

The explosion code is as follows:

static void ExplodeSprite(GameObject sprite, int numCuts, float explosionForce);
static void ExplodeSprite(GameObject sprite, int numCuts, float explosionForce, bool destroySlicedObjects, ref List<SpriteSlicer2DSliceInfo> slicedObjectInfo);

It basically slices the object multiple times at random angles through the centre, and then applies an outward force to each child object away from the centre. I’ve got some ideas for improving this - specifically running a triangulation algorithm over the mesh and then creating the child objects from that. Should make a more pleasing shattering type effect with more evenly distributed polys (its a bit ‘chunky’ at the moment).

Fabulous! Looks absolutely fabulous! :smile:

Definitely on my shopping list!

Just one question - what’s Toolkit 2D? Do you mean Unity’s own 2D system or 2D TOOLKIT?

BTW, if you want to attache screenshot, its better you just show them inside your post using [/B] tag.
It saves the user from having to click it. :wink:

Hah - thank you so much, I’d assumed that images were disabled on the boards or something - was just caused by my profile settings! Oops :stuck_out_tongue:

Yeah, sorry, I mean 2D Toolkit - have amended the original post. The 2D Toolkit code internally calls everything TK2D, so I’ve got into the habit of calling it that :slight_smile:

This is great. Going to pick this up soon!

Added a little tutorial video to the first post :slight_smile:

Hi mrsquare, looks great, but does this have playmaker support?

I haven’t added Playmaker support personally, but I was contacted by some of the Playmaker guys a while back and sent them a copy of the asset so that they could integrate the two systems. I never actually heard back from them though, so not sure what the current status is.

Playmaker thread about it here:

I’ll PM jeanfabre on the Playmaker forums and see if he has an update.

Cool, thanks for the info, I’ll keep checking the thread then.

I’m curious how this plugin works. I’m thinking you

  • create 2 new meshes from the existing sprite rectangle and the line intersect points
  • create a new material at runtime using some custom shader and using the texture from the original sprite
  • uv map new meshes
  • generate new game objects with new meshes
  • create polygon colliders to match the meshes

Is that close to truth? Btw I totally understand if you want to withhold this information and make me buy it to find out :stuck_out_tongue:
All your assets look really awesome, congrats!

Hi,

my aplogies it went off my radar. I had everything! I just wanted to push one more thing, and provide a 100% playmaker swipe management for the interaction, but I guess my brain shut down that task :slight_smile: so SpriteSlicer is now avaible in playmaker, and to provide the desired effect I also added an action to go trhough all the pieces generated so you can add force, delete the previous parent pieces etc etc.

https://hutonggames.fogbugz.com/default.asp?W1177

Sorry for the delay:)

Bye,

Jean

Hi Jean, thanks for going the extra mile on this, much appreciated!

Yep, that’s almost exactly it :slight_smile: There are lots of online tutorials that go into how to slice a Box2D polygon along a given vector (Ray Wenderlich has a particularly good cocos2D one) - Sprite Slicer is essentially a C# implementation of that algorithm, and then all the extra stuff on top to work with Unity materials, gameObjects etc. But yeah - not too hard to implement yourself if you’ve got a bit of coding knowledge.

Thanks for sharing! I guess the real challenge would be getting it to work properly with concave objects where certain slices should result in more than 2 objects. Maybe you could analyze the texture data to find isolated groups of pixels after the initial cut or something? Definitely not a trivial problem

Hi, I’m using Sprite Slicer and love it! I have one issue which is with usage with the PolygonCollider2D. I have sprites that are generated at runtime and then add the PolygonCollider2D component to them, at which time Unity automatically traces the edges of the sprite and sets the vertices for me. The problem is, for pretty much every shape I have, some of which are simple roundish object, others more complex, Sprite Sliver complains that none of them are convex and thus won’t slice them. I then tried disabling the convex check by sedating s_AllowConvexSlicing to true. This works great in the Unity editor, but once I put it on an iPad, you see some very weird textures appear after slicing, and unexpected shapes. Perhaps I should be manually setting the vertices when I add the collider? Will that even work or will it still likely be seen as not convex?

Hey,

Yeah, Unity generates a lot of vertices (far more than it really needs to) when its automatically tracing a sprite. If you zoom in loads you’ll probably see a couple of rogue vertices that are causing it to be a concave shape even it doesn’t appear to be one visually.

I don’t really recommend using the s_AllowConvexSlicing flag (oops - that should be s_AllowConcaveSlicing shouldn’t it!) - it might work acceptably on a very few concave objects, but in general its going to cause very weird issues as you’ve seen.

If you can add the vertices yourself rather than letting Unity do it, that would probably be the ideal solution, although if you have a look in the Sprite Slicer code, there’s a static ‘MakeConvex()’ function which attempts to repeatedly trim off vertices from the polygon collider until it becomes convex. Currently this is designed for use in the editor (it’s attached to Tools->Sprite Slicer->Make Convex button), but it shouldn’t be hard to make it work at runtime too.

Yes, I just noticed your editor script. That is super handy! I just brought each sprite into a scene, ran the MakeConvex function, did a little hand tweaking and then exported the vertices to a data file I load at runtime and use to set the collider vertices upon creation. Thanks for making that available.

Hi, I’m playing with Sprite Slicer and I added CircleCollider2D to an object (Fixed Angle is checked in Rigidbody2D) and tried to use SpriteSlicer2D.SliceAllSprites, but it couldn’t be sliced saying

But sometimes it works and the sprite is destroyed (not sliced, just destroyed. Objects might be too small? The radius of the circle collider is 0.4)
Explosion always works though.
SliceManager.cs is my test code that was just copied from the video tutorial. Is there any workaround?

EDIT: Setting a BoxCollider2D worked, but Polygon Collider couldn’t be cut either.

Hi - the fact that ExplodeSprite works and SliceAllSprites doesn’t suggests that the problem isn’t actually with the object you’re trying to cut, but that another invalid object is getting included in the slicing operation. Are there some other concave physics bodies nearby in the scene that might be getting intersected by the slice vector?

You could try using SliceSprite() instead, if you’re only wanting to slice that one particular sprite.

I’m seeing an issue where certain sprites I have can’t be sliced at certain angles. The sprites get instantiated and a polygon collider attached with points data that I’ve tweaked by hand. The process is, I add a sprite in the scene, add the collider, select it, run the MakeConvex script. Inevitably I have to adjust some points to make it match the shape, and then I run MakeConvex again. I then export the points to a text file, and then load them in later at runtime. Most of my objects slice just fine, but a few won’t slice at certain angles, I get the warning that vertices were rejected. Any way I can tell whether a sprite will be sliceable from all angles of approach?