DependsOnSourceAsset
Allows you to specify that an Asset depends directly on the source file of another Asset (as opposed to the import result of another asset).
Read time 3 minutesLast updated 25 days ago
Definition
- Type: Method
- Namespace: UnityEditor.AssetImporters
- Assembly: UnityEditor.CoreModule
DependsOnSourceAsset(string)
Allows you to specify that an Asset depends directly on the source file of another Asset (as opposed to the import result of another asset).
public void DependsOnSourceAsset(string path)
Parameters
The path of the source dependency.
Remarks
When you specify that one asset depends on another (eg, Asset A depends on Assset B), it means if that Asset B is modified, not only will Asset B be reimported, but also Asset A will be reimported.
Note: This methods sets up a dependency on the Asset source file itself, not the import result (the artifact) of the Asset. If you want to set up a dependency on the import result of an asset, use AssetImportContext.DependsOnArtifact.
Note: 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 that's 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. Use the GUID overload when you already hold a GUID.
Examples
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)
Allows you to specify that an Asset depends directly on the source file of another Asset (as opposed to the import result of another asset).
public void DependsOnSourceAsset(GUID guid)
Parameters
Remarks
When you specify that one asset depends on another (eg, Asset A depends on Assset B), it means if that Asset B is modified, not only will Asset B be reimported, but also Asset A will be reimported.
Note: This methods sets up a dependency on the Asset source file itself, not the import result (the artifact) of the Asset. If you want to set up a dependency on the import result of an asset, use AssetImportContext.DependsOnArtifact.
Note: 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 that's 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. Use the GUID overload when you already hold a GUID.
Examples
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); }}