Version: 2022.2
LanguageEnglish
  • C#

AssetImportContext.DependsOnCustomDependency

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 DependsOnCustomDependency(string dependency);

Parameters

dependency Name of dependency. You can use any name you like, but because these names are global across all your Assets, it can be useful to use a naming convention (eg a path-based naming system as in the example below) to avoid clashes with other custom dependency names.

Description

Allows you to specify that an Asset has a custom dependency.

Use custom dependency if need to setup a dependency to something that can't be expressed as either an source asset dependency or artifact dependency.

using UnityEngine;
using UnityEditor.AssetImporters;
using UnityEditor.Experimental;
using UnityEngine.Assertions;
using UnityEditor;

class MySystem { public const string CustomDependencyName = "MyProject/Settings";

public static string GetSetting() { return "42"; }

public static void RegisterCustomDependency() { // You cannot register a custom dependency during the Asset Import process, so this method must be called before the Asset is imported. AssetDatabaseExperimental.RegisterCustomDependency(CustomDependencyName, Hash128.Compute(GetSetting())); } }

class SomeValue : ScriptableObject { public int value; }

[ScriptedImporter(1, "data", AllowCaching = true)] public class MyDataImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { ctx.DependsOnCustomDependency(MySystem.CustomDependencyName);

var someObj = ScriptableObject.CreateInstance<SomeValue>();

var setting = MySystem.GetSetting(); if (setting == "42") someObj.value = 100; else someObj.value = -1;

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