DependsOnArtifact
Sets up a dependency on the import result that the specified artifact key identifies.
Read time 4 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEditor.AssetImporters
- Assembly: UnityEditor.CoreModule
DependsOnArtifact(ArtifactKey)
Sets up a dependency on the import result that the specified artifact key identifies.
public void DependsOnArtifact(ArtifactKey key)
Parameters
The artifact key that identifies the import result this asset depends on.
Remarks
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 isn't valid.
keyDependsOnArtifact(GUID)
Sets up a dependency on the import result of the asset with the specified GUID.
public void DependsOnArtifact(GUID guid)
Parameters
Remarks
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 is empty.
guidDependsOnArtifact(string)
Sets up a dependency on the import result of the asset at a path.
public void DependsOnArtifact(string path)
Parameters
The project-relative path of the asset whose import result this asset depends on. This is the path to the source asset in the folder, not to the artifact in the folder.
AssetsLibraryRemarks
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 is null or empty.
pathExamples
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 a dependency on the import result of the asset that an object reference points to.
public ArtifactDependencyResult DependsOnArtifact(Object artifact)
Parameters
A reference to the asset whose import result this asset depends on.
Returns
Type | Description |
|---|---|
| ArtifactDependencyResult | An ArtifactDependencyResult value that shows whether the method registered the dependency, or why it didn't. |
Remarks
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 reference loads the referenced asset as soon as Unity deserializes the importer. To avoid that, use the overload that takes a LazyLoadReference<T>.
ObjectDependsOnArtifact<T>(LazyLoadReference<T>)
Sets up a dependency on the import result of the asset that a lazy-load reference points to.
public ArtifactDependencyResult DependsOnArtifact<T>(LazyLoadReference<T> artifact) where T : Object
Parameters
A lazy-load reference to the asset whose import result this asset depends on.
Returns
Type | Description |
|---|---|
| ArtifactDependencyResult | An ArtifactDependencyResult value that shows whether the method registered the dependency, or why it didn't. |
Remarks
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<T> doesn't load the referenced asset when Unity deserializes the importer.
Examples
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.. } }}