文档

Unity Engine

Unity Engine

DependsOnArtifact

Setup artifact dependency to an asset.
阅读时间3 分钟最后更新于 19 小时前

Definition

DependsOnArtifact(ArtifactKey)

Setup 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).
Note: If you specify an Asset as a dependency that doesn't exist or hasn't yet been imported, the dependency is still registered. It is registered as an un-imported Asset. When the Asset is later imported, all Assets which depend on it will also get reimported.

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)

Setup artifact dependency to an asset.
public void DependsOnArtifact(GUID guid)

Parameters

guid

GUID
The guid 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).
Note: If you specify an Asset as a dependency that doesn't exist or hasn't yet been imported, the dependency is still registered. It is registered as an un-imported Asset. When the Asset is later imported, all Assets which depend on it will also get reimported.

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)

Setup artifact dependency to an asset.
public void DependsOnArtifact(string path)

Parameters

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.

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).
Note: If you specify an Asset as a dependency that doesn't exist or hasn't yet been imported, the dependency is still registered. It is registered as an un-imported Asset. When the Asset is later imported, all Assets which depend on it will also get reimported.

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