Search Unity

Showcase Updating Environment Lighting in Editor like a Build does (no bake)

Discussion in 'Global Illumination' started by Gamebient, May 2, 2022.

  1. Gamebient

    Gamebient

    Joined:
    Mar 8, 2019
    Posts:
    15
    The Problem:
    As far as I can tell, the only way to update or keep your lighting the way you see it in the editor is by enabling Auto Generate in the Lighting Tab. However, for a multi-scene workflow unity advises to generate the lightmaps yourself. This is fine if your scene contains baked and realtime lightings that you want to have full control of.
    In my case, I just needed the directional light as a realtime light source. Always baking the environment, even for realtime(!) lighting just for the editor completely destroys the workflow and clutters your project with baked files you effectively don't need.​
    The Solution:
    First things first, my solution only works if the scene containing your directional light is loaded as your active scene. At least tests with other scenarios didn't work on my end.
    I want to give some credit to someone in this thread who got me to the right tracks. It seems like a method call to
    DynamicGI.UpdateEnvironment();
    recalculates, or rather, rebakes everything about the environment aspect of your lighting.
    Using this is a lifesaver. All you have to do is create a script in an editor folder and add the following few lines of code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEditor;
    4.  
    5. [InitializeOnLoad]
    6. public static class EditorSceneLoadLightUpdate
    7. {
    8.     static EditorSceneLoadLightUpdate()
    9.     {
    10.         SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
    11.     }
    12.  
    13.     private static void SceneManager_activeSceneChanged(Scene arg0, Scene arg1)
    14.     {
    15.         DynamicGI.UpdateEnvironment();
    16.     }
    17. }
    This script subscribes to the activeSceneChanged event of the SceneManager and whenever the active scene changed, updates the ambient/environment lighting.

    I've seen quite some threads about this but everyone just seems to stick to using the "Generate Lighting" option. That's why I made this post so if anybody else looks for a solution to this, there is at least one other solution to consider.​
     
  2. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    Thank you for sharing this workaround. Rest assured that we are looking into how to improve multi-scene lighting workflows. One of our developers, @YvainR has already made some progress on it.