Is it possible to set Terrain Lightmap Resolution in an editor script?

I see that the terrain texture size does not appear to be exposed in the LightmapEditorSettings class for editor scripts. (The resolution refers to the textels/pixel of regular lightmaps)

Is there a way to set this in a script?

3 Answers

3

Actually, it might work with reflection. Import System.Reflection, and try the code below:

 private static void ResizeTerrainLightmap(Terrain terrain, int newSize)
    {
        System.Type myType = typeof(Terrain);
        MethodInfo method = myType.GetMethod("set_lightmapSize", BindingFlags.NonPublic | BindingFlags.Instance);
        method.Invoke(terrain, new object[] { newSize });
    }

Also, I had to save the scene, close and reopen it, so that Unity recognizes the change. Not sure the reason for that, but it worked for me.

No. Terrain.lightmapSize is a protected private variable at this time. Vote to make it public, perhaps as an editor class, in Unity Feedback.

This code exactly what you want

Terrain goTerrain = go.GetComponent<Terrain>();
if (goTerrain)
{
    SerializedObject so = new SerializedObject(copyObjectTerrain);
    so.FindProperty("m_ScaleInLightmap").floatValue = newValue;
    so.ApplyModifiedProperties();
}