Search Unity

Access Lightmap Scene Settings via Script

Discussion in 'Global Illumination' started by Gizmoi, Mar 15, 2018.

  1. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    I'm trying to set some lighting settings programmatically and I'm really struggling to find how to do it.

    This thread here has some insights, but I don't want to rely on reflection or digging around serialised properties as there's a good chance they'll break whenever we upgrade Unity.

    Can anyone from Unity chime in and tell me if it is and/or why it isn't available via Editor script?
     
  2. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,876
    The only solution is the reflection method to access all functions and variables

    Reflection method works well, why you don't like to use it ?
     
  3. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    It works fine now. But when the API changes or is removed, I get no warning apart from my builds start to fail.
     
  4. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,876
    Add #if UNITY_EDITOR into your code

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using System;
    3. using System.Reflection;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using Object = UnityEngine.Object;
    7.  
    8.  
    9. public static class LB_LightingSettingsHepler
    10. {
    11.     public static void SetBounceIntensity(float val)
    12.     {
    13.         SetFloat("m_GISettings.m_IndirectOutputScale", val);
    14.     }
    15.  
    16.     public static void SetLightmapParameters(LightmapParameters parameter)
    17.     {
    18.         SetObject("m_LightmapEditorSettings.m_LightmapParameters", (Object) parameter);
    19.     }
    20.  
    21.     public static void SetObject(string name, Object val)
    22.     {
    23.         ChangeProperty(name, property => property.objectReferenceValue = val);
    24.     }
    25.  
    26.     public static void SetIndirectSamples(int val)
    27.     {
    28.         SetInt("m_LightmapEditorSettings.m_PVRSampleCount", val);
    29.     }
    30.  
    31.     public static void SetDirectSamples(int val)
    32.     {
    33.         SetInt("m_LightmapEditorSettings.m_PVRDirectSampleCount", val);
    34.     }
    35.  
    36.     public static void SetIndirectResolution(float val)
    37.     {
    38.         SetFloat("m_LightmapEditorSettings.m_Resolution", val);
    39.     }
    40.  
    41.     public static void SetAmbientOcclusion(bool val)
    42.     {
    43.         SetBool("m_LightmapEditorSettings.m_AO", val);
    44.     }
    45.  
    46.     public static void SetAmbientOcclusionDirect(float val)
    47.     {
    48.         SetFloat("m_LightmapEditorSettings.m_CompAOExponentDirect", val);
    49.     }
    50.  
    51.     public static void SetAmbientOcclusionIndirect(float val)
    52.     {
    53.         SetFloat("m_LightmapEditorSettings.m_CompAOExponent", val);
    54.     }
    55.  
    56.     public static void SetAmbientOcclusionDistance(float val)
    57.     {
    58.         SetFloat("m_LightmapEditorSettings.m_AOMaxDistance", val);
    59.     }
    60.  
    61.     public static void SetBakedGiEnabled(bool enabled)
    62.     {
    63.         SetBool("m_GISettings.m_EnableBakedLightmaps", enabled);
    64.     }
    65.  
    66.     public static void SetFinalGatherEnabled(bool enabled)
    67.     {
    68.         SetBool("m_LightmapEditorSettings.m_FinalGather", enabled);
    69.     }
    70.  
    71.     public static void SetFinalGatherRayCount(int val)
    72.     {
    73.         SetInt("m_LightmapEditorSettings.m_FinalGatherRayCount", val);
    74.     }
    75.  
    76.     public static void SetFloat(string name, float val)
    77.     {
    78.         ChangeProperty(name, property => property.floatValue= val);
    79.     }
    80.  
    81.     public static void SetInt(string name, int val)
    82.     {
    83.         ChangeProperty(name, property => property.intValue = val);
    84.     }
    85.  
    86.     public static void SetBool(string name, bool val)
    87.     {
    88.         ChangeProperty(name, property => property.boolValue = val);
    89.     }
    90.  
    91.     public static void SetDirectionalMode(LightmapsMode mode)
    92.     {
    93.         SetInt("m_LightmapEditorSettings.m_LightmapsBakeMode", (int) mode);
    94.     }
    95.  
    96.     public static void ChangeProperty(string name, Action<SerializedProperty> changer)
    97.     {
    98.         var lightmapSettings = getLighmapSettings();
    99.         var prop = lightmapSettings.FindProperty(name);
    100.         if (prop != null)
    101.         {
    102.             changer(prop);
    103.             lightmapSettings.ApplyModifiedProperties();
    104.         }
    105.         else Debug.Log("lighmap property not found: " + name);
    106.     }
    107.  
    108.     static SerializedObject getLighmapSettings()
    109.     {
    110.         var getLightmapSettingsMethod = typeof(LightmapEditorSettings).GetMethod("GetLightmapSettings", BindingFlags.Static | BindingFlags.NonPublic);
    111.         var lightmapSettings = getLightmapSettingsMethod.Invoke(null, null) as Object;
    112.         return new SerializedObject(lightmapSettings);
    113.     }
    114.  
    115. }
    116. #endif
     
  5. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    I think you misunderstand. I have it working via reflection and SerializedProperties, but when the internal API changes, this will stop working with no warning.
     
  6. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,876
    So you must update your asset after released any new versions of the unity

    This is a regular thing
     
  7. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,876
  8. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    But now I have to explicitly test that all of my reflections still work, rather than having Unity correctly deprecate fields and methods. Having to use reflection to access parts of the framework is a workaround, not an accepted solution.

    Also, most of the decompiled versions of Unity have been taken down or are out of date.
    Use Unity's official one if you're interested.
     
  9. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,876
    I think Unity doesn't like us to access his classes so we can use non official way

    I don't know anymore

    I'm happy with reflections