Version: Unity 6.6 Alpha (6000.6)
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

Setup 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 declare artifact dependencies using either LazyLoadReference<T0> or regular Object references. Use AssetDatabase.TryGetGUIDAndLocalFileIdentifier to obtain the GUID from either type of reference.

LazyLoadReference<T0> is preferred for importer settings because it does not load the referenced asset when the importer is deserialized. Regular Object references can also be used, but they trigger loading the referenced asset immediately upon deserialization.

In both cases, the referenced asset may not yet have been imported, so always validate references.

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) { // Declare artifact dependencies using LazyLoadReference if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(sourceClip.entityId, out string clipGuid, out long _)) { if (GUID.TryParse(clipGuid, out GUID clipArtifactGuid)) { ctx.DependsOnArtifact(clipArtifactGuid); } }

if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(sourcePrefab.entityId, out string prefabGuid, out long _)) { if (GUID.TryParse(prefabGuid, out GUID prefabArtifactGuid)) { ctx.DependsOnArtifact(prefabArtifactGuid); } }

// Declare artifact dependency using a regular Object reference if (!ReferenceEquals(sourceTexture, null)) { if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(sourceTexture, out string textureGuid, out long _)) { if (GUID.TryParse(textureGuid, out GUID textureArtifactGuid)) { ctx.DependsOnArtifact(textureArtifactGuid); } } }

if (sourceClip.asset != null && sourcePrefab.asset != null && sourceTexture != null) { // Do import stuff.. } } }