Search Unity

Question Edit Terrain Lightmap Scale via Code

Discussion in 'Global Illumination' started by Zylex, Aug 20, 2020.

  1. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    We are automating our lightmapping bake setup but in order to do so we also need access to the Lightmap Scale setting of the terrain:



    Is there any way to access that value? Same goes for the Lightmap Parameters.
     
  2. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    320
    Hello,
    These fields are not publicly exposed, but you can still edit them using a SerializedObject.
    Here is some example code to get you started:
    Code (CSharp):
    1. // Create SerializedObject from Terrain component
    2. SerializedObject s = new SerializedObject(myTerrainGameObject.GetComponent<Terrain>());
    3.  
    4. // Find and edit properties      
    5. s.FindProperty("m_ScaleInLightmap").floatValue = myScale;
    6. var currentParameters = s.FindProperty("m_LightmapParameters").objectReferenceValue as LightmapParameters;
    7. s.FindProperty("m_LightmapParameters").objectReferenceValue = myLightmapParameters;
    8.  
    9. // Remember to apply changes when done editing
    10. s.ApplyModifiedProperties();
     
    TerraUnity likes this.
  3. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    Thanks for the answer!
     
  4. joeysipos

    joeysipos

    Joined:
    Jul 28, 2015
    Posts:
    41
    Thanks for the answer! I would have never figured this out. Any plans to make these field publicly exposed?