Batch mode and built-in coroutine compatibility
Support for custom Menu Item and Editor features

按文件夹将默认值应用于资源

对于大项目,可使用多个预设来导入相同类型的资源。例如,对于纹理资源,您可能具有用于导入默认纹理的预设以及用于光照贴图纹理的另一个预设。在项目的 Assets 文件夹中,已经为其中每种类型的纹理设置单独文件夹。

TexturesDefault 和 TexturesLighting 文件夹各有一个预设
TexturesDefaultTexturesLighting 文件夹各有一个预设

The following script applies a Preset based on the folder that you add an asset to. This script chooses the Preset that is in the same folder as the asset. If there is no Preset in the folder, this script searches parent folders. If there are no Presets in parent folders, Unity uses the default Preset that the Preset window specifies.

为了使用此脚本,请在 Project 窗口中新建名为 Editor 的文件夹,在此文件夹中新建 C# 脚本,然后复制并粘贴以下脚本。

using System.IO;
using UnityEditor;
using UnityEditor.Presets;

public class PresetImportPerFolder : AssetPostprocessor
{
    void OnPreprocessAsset()
    {
        // 确保我们在第一次导入资源时应用预设。
        if (assetImporter.importSettingsMissing)
        {
            // 获取当前导入的资源文件夹。
            var path = Path.GetDirectoryName(assetPath);
            while (!string.IsNullOrEmpty(path))
            {
                // 查找此文件夹中的所有预设资源。
                var presetGuids = AssetDatabase.FindAssets("t:Preset", new[] { path });
                foreach (var presetGuid in presetGuids)
                {
                    // 确保不是在子文件夹中测试预设。
                    string presetPath = AssetDatabase.GUIDToAssetPath(presetGuid);
                    if (Path.GetDirectoryName(presetPath) == path)
                    {
                        //加载预设,然后尝试将其应用于导入器。
                        var preset = AssetDatabase.LoadAssetAtPath<Preset>(presetPath);
                        if (preset.ApplyTo(assetImporter))
                            return;
                    }
                }

                //在父文件夹中重试。
                path = Path.GetDirectoryName(path);
            }
        }
    }
}

2017–03–27 Page published with limited editorial review 2018.1 中的新功能 NewIn20181

Batch mode and built-in coroutine compatibility
Support for custom Menu Item and Editor features