Search Unity

Access Lighting window properties in Script ?

Discussion in 'Scripting' started by Eldoir, May 26, 2015.

  1. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    33
    Hello,

    You just read my question - I would know if it is possible to access the "Reflection Bounces" property (not only that one but it's my priority) of the "Lighting" window in a script, to modify it in runtime ?

    Thanks in advance for your response,
    Regards
     
  2. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    51
    it's my question
     
  3. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    3,732
    I've been looking all over for this as well; there doesn't seem to be a way to adjust all of the lighting window properties via script; some are under RenderSettings, but some (like turning off pre-computed or runtime GI) only seem to be accessible in the editor..
     
  4. Wheresmind

    Wheresmind

    Joined:
    Oct 31, 2013
    Posts:
    5
  5. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    3,732
    Unfortunately not - I want to turn off realtime GI and baked GI when creating a new scene from script, but there doesn't seem to be a way to do this..
     
    protopop likes this.
  6. Wheresmind

    Wheresmind

    Joined:
    Oct 31, 2013
    Posts:
    5
    Sorry I meant that RenderSettings.reflectionBounces might the answer to :

    .
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,470
    It depends on what you are trying to do. Most of what you see in the Scene tab of the Lighting Window. Can be accessed as static properties on the RenderSettings class but most of the per object stuff in the object tab you have to access on the individual Renderers. Some of this data is still hidden, and you will have to access by finding SerializedProperty's on the Renderers SerializedObject.
     
  8. jnt

    jnt

    Joined:
    Sep 30, 2014
    Posts:
    15
    I am currently writing an editor script to bake lightmaps for multiple scenes in our game. As you can imagine this could save tons of time.

    The script references a 'master' scene in which I set all the Lighting window attributes that I want for baking, opens each scene that I want to bake, applies all the attributes from the 'master' scene and bakes the lightmaps.

    The problem is, like some of the other posters, I can't find all these settings in script. This is what I have so far:

    Key:
    black - I think I've found it.
    green - I don't need it
    red - I can't find it
    LightingWindowAttributes.png

    Has anyone found any more of these attributes? Or could someone from Unity shed some light. I've looked high and low for info on this but can't find any...

    Thanks
     
  9. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    89
    Looking into this myself now, cheers for the map
    In my tests though LightmapEditorSettings.aoMaxDistance isnt linked to that max distance, its probably obsolete but not marked for some reason
     
  10. jnt

    jnt

    Joined:
    Sep 30, 2014
    Posts:
    15
    bump
     
  11. yakandco

    yakandco

    Joined:
    Dec 3, 2014
    Posts:
    85
    Bumping again as well.. we generate a heap of scenes and really need to be able to turn off Precomputed Realtime GI and Baked GI per scene in editor script.

    It's always so frustrating when finding settings have not been exposed to the coding API.. sigh.
     
  12. yakandco

    yakandco

    Joined:
    Dec 3, 2014
    Posts:
    85
    So... I have been able to find the serialized fields in the scene xaml:

    LightmapSettings:
    m_ObjectHideFlags: 0
    serializedVersion: 6
    m_GIWorkflowMode: 1
    m_LightmapsMode: 1
    m_GISettings:
    serializedVersion: 2
    m_BounceScale: 1
    m_IndirectOutputScale: 1
    m_AlbedoBoost: 1
    m_TemporalCoherenceThreshold: 1
    m_EnvironmentLightingMode: 0
    m_EnableBakedLightmaps: 1 <---- Baked GI
    m_EnableRealtimeLightmaps: 1 <---- Precomputed Realtime GI


    But still no API methods to change them. I could parse and edit the xaml file directly, but I hope it's just the case that I can't find the class?!
     
  13. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    32
    Hi folks! You can change those values via reflection.
    Here is a script I wrote for "Baked GI" section of Lighting window. You can change all other properties the same way. All you need to know are property names which you can find in LightingWindow class, decompiler is your friend here :)
    Just place this script into Editor folder.


    Code (CSharp):
    1. using System;
    2. using System.Reflection;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using Object = UnityEngine.Object;
    6.  
    7.  
    8. public static class LightingSettingsHepler
    9. {
    10.     public static void SetIndirectResolution(float val)
    11.     {
    12.         SetFloat("m_LightmapEditorSettings.m_Resolution", val);
    13.     }
    14.  
    15.     public static void SetAmbientOcclusion(float val)
    16.     {
    17.         SetFloat("m_LightmapEditorSettings.m_CompAOExponent", val);
    18.     }
    19.  
    20.     public static void SetBakedGiEnabled(bool enabled)
    21.     {
    22.         SetBool("m_GISettings.m_EnableBakedLightmaps", enabled);
    23.     }
    24.  
    25.     public static void SetFinalGatherEnabled(bool enabled)
    26.     {
    27.         SetBool("m_LightmapEditorSettings.m_FinalGather", enabled);
    28.     }
    29.  
    30.     public static void SetFinalGatherRayCount(int val)
    31.     {
    32.         SetInt("m_LightmapEditorSettings.m_FinalGatherRayCount", val);
    33.     }
    34.  
    35.     public static void SetFloat(string name, float val)
    36.     {
    37.         ChangeProperty(name, property => property.floatValue= val);
    38.     }
    39.  
    40.     public static void SetInt(string name, int val)
    41.     {
    42.         ChangeProperty(name, property => property.intValue = val);
    43.     }
    44.  
    45.     public static void SetBool(string name, bool val)
    46.     {
    47.         ChangeProperty(name, property => property.boolValue = val);
    48.     }
    49.  
    50.     public static void ChangeProperty(string name, Action<SerializedProperty> changer)
    51.     {
    52.         var lightmapSettings = getLighmapSettings();
    53.         var prop = lightmapSettings.FindProperty(name);
    54.         if (prop != null)
    55.         {
    56.             changer(prop);
    57.             lightmapSettings.ApplyModifiedProperties();
    58.         }
    59.         else Debug.LogError("lighmap property not found: " + name);
    60.     }
    61.  
    62.     static SerializedObject getLighmapSettings()
    63.     {
    64.         var getLightmapSettingsMethod = typeof(LightmapEditorSettings).GetMethod("GetLightmapSettings", BindingFlags.Static | BindingFlags.NonPublic);
    65.         var lightmapSettings = getLightmapSettingsMethod.Invoke(null, null) as Object;
    66.         return new SerializedObject(lightmapSettings);
    67.     }
    68.  
    69.     public static void Test()
    70.     {
    71.         SetBakedGiEnabled(true);
    72.         SetIndirectResolution(1.337f);
    73.         SetAmbientOcclusion(1.337f);
    74.         SetFinalGatherEnabled(true);
    75.         SetFinalGatherRayCount(1337);
    76.     }
    77. }
    78.  
     

    Attached Files:

    LightingBox2 likes this.
  14. yakandco

    yakandco

    Joined:
    Dec 3, 2014
    Posts:
    85
    That's brilliant!! Thanks so much, great work!! :)
     
  15. yakandco

    yakandco

    Joined:
    Dec 3, 2014
    Posts:
    85
    That worked perfectly.. I added something like this to our automated scene creation and I can once again control how the scene's are created, thanks.

    Also to add to you method list:

    public static void SetPrecomputedRealtimeGIEnabled(bool enabled)
    {
    SetBool("m_GISettings.m_EnableRealtimeLightmaps",enabled);
    }
     
  16. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    32
    It's great to hear that my script was helpful for someone :)
    Here are some other lighting properties names. I'm too lazy to write wrappers for them, but here they are:

    SerializedProperty property1 = so.FindProperty("m_LightmapEditorSettings.m_Resolution");
    SerializedProperty property2 = so.FindProperty("m_LightmapEditorSettings.m_BakeResolution");
    SerializedProperty property3 = so.FindProperty("m_LightmapEditorSettings.m_Padding");
    SerializedProperty property4 = so.FindProperty("m_LightmapEditorSettings.m_CompAOExponent");
    SerializedProperty property5 = so.FindProperty("m_LightmapEditorSettings.m_AOMaxDistance");
    SerializedProperty property6 = so.FindProperty("m_LightmapEditorSettings.m_TextureCompression");
    SerializedProperty property7 = so.FindProperty("m_LightmapEditorSettings.m_FinalGather");
    SerializedProperty property8 = so.FindProperty("m_LightmapEditorSettings.m_FinalGatherRayCount");

    SerializedProperty property1 = so.FindProperty("m_GISettings.m_AlbedoBoost");
    SerializedProperty property2 = so.FindProperty("m_GISettings.m_IndirectOutputScale");
    SerializedProperty property3 = so.FindProperty("m_LightmapEditorSettings.m_TextureWidth");
    SerializedProperty property4 = so.FindProperty("m_LightmapEditorSettings.m_LightmapParameters");
    SerializedProperty property5 = so.FindProperty("m_LightmapsMode");

    SerializedProperty property1 = so.FindProperty("m_GISettings.m_BounceScale");
    SerializedProperty property2 = so.FindProperty("m_GISettings.m_TemporalCoherenceThreshold");

    SerializedProperty property1 = so.FindProperty("m_GISettings.m_EnableRealtimeLightmaps");
    SerializedProperty property2 = so.FindProperty("m_GISettings.m_EnableBakedLightmaps");
     
  17. Taylor-Libonati

    Taylor-Libonati

    Joined:
    Jan 20, 2013
    Posts:
    15
    Thank you so much for your script! It totally saved me on a project.
     
  18. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    32
    In Unity 5.4.1 this property doesn't work anymore:
    LightmapSettings.lightmapsMode

    To set Directional Mode use this method:
    Code (CSharp):
    1. public static void SetDirectionalMode(LightmapsMode mode)
    2. {
    3.     SetInt("m_LightmapEditorSettings.m_LightmapsBakeMode", (int) mode);
    4. }
     

    Attached Files:

  19. jhybe

    jhybe

    Joined:
    Feb 3, 2013
    Posts:
    9
    Just what I was looking for !! Thanks !!
     
  20. tito91

    tito91

    Joined:
    Aug 30, 2015
    Posts:
    10
    This thread is a great reference. I have a question tho: how to manipulate Ambient Occlusion toggle in Baked GI section? I would like to completely enable/disable AO bake from script.
     
  21. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    32
    With help of the script above use this method: LightingSettingsHelper.SetBakedGiEnabled(bool enabled);
     
  22. tito91

    tito91

    Joined:
    Aug 30, 2015
    Posts:
    10
    Thank you for your effort, but that's not quite what I meant by AO toggle. I've marked it on a picture attached. The setting you mentioned is switching Baked GI completely, and I would like to switch only Ambient Occlusion calculations.
     

    Attached Files:

  23. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    32
    I fould that propery name for you :)

    SetBool("m_LightmapEditorSettings.m_AO", isAoEnabled);
     
  24. tito91

    tito91

    Joined:
    Aug 30, 2015
    Posts:
    10
    Great, that's just what I needed. Thanks for the help.
     
  25. kblood

    kblood

    Joined:
    Jun 20, 2012
    Posts:
    76
    This looks very interesting. I was hoping to use this for the Unity3D game Satellite Reign to test out something with its modding system. But then I found that these scripts use the:

    using UnityEditor;

    line. I guess this makes it impossible for me to actually change this during runtime? Or can I somehow load another DLL or something to make it possible to with a mod, do these things during runtime?

    Why is anyone even doing this if it can only be done while in the editor? To change how you build the scenes?
     
  26. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    32
    Yes, this only works in editor. There is no sense to change lighting settings at runtime because lighmap baking is editor only feature.
    I need to change that settings in editor to keep light settings the same across several scenes.
     
  27. kblood

    kblood

    Joined:
    Jun 20, 2012
    Posts:
    76
    You are saying there is no reason to turn off the Precomputed Realtime GI? Because it might be baked in the editor, but I would think the realtime part does affect CPU or GPU use in game. I want to make sure it does not. I want to make everything ambient light if possible, during runtime.

    I am thinking I will iterate over all game objects in the game, and find all light components and disable them. But the lighting settings though... I have tried making a new map and set it to active during runtime, but that did not change anything. I guess a new scene has some default light settings or copies them from the currently active map. The modding system might allow for me to add a scene to the game, but I have not figured out how to use it yet. Seems likely I can only use the scripts I add to the DLL.
     
  28. noah_petro

    noah_petro

    Joined:
    Jun 20, 2017
    Posts:
    6
    What if I wanted to change the reflection source from Skybox to Custom via script?
     
  29. LightingBox2

    LightingBox2

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

    LightingBox2

    Joined:
    Mar 31, 2015
    Posts:
    2,815
    Another example for Lightmap parameterrs (and all object type):
    Code (CSharp):
    1. public static void SetLightmapParameters(LightmapParameters parameter)
    2.     {
    3.         SetObject("m_LightmapEditorSettings.m_LightmapParameters", (Object) parameter);
    4.     }
    5.  
    6.  
    7.     public static void SetObject(string name, Object val)
    8.     {
    9.         ChangeProperty(name, property => property.objectReferenceValue = val);
    10.     }
    11.  
    Usage:
    Code (CSharp):
    1. LightmapParameters a = new LightmapParameters ();
    2.             a.name = "Lighting Box Very-Low";
    3.             a.resolution = 0.125f;
    4.             a.clusterResolution = 0.4f;
    5.             a.irradianceBudget = 96;
    6.             a.irradianceQuality = 8192;
    7.             a.modellingTolerance = 0.001f;
    8.             a.stitchEdges = true;
    9.             a.isTransparent = false;
    10.             a.systemTag = -1;
    11.             a.blurRadius = 2;
    12.             a.antiAliasingSamples = 8;
    13.             a.directLightQuality = 64;
    14.             a.bakedLightmapTag = -1;
    15.             a.AOQuality = 256;
    16.             a.AOAntiAliasingSamples = 16;
    17.             a.backFaceTolerance = 0.9f;
    18.  
    19.             LightingSettingsHepler.SetLightmapParameters (a);
     
  31. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    325
    i tried to write an script but got this error:
    error CS0120: An object reference is required to access non-static member `UnityEditor.LightmapParameters.resolution'

    and this is the code:
    LightmapParameters.resolution = 16;

    how to solve it?

    thanks.
     
  32. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    802
    You need an instance of the class to change that field!

    Code (CSharp):
    1. LightmapParameters lightParams = new LightmapParameters();
    2.  
    3. // Some resolution
    4. lightParams.resolution = 1234;
     
  33. gilgsjr

    gilgsjr

    Joined:
    Nov 1, 2018
    Posts:
    1
    what Editor folder?

    I could not find this folder..
     
  34. LightingBox2

    LightingBox2

    Joined:
    Mar 31, 2015
    Posts:
    2,815
    Create a new folder called Editor
     
  35. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,105
    Ah, turns out that at least in Unity 2019.1, and also 2018.4, this is now much easier (I haven't looked at earlier Unity versions, so could very well be that this works for all of 2018, or even earlier):

    Lightmapping
    LightmapEditorSettings
    For my purposes, this does all that I need. Someone would have to check if the reflection-based methods gives us access to anything that is not exposed in modern versions of Unity.
     
    idbrii and Katunator like this.
  36. Jakub_Machowski

    Jakub_Machowski

    Joined:
    Mar 19, 2013
    Posts:
    338
    IS there any of that Lightmapping class acess in Engine in build not only editor? Anyone knows ?