- Home /
Pixel Perfect 2D Polygon Collider
There has to be a way to generate a pixel perfect 2D polygon collider. Even with an image as simple as a 20x20 pixel black circle surrounded by a transparent backround Unitys default polygon collider fails to generate around the corners of the pixels. I don't want an answer saying "You can hold shift + ctrl to add and remove vertices." I want an answer with results.
Preforming the tedious task of moving vertices to match up with grid lines is already a painful task. But becomes that much worse when you want to make a 360*240 custom sprite. Along with the fact that there is no snap settings for collider verticies, the job is a waste of time.
I've looked through post after post and I have seen paid assets, broken links, and the old "Add and remove vertices using shift and ctrl." There has to be a better way using scripting or even a free asset. I have tried making a 3d mesh of the object which doesn't work due to the fact that the 3d and 2d physics engine do not work the same.
There needs to be a solution to a pixel perfect 2d polygon collider generator.
Yes, it's a reasonable feature request. I think the only way to make it happen atm is to write your own Editor extension. It wouldn't be too hard to write an Editor window that replicates the polygon collider editor functionality but includes snapping. Even simpler would be a simple function that goes through each point in the polygon collider and snaps it to the nearest pixel grid point. With this, you could still use Unity's built-in editor and only have to put the points at the approximate positions.
As of now I threw together a messy scipt that snaps the collider points to the nearest tenth of a unity by rounding each point. Though this works it still requires points to be placed and removed close to corners on the map. This is the script as of now:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ColliderSnap : $$anonymous$$onoBehaviour {
PolygonCollider2D col;
void Start () {
}
void Update () {
col = GetComponent<PolygonCollider2D> ();
List<Vector2[]> NewPath = new List<Vector2[]> ();
for (int i = 0; i < col.pathCount; i++) {
NewPath.Add (col.GetPath (i));
}
int PathCount = 0;
float x = 0f;
float y = 0f;
foreach (Vector2[] Path in NewPath) {
for (int j = 0; j < Path.Length; j++) {
x = Path [j].x * 10;
y = Path [j].y * 10;
Path [j].x = ($$anonymous$$athf.Round (x)) / 10;
Path [j].y = ($$anonymous$$athf.Round (y)) / 10;
//Debug.Log ("Path " + PathCount + ": " + Path [j].x + "," + Path [j].y);
}
col.SetPath (PathCount, Path);
PathCount++;
}
}
}
If it works, it's a good start I'd say. You could expose the snap functionality so you can input different values, not only 1/10th. Often, sprite resolution will be 1 pixel per unit, or 16 pixels per unit.
Answer by $$anonymous$$ · Nov 16, 2018 at 01:16 AM
I created a free c# script that does this automatically... https://github.com/RandomiaGaming/Unity-PixelPerfectPolygonCollider2D
Answer by ppehkone · Feb 01, 2019 at 02:27 AM
@jchrist0922 Can you show how to use that script or give an example or tutorial?
When I attach the scripts you provided to an object, unity gives an error stating that it can't read the sprite I feed the script, and when I set the sprite to read/write in the advanced options, your script crashes unity every time (I need to close unity from task manager to get it working again).