Auto Generate and Generate Lighting

Geprüft mit Version: 2018.1

-

Schwierigkeitsgrad: 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 press 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. Firstly, making changes to data in memory is much quicker than writing data to a file. Secondly, 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. As Auto Generate is intended to promote fast iteration, 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, the only time that Scenes with auto-generated lighting data appears with all of their correct GI data in Play Mode is if the following criteria are met:

Either:

  • 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

Or:

  • 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 will be missing.

Problems with Auto-Generated Lighting Data in Play Mode

Unloading Scenes in Play Mode

When a single Scene or collection of Scenes with auto-generated lighting data are tested in Play Mode as above, the GI data for those Scenes are lost 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, the GI data is destroyed along with the GameObjects. As there is no Lighting Data Asset on disk from which Unity can reload the GI data, 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 a Scene baked with Auto Generate is loaded using a user script in Play Mode, its GI data isn't resident in memory. If you load the Scene non-additively, this will be 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 wish to test only this specific combination of Scenes by immediately entering Play Mode, it is very quick to 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 wish 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.