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.
CloseFor 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.
Closepath | The path of the source dependency. |
Creates dependency between the asset and a source asset.
The asset depends on the source asset. If the source asset changes or the path of the source asset changes, then the asset is reimported.
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); } }