Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

AssetImportContext.DependsOnArtifact

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public void DependsOnArtifact(string path);

Declaration

public void DependsOnArtifact(GUID guid);

Declaration

public void DependsOnArtifact(ArtifactKey key);

Parameters

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.

Description

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

Declaration

public ArtifactDependencyResult DependsOnArtifact(Object artifact);

Declaration

public ArtifactDependencyResult DependsOnArtifact(LazyLoadReference<T> artifact);

Parameters

Parameter Description
artifact The Object or LazyLoadReference<T0> referencing the Asset whose artifact should be the dependency.

Returns

ArtifactDependencyResult An ArtifactDependencyResult value that reports whether the dependency was registered, or why it was not.

Description

Sets up an artifact dependency from a LazyLoadReference<T0> or a regular Object reference.

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

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<T0> 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.