Version: 2022.3
LanguageEnglish
  • C#

AssetImportContext.DependsOnSourceAsset

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public void DependsOnSourceAsset(string path);

Declaration

public void DependsOnSourceAsset(GUID guid);

Parameters

path The path of the source dependency.
guid The guid of the source asset dependency.

Description

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).

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 DependsOnArtifact.

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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961