GetArtifactData
Returns the data of the artifact file that was created by another importer, and adds a dependency to that file and the source asset path.
Read time 4 minutesLast updated 11 days ago
Definition
- Type: Method
- Namespace: UnityEditor.AssetImporters
- Assembly: UnityEditor.CoreModule
GetArtifactData(string, string)
Returns the data of the artifact file that was created by another importer, and adds a dependency to that file and the source asset path.
public byte[] GetArtifactData(string path, string filename)
Parameters
Returns
Type | Description |
|---|---|
| byte[] | A byte array containing the content of the requested artifact file, or an empty array if the file could not be found. |
Remarks
In addition to creating a dependency on the import artifact, this overload of the method also adds a dependency to the asset's path. This means if you rename or move the asset that created the artifact file in the project, Unity reimports the asset, even if the artifact file is unchanged. This can be useful if you want to implement a functional naming convention as part of your asset import processing. If you don't want this behavior, use the GUID or ArtifactKey overload to avoid depending on the asset path.
GetArtifactData(GUID, string)
Returns the data of the artifact file that was created by another importer, and adds a dependency to that file.
public byte[] GetArtifactData(GUID guid, string filename)
Parameters
Returns
Type | Description |
|---|---|
| byte[] | A byte array containing the content of the requested artifact file, or an empty array if the file could not be found. |
Remarks
This method must be used in conjunction with AssetImportContext.SetOutputArtifactData or AssetImportContext.SetOutputArtifactFile. Once an artifact file has been created by another importer, using this method returns the artifact file's content as a byte array and adds a dependency to it. It is then possible to use this data directly to generate the import result. The following example shows a ScriptedImporter generating a Material and setting its base color using the first pixel color saved in an artifact file from the example inside AssetImportContext.SetOutputArtifactData. Note: While there is no limit on how many artifact files are created using AssetImportContext.SetOutputArtifactData, it is only possible to depend upon the first 32 artifact files created by a single asset import.
Examples
using System.Text;using UnityEditor;using UnityEditor.AssetImporters;using UnityEngine;[ScriptedImporter(1, "pixelMat", 1, AllowCaching = true)]public class MaterialFromFirstPixel : ScriptedImporter{ public LazyLoadReference<Texture2D> m_Texture; public override void OnImportAsset(AssetImportContext importContext) { var color = Color.magenta; if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(m_Texture, out var stringGuid, out var id)) { byte[] data; if (GUID.TryParse(stringGuid, out var guid)) data = importContext.GetArtifactData(guid, "firstpixelcolor"); else data = importContext.GetArtifactData(AssetDatabase.GUIDToAssetPath(stringGuid), "firstpixelcolor"); if (data.Length > 0) { var colorString = Encoding.UTF8.GetString(data); ColorUtility.TryParseHtmlString(colorString, out color); } } var mat = new Material(Shader.Find("Standard")); mat.color = color; importContext.AddObjectToAsset("main", mat); importContext.SetMainObject(mat); }}
GetArtifactData(ArtifactKey, string)
Returns the data of the artifact file that was created by another importer, and adds a dependency to that file.
public byte[] GetArtifactData(ArtifactKey key, string fileName)
Parameters
The artifact key of the artifact file dependency.
The name of the artifact file to depend upon. See AssetImportContext.SetOutputArtifactData.
Returns
Type | Description |
|---|---|
| byte[] | A byte array containing the content of the requested artifact file, or an empty array if the file could not be found. |
Remarks
This method must be used in conjunction with AssetImportContext.SetOutputArtifactData or AssetImportContext.SetOutputArtifactFile. Once an artifact file has been created by another importer, using this method returns the artifact file's content as a byte array and adds a dependency to it. It is then possible to use this data directly to generate the import result. Use this overload when you need more precise control over the artifact key than a GUID provides. Note: While there is no limit on how many artifact files are created using AssetImportContext.SetOutputArtifactData, it is only possible to depend upon the first 32 artifact files created by a single asset import.