Experimental: this API is experimental and might be changed or removed in the future.

AssetImportContext.DependsOnSourceAsset

切换到手册
public void DependsOnSourceAsset (string path);

参数

path源依赖关系的路径。

描述

创建资源和源资源之间的依赖关系。

资源取决于源资源。如果源资源发生变化或源资源的路径发生变化,则会重新导入资源。

using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.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