DependsOnSourceAsset
Sets up a dependency on the source file of the asset at a path.
Read time 2 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEditor.AssetImporters
- Assembly: UnityEditor.CoreModule
DependsOnSourceAsset(string)
Sets up a dependency on the source file of the asset at a path.
public void DependsOnSourceAsset(string path)
Parameters
The project-relative path of the source file that this asset depends on.
Remarks
A source dependency means that if the other asset's source file changes, Unity reimports this asset too. This is a dependency on the source file itself, not on the import result. To depend on the import result, use AssetImportContext.DependsOnArtifact. The path doesn't have to point to an existing asset. Unity records the location either way, so the importer runs again if an asset is later created at that path, or if the asset there is deleted. This lets an importer produce one result while the file is missing and a different result once it exists. Unity always interprets the string as a project-relative path, so a string in any other form registers a dependency on a location that never resolves. If you already have a GUID, use the overload that takes a GUID. The method throws an exception if is null or empty.
pathExamples
using UnityEngine;using UnityEditor;using UnityEditor.AssetImporters;using System.IO;[ScriptedImporter(1, "cube")]public class CubeWithTextureImporter : ScriptedImporter{ public override void OnImportAsset(AssetImportContext ctx) { var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); ctx.AddObjectToAsset("main obj", cube); ctx.SetMainObject(cube); var material = new Material(Shader.Find("Standard")); var lines = File.ReadAllLines(ctx.assetPath); var texturePath = lines[0]; var texture = AssetDatabase.LoadAssetAtPath<Texture>(texturePath); if (texture != null) { material.SetTexture("_MainTex", texture); // add a dependency on the texture, such that if it changes or moves, we reimport the asset ctx.DependsOnSourceAsset(texturePath); } ctx.AddObjectToAsset("MaterialWithTexture", material); }}
DependsOnSourceAsset(GUID)
Sets up a dependency on the source file of the asset with the specified GUID.
public void DependsOnSourceAsset(GUID guid)
Parameters
Remarks
A source dependency means that if the other asset's source file changes, Unity reimports this asset too. This is a dependency on the source file itself, not on the import result. To depend on the import result, use AssetImportContext.DependsOnArtifact. The method throws an exception if is empty.
guid