Unity2D - Runtime creation of PolygonCollider2D collision data with 2D "voxel" mesh data
Hello, This is a short post showing how to generate the collision data for a PolygonCollider2D using 2D " voxel " data using the most basic of methods. This tutorial exists because I struggled to find something like this. Please note that this solution uses minimal optimization methods; there are better solutions out there. This function assumes you already have a Mesh created with some 2D pixel system that contains vertex and triangle data. // Custom collider function static void GenerateCollider(PolygonCollider2D polygonCollider2D, Vector3[] vertices, int[] triangles) { // Set paths to 0 to remove old data; default polygonCollider2D.pathCount = 0; // Loop through triangle data for(var i = 0; i < triangles.Length; i += 3) { // Increment pathCount polygonCollider2D.pathCount++; // Add 3 vertices for a triangle polygonCollider2D.SetPath(i / 3, ...