In the documentation for Polygon Collider 2D (and videos) there is an section “Create From Sprite”, seems to be missing here completely… anyone else?
That’s from an earlier version; guess the docs weren’t updated properly for the released version. At least for the “edit collider” toggle, you can hold shift instead. The other stuff was removed.
–Eric
So there’s no automatic way to optimize/poly-reduce the mesh now then? Seems a little silly to remove that, I can understand the alpha threshold being extraneous, but I can’t see a way to get rid of many points quickly and some points can’t be removed at all if it generates them internally on the mesh.
The alpha threshold was useful. I was told those features were removed because they were having some issues with them, but they might return eventually. You can remove any points by holding down control while you’re editing the collider. The collider you use doesn’t have to bear any resemblance to the auto-generated collider if you don’t want it to.
–Eric
I understand that you can manually remove points using the control key, but you can’t remove internal points that way and it’s going to be a lot of work with many meshes or even just a single larger/more complex shape. It’s not the end of the world as 2d path reduction algorithms aren’t complex to make yourself, but it just seems an odd thing to omit and makes a bit of a nuisance. Hopefully they’ll be back.
You can remove almost any points that way, except that it won’t let you have fewer than 3 points in a polygon (internal or external). Also, a possibility would be to make an image in the shape of the polygon collider that you want, add the polygon collider component to auto-generate the collider, then swap out the image for a different one.
–Eric
My sprite has a bunch of transparency and it generates a poly that’s very complex and doesn’t edit effectively… sure would be nice to be able to build a simple shape around the sprite from scratch or have more effective editting capabilities
Just noticed the discussion.
Sorry for the advertisement, but maybe you would like to give 2D ColliderGen a try, it has a lot of options (alpha threshold, vertex count, disable/enable individual holes, etc) to generate 2D or 3D Colliders from Unity’s built-in sprites (and other packages as well).
Cheers,
Harald
Hi,
I want to share an editor script I’m using to remove internal collider shapes from selected gameobjects. It works perfect to remove internal colliders, but it’s useful also to remove all shapes except one in case you only want your collider to have one shape. In this case, be careful because the remaining shape won’t bound perfectly your object in some cases.
One example when it works perfect removing internal shapes:
You have to take care if you use this script to remove all collider shapes except one. Here is an example that what can happen:
You can download the class here: 1528675–88132–$RemoveInternalShapes.cs (1.99 KB)
Hope it helps anyone
PD: I adapted the code from this post, and allowed to work with multiple selected GameObjects http://forum.unity3d.com/threads/225818-Physics2D-polygon-collider-how-to-remove-shape?p=1504572&viewfull=1#post1504572.
/**
* Author: Engidia SCP - Eduard Bosch (eduardbosch@engidia.com)
* File: RemoveInternalShapes.cs
*/
using UnityEngine;
using UnityEditor;
using System.Collections;
public class RemoveInternalShapes : MonoBehaviour {
[MenuItem ("GameObject/Remove Internal Shapes", true)]
static bool Validate ()
{
// Check if there are any selected GameObject with more than one path
foreach (GameObject lObj in Selection.gameObjects)
{
PolygonCollider2D lCollider = lObj.GetComponent<PolygonCollider2D>();
if (lCollider!=null lCollider.pathCount > 1)
{
return true;
}
}
return false;
}
[MenuItem ("GameObject/Remove Internal Shapes")]
static void RemoveShapes ()
{
foreach (GameObject lObj in Selection.gameObjects)
{
PolygonCollider2D lCollider = lObj.GetComponent<PolygonCollider2D>();
if (lCollider==null)
{
continue;
}
// Allow undo action
Undo.RecordObject (lCollider, "Remove Interior Shapes");
// Get the shape that are more to the left than the others to take it as the exterior path
int lExteriorShape = 0;
float lLeftmostPoint = Mathf.Infinity;
Vector2[] lPath;
for (int i=0, length=lCollider.pathCount; i<length ; ++i)
{
lPath = lCollider.GetPath(i);
foreach (Vector2 lPoint in lPath)
{
if (lPoint.x < lLeftmostPoint)
{
lExteriorShape = i;
lLeftmostPoint = lPoint.x;
}
}
}
// Initialize collider with exterior path
lPath = lCollider.GetPath (lExteriorShape);
// Set only the exterior path
lCollider.pathCount = 1;
lCollider.SetPath (0, lPath);
}
}
}
Hello Eduard
Thx for your script, i’m interested on using it, but I don’t get how to.
Do you have to place the .cs on the objects you wish to use it on ?
and from then, what action will I have to do ?
Thx.
It’s an editor script, so you just add the script to a folder called Editor in your project view. Then a menu item shows up for you to use under GameObject.
Hope that helps, thanks!
Thx a lot
I actually found this recently for free too:
(Edit: it seems it’s now $5 ;(… not expensive but too bad it’s not free anymore)
Unity Advanced Polygon Collider : http://u3d.as/mZU
It worked very well with many options and possible configurations,
Thanks again for your answer and your script !!!