# DependsOnArtifact

> Sets up an artifact dependency to an asset.

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor.AssetImporters](/engine/6000.7/script-reference/unityeditor/assetimporters.md)
* **Assembly:** UnityEditor.CoreModule

## DependsOnArtifact(ArtifactKey)

Sets up an artifact dependency to an asset.

```csharp
public void DependsOnArtifact(ArtifactKey key)
```

### Parameters

**** (\[ArtifactKey]\(/engine/6000.7/script-reference/unityeditor/experimental/artifactkey)): The artifact key of the artifact dependency.

### Remarks

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.

### Examples

```csharp
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);
        }
    }
}
```

## DependsOnArtifact(GUID)

Sets up an artifact dependency to an asset.

```csharp
public void DependsOnArtifact(GUID guid)
```

### Parameters

**** (GUID): The guid of the artifact dependency.

### Remarks

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.

### Examples

```csharp
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);
        }
    }
}
```

## DependsOnArtifact(string)

Sets up an artifact dependency to an asset.

```csharp
public void DependsOnArtifact(string path)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): 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.

### Remarks

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.

### Examples

```csharp
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);
        }
    }
}
```

## DependsOnArtifact(Object)

Sets up an artifact dependency from a [LazyLoadReference\<T>](/engine/6000.7/script-reference/unityengine/lazyloadreference1.md) or a regular `Object` reference.

```csharp
public ArtifactDependencyResult DependsOnArtifact(Object artifact)
```

### Parameters

**** (\[Object]\(/engine/6000.7/script-reference/unityengine/object)): The `Object` or [LazyLoadReference\<T>](/engine/6000.7/script-reference/unityengine/lazyloadreference1.md) referencing the Asset whose artifact should be the dependency.

### Returns

| Type                                                                                                               | Description                                                                                                                                                                                        |
| ------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ArtifactDependencyResult](/engine/6000.7/script-reference/unityeditor/assetimporters/artifactdependencyresult.md) | An [ArtifactDependencyResult](/engine/6000.7/script-reference/unityeditor/assetimporters/artifactdependencyresult.md) value that reports whether the dependency was registered, or why it was not. |

### Remarks

The API is intended to fail gracefully (set up a dependency when possible, never throw).

```csharp
using UnityEditor;
using UnityEditor.AssetImporters;
using UnityEngine;

[ScriptedImporter(1, "myimporter")]
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..
        }
    }
}
```

[LazyLoadReference\<T>](/engine/6000.7/script-reference/unityengine/lazyloadreference1.md) is preferred 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.

## DependsOnArtifact\<T>(LazyLoadReference\<T>)

Sets up an artifact dependency from a [LazyLoadReference\<T>](/engine/6000.7/script-reference/unityengine/lazyloadreference1.md) or a regular `Object` reference.

```csharp
public ArtifactDependencyResult DependsOnArtifact<T>(LazyLoadReference<T> artifact) where T : Object
```

### Parameters

**** (\[LazyLoadReference\<T>]\(/engine/6000.7/script-reference/unityengine/lazyloadreference1)): The `Object` or [LazyLoadReference\<T>](/engine/6000.7/script-reference/unityengine/lazyloadreference1.md) referencing the Asset whose artifact should be the dependency.

### Returns

| Type                                                                                                               | Description                                                                                                                                                                                        |
| ------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ArtifactDependencyResult](/engine/6000.7/script-reference/unityeditor/assetimporters/artifactdependencyresult.md) | An [ArtifactDependencyResult](/engine/6000.7/script-reference/unityeditor/assetimporters/artifactdependencyresult.md) value that reports whether the dependency was registered, or why it was not. |

### Remarks

The API is intended to fail gracefully (set up a dependency when possible, never throw).

```csharp
using UnityEditor;
using UnityEditor.AssetImporters;
using UnityEngine;

[ScriptedImporter(1, "myimporter")]
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..
        }
    }
}
```

[LazyLoadReference\<T>](/engine/6000.7/script-reference/unityengine/lazyloadreference1.md) is preferred 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.
