Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Access Lighting window properties in Script ?

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

  1. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    38
    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:
    54
    it's my question
     
  3. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    4,580
    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:
    4,580
    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,479
    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:
    91
    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:
    86
    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:
    86
    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:
    436
    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:

  14. yakandco

    yakandco

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

    yakandco

    Joined:
    Dec 3, 2014
    Posts:
    86
    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:
    436
    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");
     
    laurentlavigne likes this.
  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:
    436
    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:
    436
    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:
    436
    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:
    82
    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:
    436
    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:
    82
    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. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    2,865
    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. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    2,865
    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:
    389
    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. UnityLighting

    UnityLighting

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

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,274
    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:
    450
    IS there any of that Lightmapping class acess in Engine in build not only editor? Anyone knows ?
     
  37. akuserucho

    akuserucho

    Joined:
    Nov 4, 2016
    Posts:
    4
    I need this as well... trying to set the lighting data asset at runtime.
     
  38. kblood

    kblood

    Joined:
    Jun 20, 2012
    Posts:
    82
    Pretty sure it does not exist in the build. I was trying to access this while trying to mod Satellite Reign and I changed some settings on the camera, but generally lighting is something you bake in the editor before you compile a build. This is why there are editor specific code, because once the game is compiled, you wont have these options. One reason is that it is pretty platform specific, another reason is that it often requires read and write access to the games asset files. They are not meant to be changed at run time.

    I hoped to be able to switch to realtime GI, but that does not seem possible either.
     
  39. JenniferNordwall

    JenniferNordwall

    Unity Technologies

    Joined:
    Sep 19, 2016
    Posts:
    160
    Hey! All Lightmapping is Editor only. If you have a Lighting Settings asset, that lives in Engine land and have 3 properties available during Runtime (bakedGI, realtimeGI and environment lighting mode (realtime/baked))

    What are you guys trying to do?
     
  40. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    436
    In my case I needed to automatically setup lighting for multiple scenes. As of Unity 2020.1 (maybe earlier) there is a Lighting Settings that you can assign in Lighting window so I don't need these dirty hacks anymore.
     
  41. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    324
    Anyone know how to toggle the "Auto Generate" option via script? I've been trying:

    Code (CSharp):
    1.         public static void SetAutoGenerateGI(bool val)
    2.         {
    3.             SetInt("m_GIWorkflowMode", val ? 1 : 0);
    4.         }
    5.  
    with the LightmapSettingsHelper script posted above. Tried with SetBool instad of SetInt too.

    Look at the source, it seems like changing this serialized propery should work.
     
  42. NarryG

    NarryG

    Joined:
    Mar 11, 2020
    Posts:
    6
    My specific use-case is that I have a project where the active scene always has to stay as the main scene (I'm using Mirror networking), and I'm additively loading subscenes and teleporting the player around. I need those subscenes to have the correct skybox/fog/etc, but I want to be able to configure it for the scenes in the editor, then just grab that data at runtime & apply it to the active main scene.

    So essentially, I need the information that the non-main but loaded scene would be using for its skybox, fog, etc, so I can then apply it to the main scene.


    There's a UnityEditor function, Unsupported.SetOverrideLightingSettings that does EXACTLY what I need, but this seems to be editor only.
     
    Last edited: Apr 29, 2020
  43. JenniferNordwall

    JenniferNordwall

    Unity Technologies

    Joined:
    Sep 19, 2016
    Posts:
    160
    Which version?
    20.1 https://docs.unity3d.com/2020.1/Documentation/ScriptReference/LightingSettings-autoGenerate.html
    19.3 or lower: https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Lightmapping-giWorkflowMode.html
    It needs to be set to an enum
     
    hungrybelome likes this.
  44. JenniferNordwall

    JenniferNordwall

    Unity Technologies

    Joined:
    Sep 19, 2016
    Posts:
    160
    Hi

    So the Unsupported.SetOverrideLightingSettings method was made for us so that we can show a prefab or other minor preview scene that we don't like being affected by whatever lighting and render settings in the main scene. It only temporarily does this and isn't meant to be used in production.
    What is preventing you from setting the loaded scene as the active? Or copying over it's settings to the active scene? Not sure about your setup, but in the later versions it should be easy to just swap the whole LightingSettings object (new in 20.1) over to the other scene, and then setting whatever render settings (fog etc) manually.
    Hope this works :)
     
  45. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    324
  46. NarryG

    NarryG

    Joined:
    Mar 11, 2020
    Posts:
    6
    The way additive scenes work in the Mirror Networking addon is that you always have to keep a main scene active, then you load the other scenes additive and move the gameobjects around. I poked the devs of the addon and they said there were a variety of reasons it worked that way, but the main one was because when sending objects to spawn on the client, there's a lot of assumptions made about the scene they're in. As such, the technique they used is to have a mainscene that's always active, then spawn networked objects in the main scene then move them to their respective scenes.

    The LightingSettings will help, but that still means I'll need to somehow get the fog value out of each scene so I can then override the fog in the main scene. I could just write the fog in a script and then apply it at runtime, but that means I can't use the editor functionality.

    The issue isn't changing the value, the issue is that the value isn't available unless the scene is active. As far as I could tell, there is no way to get that information out of the scene without setting it to be the active scene and, if I make it the active scene, it can break the networking plugin.

    I came up with a dirty workaround where, on build, I parse all the scenes and copy the settings into a RenderSettings wrapper scriptableobject, that is then used to pull the information at runtime. It works well enough for my needs, but getting lighting/fog information from another scene doesn't seem like something that should require jumping through hoops.
     
  47. AndreaPablo

    AndreaPablo

    Joined:
    Dec 31, 2019
    Posts:
    2
    so, it's greater, than just good. thank you really much, it is very helped me.
     
    thefranke likes this.
  48. Super_Solomob422

    Super_Solomob422

    Joined:
    Jul 7, 2019
    Posts:
    7
    Aye this helps a lot.
     
  49. NarryG

    NarryG

    Joined:
    Mar 11, 2020
    Posts:
    6
    Here's a non-reflection based method for 2019.4 in-case you can't use reflection. This bakes the settings into scriptableobjects at build time which you can then use later.

    Wrapper object
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3.  
    4. namespace CommGame.Helpers
    5. {
    6.     public class RenderSettingsWrapperObject : ScriptableObject
    7.     {
    8.         public float ambientSkyboxAmount;
    9.         public bool fog;
    10.         public float fogStartDistance;
    11.         public float fogEndDistance;
    12.         public FogMode fogMode;
    13.         public Color fogColor;
    14.         public float fogDensity;
    15.         public AmbientMode ambientMode;
    16.         public Color ambientSkyColor;
    17.         public Color ambientEquatorColor;
    18.         public Color ambientGroundColor;
    19.         public float ambientIntensity;
    20.         public Color ambientLight;
    21.         public Color subtractiveShadowColor;
    22.         public Material skybox;
    23.         public Light sun;
    24.         public SphericalHarmonicsL2 ambientProbe;
    25.         public Cubemap customReflection;
    26.         public float reflectionIntensity;
    27.         public int reflectionBounces;
    28.         public DefaultReflectionMode defaultReflectionMode;
    29.         public int defaultReflectionResolution;
    30.         public float haloStrength;
    31.         public float flareStrength;
    32.         public float flareFadeSpeed;
    33.  
    34.         public void SetRenderSettingsFromWrapper()
    35.         {
    36.             RenderSettings.fog = fog;
    37.             RenderSettings.fogStartDistance = fogStartDistance;
    38.             RenderSettings.fogEndDistance = fogEndDistance;
    39.             RenderSettings.fogMode = fogMode;
    40.             RenderSettings.fogColor = fogColor;
    41.             RenderSettings.fogDensity = fogDensity;
    42.             RenderSettings.ambientMode = ambientMode;
    43.             RenderSettings.ambientSkyColor = ambientSkyColor;
    44.             RenderSettings.ambientEquatorColor = ambientEquatorColor;
    45.             RenderSettings.ambientGroundColor = ambientGroundColor;
    46.             RenderSettings.ambientIntensity = ambientIntensity;
    47.             RenderSettings.ambientLight = ambientLight;
    48.             RenderSettings.subtractiveShadowColor = subtractiveShadowColor;
    49.             RenderSettings.skybox = skybox;
    50.             RenderSettings.sun = sun;
    51.             RenderSettings.ambientProbe = ambientProbe;
    52.             RenderSettings.customReflection = customReflection;
    53.             RenderSettings.reflectionIntensity = reflectionIntensity;
    54.             RenderSettings.reflectionBounces = reflectionBounces;
    55.             RenderSettings.defaultReflectionMode = defaultReflectionMode;
    56.             RenderSettings.defaultReflectionResolution = defaultReflectionResolution;
    57.             RenderSettings.haloStrength = haloStrength;
    58.             RenderSettings.flareStrength = flareStrength;
    59.             RenderSettings.flareFadeSpeed = flareFadeSpeed;
    60.         }
    61.         public void SetWrapperFromRenderSettings()
    62.         {
    63.             fog = RenderSettings.fog;
    64.             fogStartDistance = RenderSettings.fogStartDistance;
    65.             fogEndDistance = RenderSettings.fogEndDistance;
    66.             fogMode = RenderSettings.fogMode;
    67.             fogColor = RenderSettings.fogColor;
    68.             fogDensity = RenderSettings.fogDensity;
    69.             ambientMode = RenderSettings.ambientMode;
    70.             ambientSkyColor = RenderSettings.ambientSkyColor;
    71.             ambientEquatorColor = RenderSettings.ambientEquatorColor;
    72.             ambientGroundColor = RenderSettings.ambientGroundColor;
    73.             ambientIntensity = RenderSettings.ambientIntensity;
    74.             ambientLight = RenderSettings.ambientLight;
    75.             subtractiveShadowColor = RenderSettings.subtractiveShadowColor;
    76.             skybox = RenderSettings.skybox;
    77.             sun = RenderSettings.sun;
    78.             ambientProbe = RenderSettings.ambientProbe;
    79.             customReflection = RenderSettings.customReflection;
    80.             reflectionIntensity = RenderSettings.reflectionIntensity;
    81.             reflectionBounces = RenderSettings.reflectionBounces;
    82.             defaultReflectionMode = RenderSettings.defaultReflectionMode;
    83.             defaultReflectionResolution = RenderSettings.defaultReflectionResolution;
    84.             haloStrength = RenderSettings.haloStrength;
    85.             flareStrength = RenderSettings.flareStrength;
    86.             flareFadeSpeed = RenderSettings.flareFadeSpeed;
    87.         }
    88.     }
    89. }
    90.  
    Generator
    Code (CSharp):
    1. using CommGame.Helpers;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. using UnityEditor;
    5. using UnityEditor.Build;
    6. using UnityEditor.Build.Reporting;
    7.  
    8. public class RenderSettingsWrapperGenerator : EditorWindow, IPreprocessBuildWithReport
    9. {
    10.     public int callbackOrder { get; }
    11.  
    12.  
    13.     public void OnPreprocessBuild(BuildReport report)
    14.     {
    15.         RebuildRenderSettingsWrappers();
    16.     }
    17.  
    18.     [MenuItem("Tools/Narry/Build Rendersettings Caches")]
    19.     private static void RebuildRenderSettingsWrappers()
    20.     {
    21.         for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
    22.         {
    23.             UnityEditor.SceneManagement.EditorSceneManager.OpenScene(SceneUtility.GetScenePathByBuildIndex(i));
    24.             SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(i));
    25.             var scene = SceneManager.GetActiveScene();
    26.             var rswo = ScriptableObject.CreateInstance<RenderSettingsWrapperObject>();
    27.             rswo.SetWrapperFromRenderSettings();
    28.             EditorUtility.SetDirty(rswo);
    29.             var dest = "Assets/Resources/Scenes/" + scene.name + ".asset";
    30.  
    31.             if (!AssetDatabase.IsValidFolder("Assets/Resources"))
    32.             {
    33.                 AssetDatabase.CreateFolder("Assets", "Resources");
    34.             }
    35.  
    36.             if (!AssetDatabase.IsValidFolder("Assets/Resources/Scenes"))
    37.             {
    38.                 AssetDatabase.CreateFolder("Assets/Resources", "Scenes");
    39.             }
    40.  
    41.             AssetDatabase.CreateAsset(rswo, dest);
    42.             AssetDatabase.SaveAssets();
    43.         }
    44.         UnityEditor.SceneManagement.EditorSceneManager.OpenScene(SceneUtility.GetScenePathByBuildIndex(0));
    45.         UnityEngine.SceneManagement.SceneManager.SetActiveScene(UnityEngine.SceneManagement.SceneManager.GetSceneByBuildIndex(0));
    46.     }
    47. }

    Code (CSharp):
    1.  
    2. [SerializeField]
    3. AdditiveScene[] Scenes;
    4.  
    5. [Serializable]
    6.     public class AdditiveScene
    7.     {
    8.         public string SceneName;
    9.         public RenderSettingsWrapperObject RenderSettings;
    10.         public PostProcessProfile PostProcessProfile;
    11.     }
    Code (CSharp):
    1.  
    2. public void SceneLoaded(string sceneName)
    3.         {
    4.             //This sucks but there's no other great way to do it in 2019.4 without a lot of boilerplate I don't feel like dealing with. 2020.1 brings native support for this.
    5.             foreach (var l in GameObject.FindGameObjectsWithTag("DirectionalLight"))
    6.             {
    7.                 l.GetComponent<Light>().enabled = l.scene.name == sceneName;
    8.             }
    9.             var scene = Array.Find(Scenes, x => x.SceneName == sceneName);
    10.  
    11.             SetRenderSettings(scene);
    12.  
    13.             // Force Unity to synchronously regenerate the tetrahedral tesselation for all loaded Scenes
    14.             LightProbes.Tetrahedralize();
    15.         }
    You may need to optimize the scene load code (use a dictionary?) if you have a lot of scenes, but you should be able to adapt this to your needs. If you're using something besides 2019.4, these parameters may have changed so be aware.
     
    nikescar likes this.
  50. nikescar

    nikescar

    Joined:
    Nov 16, 2011
    Posts:
    118
    I was about to write something like this myself but figured I'd see if the work had already been done. As usual, the Unity community already has a solution.

    Thank you for sharing your work!
     
unityunity