procedural mesh generation: extruding 2d shapes

Hello, I have a 2d game, where player can procedurally create 2d shapes, though, as I understand, if I want those objects to collide with other objects (and recieve ray hits etc) that 2d shape generated will need to be extruded (and after that, having a mesh collider added, etc);

is there any code around that might help with extrusion?

There are a few scripts that deal with dynamic meshes on: I am sure you can dig around and take out the relevant parts. I haven't opened the package but : might be a good place to look.

If I get your meaning, all your objects are single-sided 2D planes shaped with the contours of your shape and you want is to detect collisions between such objects.

The real problem is that in order to extrude, you need to:

  1. know which vertices to extrude and they're connections
  2. maybe also knowing what normals to use if the RecalculateNormals is giving you results that you don't want.
  3. create new vertices and faces on the sides of the extrusion.

Tracing the contour is not so straightforward. Your object can be laid out in any arbitrary configuration, so there's really no way of knowing if a vertex is on an outside edge, especially if it is concave. If it is a convex shape, you could use some optimized and publicized convex-hull algorithms.

Once you've found the contour, you would create all of the vertices along your contour as well as duplicates that are offset by some depth. After this, you would define triangles for them. Whether you share vertices or not is up to you, but if you do, then you can call Optimize and Unity will convert the triangles to triangle strips which are faster to render.

To create the new vertices and faces would be something like:

var offset : float = 1.0f;
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Array = Array(mesh.vertices);
var triangles : Array = Array(mesh.triangles);

//Assumes convexHull is a Vector3[] of vertices in the order which they are
//connected and which share vertices has been found
for(var i : int = 0; i < convexHull.Length; i++) {
    Vector3 vert = convexHull*;*
 *vert.z -= offset;*
*var index : int = mesh.vertices.Length;*
*for(; index < vertices.Length - 1;index++) {*
 *triangles.Add(index++); //0*
 *triangles.Add(index++); //1*
 *triangles.Add(index);   //2*
 *triangles.Add(index);   //2*
 *triangles.Add(index-1); //1*
 *triangles.Add(++index); //3*
*//loop back*
*index = vertices.Length - 2;*
*var index2 : int = mesh.vertices.Length;*
*triangles.Add(index++);  //0*
*triangles.Add(index);    //1*
*triangles.Add(index2);   //2*
*triangles.Add(index2++); //2*
*triangles.Add(index);    //1*
*triangles.Add(index2);   //3*
*mesh.vertices = vertices.ToBuiltin(Vector3);*
*mesh.triangles = triangles.ToBuiltin(int);*