Version: Unity 7.0 Alpha (7000.0)
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);

Parameters

Parameter Description
path The project-relative path of the asset whose import result this asset depends on. This is the path to the source asset in the Assets folder, not to the artifact in the Library folder.

Description

Sets up a dependency on the import result of the asset at a path.

An artifact dependency means that this asset depends on the import result, known as an artifact, of another asset. If the other asset changes, Unity reimports it and then reimports every asset that depends on it. If the other asset doesn't exist or isn't imported yet, Unity registers the dependency anyway, and reimports this asset when the other asset is imported. Where possible, use the overload that takes a GUID, because the GUID stays the same when you move or rename the asset. The method throws an exception if path is null or empty.

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 void DependsOnArtifact(GUID guid);

Parameters

Parameter Description
guid The GUID of the asset whose import result this asset depends on.

Description

Sets up a dependency on the import result of the asset with the specified GUID.

An artifact dependency means that this asset depends on the import result, known as an artifact, of another asset. If the other asset changes, Unity reimports it and then reimports every asset that depends on it. If the other asset doesn't exist or isn't imported yet, Unity registers the dependency anyway, and reimports this asset when the other asset is imported. The GUID stays the same when you move or rename the asset, so this overload is more reliable than the one that takes a path. The method throws an exception if guid is empty.


Declaration

public void DependsOnArtifact(ArtifactKey key);

Parameters

Parameter Description
key The artifact key that identifies the import result this asset depends on.

Description

Sets up a dependency on the import result that the specified artifact key identifies.

An artifact dependency means that this asset depends on the import result, known as an artifact, of another asset. If the other asset changes, Unity reimports it and then reimports every asset that depends on it. If the other asset doesn't exist or isn't imported yet, Unity registers the dependency anyway, and reimports this asset when the other asset is imported. The method throws an exception if key isn't valid.


Declaration

public ArtifactDependencyResult DependsOnArtifact(Object artifact);

Parameters

Parameter Description
artifact A reference to the asset whose import result this asset depends on.

Returns

ArtifactDependencyResult An ArtifactDependencyResult value that shows whether the method registered the dependency, or why it didn't.

Description

Sets up a dependency on the import result of the asset that an object reference points to.

Unlike the overloads that take a path or a GUID, this method doesn't throw. It sets up the dependency when it can, and reports the result. Using an Object reference loads the referenced asset as soon as Unity deserializes the importer. To avoid that, use the overload that takes a LazyLoadReference<T0>.


Declaration

public ArtifactDependencyResult DependsOnArtifact(LazyLoadReference<T> artifact);

Parameters

Parameter Description
artifact A lazy-load reference to the asset whose import result this asset depends on.

Returns

ArtifactDependencyResult An ArtifactDependencyResult value that shows whether the method registered the dependency, or why it didn't.

Description

Sets up a dependency on the import result of the asset that a lazy-load reference points to.

Unlike the overloads that take a path or a GUID, this method doesn't throw. It sets up the dependency when it can, and reports the result. This overload is preferred, because a LazyLoadReference<T0> doesn't load the referenced asset when Unity deserializes the importer.

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