Unity refreshes the Asset Database in the following situations:
Some other AssetDatabase APIs trigger a Refresh() but only for the Assets you specify. For example CreateAsset() and ImportAsset().
Unity performs the following steps during an Asset Database refresh:
Unity performs the steps described in the previous section during the Asset Database refresh. This section describes this process in more detail. These steps happen inside a loop, and some steps might cause the refresh process to restart (for example, if importing an Asset creates other Assets which Unity also needs to import).
Unity restarts the Asset Database refresh loop under the following conditions:
When Unity looks for changes on disk, it scans the
Packages folders in your Project to check if any files have been added, modified, or deleted since the last scan. It gathers any changes into a list to process in the next step.
Once Unity gathers the file list, it then gets the file hashes for the files which have either been added or modified. It then updates the Asset Database with the GUIDs for those files, and removes the entries for the files that it detected as deleted.
The Asset Database keeps track of two types of Asset dependencies: static dependencies and dynamic dependencies. If any dependencyIn the context of the Package Manager, a dependency is a specific package version (expressed in the form
package_name@package_version) that a project or another package requires in order to work. Projects and packages use the dependencies attribute in their manifests to define the set of packages they require. For projects, these are considered direct dependencies; for packages, these are indirect, or transitive, dependencies. More info
See in Glossary of an Asset changes, Unity triggers a reimport of that Asset.
A static dependency is a value, setting or property that an importer depends on. Static dependencies are known before the Asset is imported, and are not affected by the behavior of the importer during the import process. If a static dependency of an Asset changes, Unity re-imports that Asset.
Some common static dependencies are:
Unity typically discovers the dynamic dependencies of an asset during the import process. This is because these dependencies are defined by the content of the source asset. For example, a ShaderA program that runs on the GPU. More info
See in Glossary might reference another Shader, and a PrefabAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary might depend on other Prefabs.
The importer might also use a global state conditionally based on the content of the source asset, in which case it also becomes a dynamic dependency. Examples of this are the target platform, the Project’s color space, the graphics API, the scripting runtime version, or the Texture compressionA method of storing data that reduces the amount of storage space it requires. See Texture Compression, Animation Compression, Audio Compression, Build Compression.
See in Glossary state.
Unity stores these dynamic dependencies of an asset in an Asset Import Context.
In the list of changed or added files, Unity gathers the ones that relate to code, and sends them to the script compilation pipeline. The compiler generates assemblies from the script files and assembly definition files in your Project. For more information on this step, see documentation on script compilation assembly definition files.
If Unity detects any script changes, it reloads the C# domain. It does this because new Scripted Importers could have been created, and their logic could potentially impact the import result of Assets in the Refresh queue. This step restarts the Refresh() to ensure any new Scripted Importers take effect.
Once Unity imports all code-related assets and it reloads the domain, it then moves on to the remaining Assets. Each Asset’s importer processes that type of Asset, and identifies the file types that it should import based on the filename extensions. For example, the TextureImporter is responsible for importing .jpg, .png and .psd files, among others.
There are two types of importers:
Unity processes all native importers first, and then all scripted importers in a separate phase.
When an importer imports an asset file, Unity generates an AssetImportContext. The AssetImportContext reports the Static Dependencies of an asset.
Also, during the import step, there are a number of callbacks which occur.
Preprocess Asset Importer Calls:
Postprocess Asset Importer Calls:
One final post processing callback which is triggered once all importing has completed is
There are a number of things that can happen which will restart the refresh process on the Asset folder, some of them being:
If the import of an asset failed
If the asset was modified during the import phase of the Refresh. For example, if a file in the list gets modified so its modification date is not what it was in the previous refresh. This can happen if you start pulling files from a Version ControlA system for managing file changes. You can use Unity in conjunction with most common version control tools, including Perforce, Git, Mercurial and PlasticSCM. More info
See in Glossary system while the Editor has focus.
If an Asset created other assets during import. For example: When importing an FBX, textures can get extracted from the FBX and placed into the project, and this means that Unity has to import the textures (and any artifacts they generate).
If you force the re-import of a file during one of the pre/post process callbacks or inside OnPostProcessAllAssets, for example, using
AssetImport.SaveAndReimport. Note, you must be careful not to cause infinite reimport loops if you do this.
If an Assembly Reload happens after compiling scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary. If you generate a C# file during the refresh process, that new file must then be compiled, so Unity restarts the refresh.
If you save an asset as “Text only” but the Asset must be serialized as binary, a restart will happen. (For example, Scenes with Terrains in them must be serialized as Binary, since the terrain data would be unwieldy if viewed as an array of characters in a text file.)
Hot reloading refers to the process where Unity imports and applies any changes to scripts and assets while the Editor is open. This might happen while the Editor is in Play Mode or outside of Play Mode. You do not have to restart your application or the Editor for changes to take effect.
When you change and save a script, Unity hot-reloads all of the project’s script data. It first stores all serializable variable values in all loaded scripts, reloads the scripts, then restores the values. All of the data stored in non-serializable variables is lost during a hot reload.
Note: Unity imports assets imported by the (built-in DefaultImporter)[BuiltInImporters] first, and then script assets, so it does not call any script-defined PostProcessAllAssets for default assets.
Once all these steps have completed, the
Refresh() is complete. The Artifact Database is updated with the relevant information, and the necessary import result files are generated on disk.