Which gives me the following UV map:
[![enter image description here][2]][2]
float scaleFactor = 0.5f;
int[] tris = mesh.triangles;
Vector3[] verts = mesh.vertices;
Vector2[] uvs = new Vector2[verts.Length];
// Iterate over each face (here assuming triangles)
for(int index = 0; index < mesh.trianglestris.Length; index += 3)
{
// Get the three vertices bounding this triangle.
Vector3 v1 = verts[mesh.triangles[index]];verts[tris[index]];
Vector3 v2 = verts[mesh.triangles[indexverts[tris[index + 1]];
Vector3 v3 = verts[mesh.triangles[indexverts[tris[index + 2]];
// Compute a vector perpendicular to the face.
Vector3 normal = Vector3.Cross(v3 - v1, v2 - v1);
Debug.Log("surface " + index/3 + " : " + (normal*0.5f).magnitude);
// Form a rotation that points the z+ axis in this perpendicular direction.
// Multiplying by the inverse will flatten the triangle into an xy plane.
Quaternion rotation = Quaternion.Inverse(Quaternion.LookRotation(normal));
// Assign the uvs, applying a scale factor to control the texture tiling.
uvs[mesh.triangles[index]]uvs[tris[index]] = (Vector2)(rotation * v1) * scaleFactor;
uvs[mesh.triangles[indexuvs[tris[index + 1]] = (Vector2)(rotation * v2) * scaleFactor;
uvs[mesh.triangles[indexuvs[tris[index + 2]] = (Vector2)(rotation * v3) * scaleFactor;
}
The red triangle on the left and the dark blue one on the right should be the same shape/size: