
对于大型项目可能需要统一设置同一种类型资源的导入方式而不需要每导入一个资源就手动设置一遍。例如Texture 默认的材质是一种设置Lightmap 材质则是另外一种设置。TexturesDefault和TexturesLighting文件夹各有一个预设Preset配置。下面的脚本会根据您导入 Asset 时所在的文件夹自动应用对应的 Preset。如果当前文件夹中没有预设脚本会继续向上搜索父文件夹如果父文件夹中也没有预设则使用 Unity 预设窗口指定的默认预设。using System.IO; using UnityEditor; using UnityEditor.Presets; public class PresetImportPerFolder : AssetPostprocessor { void OnPreprocessAsset() { // Make sure we are applying presets the first time an asset is imported. // 当该资源导入且没有 meta 文件时 if (assetImporter.importSettingsMissing) { // Get the current imported asset folder. // 获取资源导入所在的文件夹路径 var path Path.GetDirectoryName(assetPath); if (!string.IsNullOrEmpty(path)) { // Find all Preset assets in this folder. // 查找该路径下所有的 Preset并返回它们的 GUID var presetGuids AssetDatabase.FindAssets(t:Preset, new[] { path }); foreach (var presetGuid in presetGuids) { // Make sure we are not testing Presets in a subfolder. // 获取 Preset 的路径 string presetPath AssetDatabase.GUIDToAssetPath(presetGuid); // 如果 Preset 所在的文件夹与资源导入的文件夹相同即二者位于同一文件夹下 if (Path.GetDirectoryName(presetPath) path) { // Load the Preset and try to apply it to the importer. // 加载该 Preset var preset AssetDatabase.LoadAssetAtPathPreset(presetPath); // 将预设应用到导入的资源上 if (preset.ApplyTo(assetImporter)) return; } } } } } }AssetDatabasehttps://docs.unity3d.com/ScriptReference/AssetDatabase.htmlhttps://docs.unity3d.com/ScriptReference/AssetDatabase.html通过上述脚本您可以在资源导入时自动匹配对应文件夹下的 Preset从而避免重复手动配置显著提升大型项目的资源导入效率。如需进一步了解 AssetDatabase 的更多用法可查阅 Unity 官方文档。