文档

Unity Engine

Download Unity

Unity Engine

Code reloading issues

Understand how code reloading can affect your project.
阅读时间2 分钟最后更新于 5 天前

The Code Reload view displays the results of a Roslyn analyzer that detects code issues that can cause unexpected or non-deterministic behavior if code reload on entering Play mode is disabled in your project (as it is by default). Code reloading can impact project iteration times (in particular, the time it takes to enter and exit Play mode), so it's best practice to fix all the issues, and then keep code reload off.

Code reload overview

For an overview of what code reload is and the effects of keeping it off, refer to Enter Play mode without code reload.
The Roslyn analyzer searches the project's code for types with static fields that would keep their value between different Play mode runs unless reset.

Project Auditor window with the Code Reload view open

Project Auditor raises warning diagnostics if a type has any static fields, automatic properties, or events that are not decorated with either the
[AutoStaticsCleanup]
or the
[NoAutoStaticsCleanup]
attribute. The analyzer emits warnings with the
UAL
prefix.

Display code reload issues

To display issues in the Code Reload view, you must enable the Use Roslyn Analyzers setting in the Preferences window. The use of Roslyn analyzers can increase Project Auditor's analysis time, so it's disabled by default.
To allow Project Auditor to use Roslyn analyzers:
  1. Open the Preferences window (Edit > Preferences (macOS: Unity > Settings)).
  2. Open the Project Auditor preferences.
  3. Enable Use Roslyn Analyzers.

Resolve code reload issues

For information on the exact warning codes generated by the analyzer and the appropriate resolution for each of them, refer to Automatic reset of static state.

Additional resources