void Update() {
#if UNITY_EDITOR
OnValidate();
#endif
Mesh mesh = new Mesh();
PolygonCollider2D _polygon2d = gameObject.GetComponent<PolygonCollider2D>();
BoxCollider2D _box2d = gameObject.GetComponent<BoxCollider2D>();
CircleCollider2D _circle2d = gameObject.GetComponent<CircleCollider2D>();
EdgeCollider2D _edge2d = gameObject.GetComponent<EdgeCollider2D>();
if (_polygon2d) {
// points are alredy rotated :)
int pointCount = _polygon2d.GetTotalPointCount();
Vector2[] points = _polygon2d.points;
Vector3[] vertices = new Vector3[pointCount];
for (int j = 0; j < pointCount; j++) {
Vector2 actual = points[j];
vertices[j] = new Vector3(actual.x, actual.y, 0);
}
Triangulator tr = new Triangulator(points);
int[] triangles = tr.Triangulate();
mesh.vertices = vertices;
mesh.triangles = triangles;
}
if (_box2d) {
mesh.vertices = GetBoxCorners(_box2d);
int[] triangles = {0, 1, 2, 1, 3, 2};
mesh.triangles = triangles;
}
if (_circle2d) {
float scale = 1f / 16f;
Vector3[] vertices = new Vector3[16];
Vector2[] points = new Vector2[16];
for (int j = 0; j < 16; j++) {
float x = (_circle2d.offset.x +
Mathf.Cos(scale * j * 2 * Mathf.PI) * _circle2d.radius) *
_circle2d.transform.localScale.x;
float y = (_circle2d.offset.y +
Mathf.Sin(scale * j * 2 * Mathf.PI) * _circle2d.radius) *
_circle2d.transform.localScale.y;
points[j] = new Vector2(x, y);
vertices[j] = new Vector3(x, y, 0);
}
Triangulator tr = new Triangulator(points);
int[] triangles = tr.Triangulate();
mesh.vertices = vertices;
mesh.triangles = triangles;
}
if (_edge2d) {
Debug.LogWarning("EdgeCollider2D is not supported");
}
_mf.mesh = mesh;
}