DependsOnArtifact
Sets up an artifact dependency to an asset.
Read time 5 minutesLast updated 14 days ago
Definition
- Type: Method
- Namespace: UnityEditor.AssetImporters
- Assembly: UnityEditor.CoreModule
DependsOnArtifact(ArtifactKey)
Sets up an artifact dependency to an asset.
public void DependsOnArtifact(ArtifactKey key)
Parameters
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
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.
public void DependsOnArtifact(GUID guid)
Parameters
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
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.
public void DependsOnArtifact(string path)
Parameters
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
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> or a regular reference.
Objectpublic ArtifactDependencyResult DependsOnArtifact(Object artifact)
Parameters
The or LazyLoadReference<T> referencing the Asset whose artifact should be the dependency.
ObjectReturns
Type | Description |
|---|---|
| ArtifactDependencyResult | An ArtifactDependencyResult 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).
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> is preferred because it doesn't load the referenced asset when the importer is deserialized. Regular references can also be used, but they trigger loading of the referenced asset immediately upon deserialization.
ObjectDependsOnArtifact<T>(LazyLoadReference<T>)
Sets up an artifact dependency from a LazyLoadReference<T> or a regular reference.
Objectpublic ArtifactDependencyResult DependsOnArtifact<T>(LazyLoadReference<T> artifact) where T : Object
Parameters
The or LazyLoadReference<T> referencing the Asset whose artifact should be the dependency.
ObjectReturns
Type | Description |
|---|---|
| ArtifactDependencyResult | An ArtifactDependencyResult 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).
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> is preferred because it doesn't load the referenced asset when the importer is deserialized. Regular references can also be used, but they trigger loading of the referenced asset immediately upon deserialization.
Object