Physics2D polygon collider - how to remove shape?

Hello there.
I have an object and Unity automatically creates a polygon collider shape for it when I add one, however, the sprite has some holes and Unity creates a shape for each one, I want to remove them… CTRL+Click won’t remove a shape, only vertex and it won’t remove any if there’s only 3 in a shape.
Any ideas?

Thanks in advance.

Good question; I think the only way to do it currently is with an editor script. Here’s a quick script which I didn’t test much, but seems to work. (Although it won’t work right on polygon colliders with multiple separate shapes, since it just assumes there’s a single exterior shape.) It requires you to click on a GameObject with a PolygonCollider2D with at least 2 shapes before it can be used.

@MenuItem("GameObject/Remove Interior Shapes", true)
static function Validate () {
	var go = Selection.activeObject as GameObject;
	if (!go) {
		return false;
	}
	var pCollider = go.GetComponent(PolygonCollider2D);
	if (!pCollider) {
		return false;
	}
	if (pCollider.pathCount < 2) {
		return false;
	}
	return true;
}

@MenuItem ("GameObject/Remove Interior Shapes")
static function RemoveInteriorShapes () {
    var pCollider = Selection.activeObject.GetComponent(PolygonCollider2D);
	Undo.RecordObject (pCollider, "Remove Interior Shapes");
    var exteriorShape = 0;
    var leftmostPoint = Mathf.Infinity;
    for (var i = 0; i < pCollider.pathCount; i++) {
    	var path = pCollider.GetPath (i);
    	for (var j = 0; j < path.Length; j++) {
    		if (path[j].x < leftmostPoint) {
    			exteriorShape = i;
    			leftmostPoint = path[j].x;
    		}
    	}
    }
    
    path = pCollider.GetPath (exteriorShape);
    pCollider.pathCount = 1;
    pCollider.SetPath (0, path);
}

–Eric

Thank you Eric.

Too bad there’s no native solution for such a simple thing, generally Unity creates complex shapes for hollowed sprites and I like to manually create the collision shape myself. Perhaps a simple feature like “CTRL+SHIFT+CLICK” allowing you to either remove an entire shape or adding a new one would be useful and could do the trick I think, anyway, my personal workaround was to change the sprite to something like a square and use the shape as a basis to creating a more complex one.

That script seems quite useful btw. Very good. Thank you very much. I think it’s specially useful in cases when Unity actually is capable of creating a good shape and you just want to remove the ‘holes’, however, for manually setting the shapes I personally prefer to start with something simple. Perhaps it’s possible to further develop the script adding an option to ‘simplify’ the shape :). I’m sure the devs will come up with something fancy in the next version so don’t bother doing something more sophisticated only for me :).

Thank you again.

Best Regards.

Hi,

I adapted to code to work with multiple selected gameobjects in case anyone needs that. I had to remove internal shapes from ore than 100 objects, so i needed that.

Here is the code http://forum.unity3d.com/threads/210443-Create-from-sprite-missing?p=1528675#post1528675

Thanks for the code Eric!