| Parameter | Description |
|---|---|
| path | The path of the Asset whose artifact should be the dependency. Note: Although the dependency is the artifact (import result) which is stored in the library folder, this parameter is the path to the Asset in the Assets folder, and not a path to the artifact in the Library folder. |
| guid | The guid of the artifact dependency. |
| key | The artifact key of the artifact dependency. |
| artifact | The Object or LazyLoadReference<T0> referencing the Asset whose artifact should be the dependency. |
Sets up an artifact dependency to an asset.
An artifact dependency is a dependency where an Asset depends on the import result (known as an artifact) of another Asset. If you change an Asset that has been marked as a dependency, all Assets which depend on it will also get reimported (after the dependency has been imported).
If you specify an asset as a dependency that doesn't exist or hasn't yet been imported, the dependency is registered as an un-imported asset. When the asset is later imported, Unity reimports all assets which depend on it.
When you set up an artifact dependency, use GUIDs rather than paths where possible because the GUID remains consistent even when the asset is moved or renamed.
using UnityEngine; using UnityEditor; using UnityEditor.AssetImporters;
class TextureInfo : ScriptableObject { public int height; }
[ScriptedImporter(1, "cube")] public class CubeWithTextureImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { var assetDependency = "Assets/MyTexture.png";
ctx.DependsOnArtifact(assetDependency);
var texture = AssetDatabase.LoadAssetAtPath<Texture>(assetDependency);
if (texture != null) { var textureInfo = ScriptableObject.CreateInstance<TextureInfo>(); textureInfo.height = texture.height; ctx.AddObjectToAsset("TextureInfo", textureInfo); } } }
You can also declare an artifact dependency directly from a LazyLoadReference<T0> or a regular Object reference by passing it to DependsOnArtifact.
LazyLoadReference<T0> is preferred for importer settings because it doesn't load the referenced asset when the importer is deserialized. Regular Object references can also be used, but they trigger loading of the referenced asset immediately upon deserialization.
An unassigned reference (a null Object or an unset LazyLoadReference<T0>) is ignored. A non-null Object that doesn't refer to a persisted Asset is also ignored.
using UnityEditor; using UnityEditor.AssetImporters; using UnityEngine;
public class MyImporter : ScriptedImporter { public LazyLoadReference<AnimationClip> sourceClip; public LazyLoadReference<GameObject> sourcePrefab; public Texture2D sourceTexture;
public override void OnImportAsset(AssetImportContext ctx) { // Set up dependencies up front, before any conditional early-return, so they are always registered. ctx.DependsOnArtifact(sourceClip); ctx.DependsOnArtifact(sourcePrefab); ctx.DependsOnArtifact(sourceTexture);
if (sourceClip.asset != null && sourcePrefab.asset != null && sourceTexture != null) { // Do import stuff.. } } }