//Function to create quad mesh void OnWizardCreate() { if (meshSource == null) { Debug.LogError("You have to select a mesh"); return; } /// 2d ColliderGen v1.4 takes a "z-thickness" property for the mesh generation and locates /// one plane at z-thicknes/2 and the other at -z-thickness/2. /// Also the amount of vertices we set in the "Outline Vertex Count" property is quadruplied. /// Eg: having an outline of 24 verts will generate a mesh collider of 92 verts. /// So we need to extract only frontal vertices (z = z-thickness/2) and avoid duplicates Vector3[] colliderVerts = meshSource.vertices; HashSet <Vector3> vertsSet = new HashSet <Vector3> (new Vector3Comparer()); for (int i = 0, c = colliderVerts.Length; i < c; ++i) { vertsSet.Add(colliderVerts [i]); } // the set should contain colliderVerts/2 verts according to my analysis if (vertsSet.Count != colliderVerts.Length / 2) { Debug.LogWarning("Set of vertices should contain colliderVerts/2 verts. The set has " + vertsSet.Count + " and colliderVerts/2 = " + colliderVerts.Length / 2); } // although we only take frontal vertices, some of them may be duplicated HashSet <Vector2> verts2DSet = new HashSet <Vector2> (new Vector2Comparer()); float z = sourceZThickness / 2f; foreach (Vector3 v in vertsSet) { if (v.z == z) { verts2DSet.Add((Vector2)v); } } Vector2[] verts = new Vector2[verts2DSet.Count]; verts2DSet.CopyTo(verts); // triangulate Mesh mesh = Triangulator.CreateMesh3D(verts, 0f); // use 0 for no extruding mesh.name = meshName; // it seems that 2D ColliderGen creates meshes normalized inside unitary XY plane center in 0.0, so the UVs are offset by 0.5 Vector2[] uvs = mesh.uv; for (int i = 0, c = uvs.Length; i < c; ++i) { uvs[i].x += 0.5f; uvs[i].y += 0.5f; } mesh.uv = uvs; //Create or Replace asset in database CreateOrReplaceAsset(mesh); //Create game object to locate into the scene if (createGameObject) { GameObject _2d_mesh = new GameObject(gameObjectName); MeshFilter meshFilter = (MeshFilter)_2d_mesh.AddComponent(typeof(MeshFilter)); _2d_mesh.AddComponent(typeof(MeshRenderer)); _2d_mesh.GetComponent <MeshRenderer>().castShadows = false; _2d_mesh.GetComponent <MeshRenderer>().receiveShadows = false; _2d_mesh.GetComponent <MeshRenderer>().sharedMaterial = materialToUse; meshFilter.sharedMesh = mesh; } }