How to create a procedural mesh with multiple materials?

I'd like to create a procedural mesh that makes use of multiple materials. How can this be done? I don't see any way from the mesh API to be able to change the material assigned to triangles? I understand the materials are set in the MeshRender, but how does the material renderer know which material to assign during rendering the different parts of the mesh?

Use Mesh.subMeshCount and Mesh.SetTriangles(), and assign materials with Renderer.materials. They go in order (the first material goes with the first submesh, etc.).