How to set up UV for lightmaps for generated meshes?

Hello!

How to I set up the UV coordinates (I suspect it is the second UV set) for a in-editor generated mesh?

I create the mesh in the editor (not in the game) because it is part of my custom editor.

The information is needed, because the Unity Lightmapper only gives complete black lightmaps when I am trying to generate one. (Yes, I set the object to be static)

Thanks and best regards,
Marian Frische

To calculate UVs suitable for lightmapping for your in-editor-created mesh please use:
Unwrapping.GenerateSecondaryUVSet().

Ehhh… I’ve got exactly the same problem, posted here http://forum.unity3d.com/threads/145880-Lightmapping-of-custom-meshes?p=997959#post997959. I also wrote to Unity Support and am waiting for an answer. If I’ve got something I’m gonna share it

I’ve just tried this:

Mesh mesh = new Mesh();

Unwrapping.GenerateSecondaryUVSet(mesh);
mesh.vertices = vertices;
mesh.normals = normals;
mesh.tangents = tangents;
mesh.colors = colors;
mesh.uv = texCoords;
mesh.triangles = triangles;

but it doesn’t work. Also, I tried moving the call to GenerateSecondaryUVSet in various places, interlacing assignment of other attributes but nothing works. The result is the same all the time, that is, I see my object blinking with various parts of the lightmap, depending on where my camera is.

When I do this:

Mesh mesh = new Mesh();

mesh.vertices = vertices;
mesh.uv = texCoords;
mesh.normals = normals;
mesh.tangents = tangents;
mesh.colors = colors;
Unwrapping.GenerateSecondaryUVSet(mesh);
mesh.triangles = triangles;

I get logs in console like this:

Shader wants normals, but the mesh  doesn't have them
Shader wants texture coordinates, but the mesh  doesn't have them

Also, my meshes are solid colored, indicating that base UVs are screwed.

When I tried:

Mesh mesh = new Mesh();

mesh.vertices = vertices;
mesh.uv = texCoords;
mesh.normals = normals;
mesh.tangents = tangents;
mesh.colors = colors;
mesh.triangles = triangles;
Unwrapping.GenerateSecondaryUVSet(mesh);

Still problems as I described. The lightmap is blinking on the mesh depending on where my camera is.