Auto Generate and Generate Lighting

確認済のバージョン: 2018.1


難易度: Advanced

Auto Generate

Unity enables Auto Generate by default in all new Scenes. To disable Auto Generate, go to the Lighting Settings (menu: Window > Lighting > Settings) window, scroll down and uncheck the Auto Generate checkbox.

When you enable Auto Generate and a Scene makes use of Baked Global Illumination (GI) or Precomputed Realtime GI, Unity rebuilds the lighting data as you edit the Scene. This means that the backend runs every time the elements in the Scene are changed in a way that affects the baked data; for example, deleting a static GameObject from the Scene triggers a bake.

You should enable Auto Generate only when you are quickly iterating on the lighting while working with a single Scene. In all other cases, you should disable Auto Generate and generate the lighting data manually.

Generate Lighting

Once you disable Auto Generate, you can generate lighting data by clicking the Generate Lighting button. This button can be found in the Lighting Settings window, next to the Auto Generate checkbox.

Lighting Data Asset

When you click the Generate Lighting button, Unity creates a Lighting Data Asset in the Project. This Lighting Data Asset contains the Scene's GI data, along with supporting data Unity uses to create the lighting for the Scene. The Lighting Data Asset is linked to the Scene.

When you use Auto Generate, Unity does not create a Lighting Data Asset in the Project. Instead, Unity stores the GI data and other lighting data for that Scene in memory. This is for two reasons:

  • Making changes to data in memory is much quicker than writing data to a file.
  • Making changes to a Lighting Data Asset flags the Scene the Asset is linked to as dirty, which in turn triggers Unity to reserialize that Scene. Auto Generate is intended to promote fast iteration, so it is important that it does not trigger these resource-intensive operations every time it runs.

Testing Scenes with Auto-Generated Lighting Data in Play Mode

The lack of a Lighting Data Asset on disk can lead to problems when testing Scenes in Play Mode. Put simply, Scenes with auto-generated lighting data only appear with all of their correct GI data in Play Mode is in the following scenarios:

Scenario 1:

  • A Scene is open for single-Scene editing in Edit Mode

  • You immediately transition from editing only that Scene in Edit Mode into testing only that Scene in Play Mode

Scenario 2:

  • A collection of Scenes are open for multi-Scene editing in Edit Mode

  • You immediately transition from editing that set of Scenes in Edit Mode to testing that set of Scenes in Play Mode

Any other set of circumstances cause Scenes that use auto-generated lighting data to appear noticeably wrong in Play Mode. Direct and Realtime lighting (for example, the Skybox or Ambient Source data) continue to function, but GI effects are missing.

Problems with Auto-Generated Lighting Data in Play Mode

Unloading Scenes in Play Mode

When you test a single Scene or collection of Scenes with auto-generated lighting data in Play Mode as above, Unity loses the GI data for those Scenes when those Scenes are unloaded.

This is because when Unity enters Play Mode for a Scene with auto-generated lighting, it attaches GI data directly to the GameObjects in that Scene. When Unity unloads the Scene, it destroys the GI data along with the GameObjects. Because there is no Lighting Data Asset on disk for Unity to reload the GI data from, Unity cannot recover this data without returning to Edit Mode. This means that if Unity loads the same Scene or Scenes again within the same Play Mode session, the Scene or Scenes appear without GI.

Loading additional Scenes in Play Mode

When you open a single Scene and enable Auto Generate in Edit Mode, Unity loads GI data for that Scene from its GI cache (if available) or generates new GI data for that Scene. This GI data resides in memory along with the other lighting data required for the Scene, and is correctly associated with the Scene when you enter Play Mode.

When Unity loads a Scene baked with Auto Generate using a user script in Play Mode, its GI data isn't resident in memory. If you load the Scene non-additively, this appears obviously wrong. If you load the Scene additively, the effects may be more subtle and the cause may not be initially obvious.

Multi-Scene Editing

When multiple Scenes are open for editing with Auto Generate enabled, Unity stores the lighting data for this specific combination of Scenes in memory.

As long as you only want to test this specific combination of Scenes (by immediately entering Play Mode), you can use this as a way to quickly iterate on the lighting for these Scenes. When you enter Play Mode with the Scenes open, Unity does not need to load any state from disk as all the Scene elements already have their GI data attached.

However, if you want to test a different combination of Scenes in Play Mode, or to load these same Scenes but in a different manner (for example, by loading Scene A first and then additively loading Scene B and Scene C), you will experience problems. Any Scenes with auto-generated lighting that you do not open immediately in Play Mode will not have GI data.