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 Assets** and 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 dependency 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 Shader might reference another Shader, and a Prefab 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 compression 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.
The importers are split into two groups: Native Importers and Scripted Importers.
Native importers are built in to Unity, and provide the import functionality for most of Unity’s basic Asset types such as 3D models, Textures and audio files.
Importer | File Formats |
---|---|
AssemblyDefinitionImporter | asmdef |
AssemblyDefinitionReferenceImporter | asmref |
AudioImporter | ogg, aif, aiff, flac, wav, mp3, mod, it, s3m, xm |
ComputeShaderImporter | compute |
DefaultImporter | rsp, unity |
FBXImporter | fbx, mb, ma, max, jas, dae, dxf, obj, c4d, blend, lxo |
IHVImageFormatImporter | astc, dds, ktx, pvr |
LocalizationImporter | po |
Mesh3DSImporter | 3ds |
NativeFormatImporter | anim, animset, asset, blendtree, buildreport, colors, controller, cubemap, curves, curvesNormalized, flare, fontsettings, giparams, gradients, guiskin, ht, mask, mat, mesh, mixer, overrideController, particleCurves, particleCurvesSigned, particleDoubleCurves, particleDoubleCurvesSigned, physicMaterial, physicsMaterial2D, playable, preset, renderTexture, shadervariants, spriteatlas, state, statemachine, texture2D, transition, webCamTexture, brush, terrainlayer, signal |
PackageManifestImporter | json |
PluginImporter | dll, winmd, so, jar, java, kt, aar, suprx, prx, rpl, cpp, cc, c, h, jslib, jspre, bc, a, m, mm, swift, xib, bundle, dylib, config |
PrefabImporter | prefab |
RayTracingShaderImporter | raytrace |
ShaderImporter | cginc, cg, glslinc, hlsl, shader |
SketchUpImporter | skp |
SpeedTreeImporter | spm, st |
SubstanceImporter | .sbsar |
TextScriptImporter | txt, html, htm, xml, json, csv, yaml, bytes, fnt, manifest, md, js, boo, rsp |
TextureImporter | jpg, jpeg, tif, tiff, tga, gif, png, psd, bmp, iff, pict, pic, pct, exr, hdr |
TrueTypeFontImporter | ttf, dfont, otf, ttc |
VideoClipImporter | avi, asf, wmv, mov, dv, mp4, m4v, mpg, mpeg, ogv, vp8, webm |
VisualEffectImporter | vfx, vfxoperator, vfxblock |
You can define your own importers to add import functionality for new file types, or to override the importer for an existing file type. These importers are called Scripted Importers.
Note: In addition to your custom importers, some of Unity’s own importers also count as Scripted Importers, and Unity processes them in this stage instead of the Native Importer stage.
The scripted importers Unity ships with are:
When an importer imports an asset file, an AssetImportContext is generated.
The AssetImportContext is used to report the Static Dependencies of an asset.
Also, during the import step, there are a number of callbacks which occur.
Preprocess Asset Importer Calls:
OnPreprocessAsset
OnPreprocessAnimation
OnPreprocessAudio
OnPreprocessModel
OnPreprocessSpeedTree
OnPreprocessTexture
Postprocess Asset Importer Calls:
OnAssignMaterialModel
OnPostprocessAnimation
OnPostprocessAssetbundleNameChanged
OnPostprocessAudio
OnPostprocessCubemap
OnPostprocessGameObjectWithAnimatedUserProperties
OnPostprocessGameObjectWithUserProperties
OnPostprocessMaterial
OnPostprocessMeshHierarchy
OnPostprocessModel
OnPostprocessSpeedTree
OnPostprocessSprites
OnPostprocessTexture
One final post processing callback which is triggered once all importing has completed is OnPostprocessAllAssets
.
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 Control 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 AssetDatabase.ForceReserializeAssets
or AssetImport.SaveAndReimport
. Note, you must be careful not to cause infinite reimport loops if you do this.
If an Assembly Reload happens after compiling scripts. 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 currently loaded script data. It first stores all serializable variables in all loaded scripts and, and after it loads the scripts, it restores them. All of the data that was not serializable is lost after a hot reload.
Note: Default assets are imported before script assets, so any script-defined PostProcessAllAssets callbacks are not called for default assets.
Once all these steps have completed, the Refresh() is complete and the Artifact Database is updated with the relevant information, as well as having the necessary import result files generated on disk.