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

Applying defaults to assets by folder

For large projects, you might use several Presets for importing the same type of asset. For example, for texture assets, you might have a Preset for importing Default textures and another for Lightmap textures. In the Assets folder of your project, you have separate folders for each of these types of textures.

The TexturesDefault and TexturesLighting folders each have a Preset
The TexturesDefault and TexturesLighting folders each have a Preset

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.

To use this script, create a new folder named Editor in the Project window, create a new C# Script in this folder, then copy and paste this script.

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.
        if (assetImporter.importSettingsMissing)
        {
            // Get the current imported asset folder.
            var path = Path.GetDirectoryName(assetPath);
            while (!string.IsNullOrEmpty(path))
            {
                // Find all Preset assets in this folder.
                var presetGuids = AssetDatabase.FindAssets("t:Preset", new[] { path });
                foreach (var presetGuid in presetGuids)
                {
                    // Make sure we are not testing Presets in a subfolder.
                    string presetPath = AssetDatabase.GUIDToAssetPath(presetGuid);
                    if (Path.GetDirectoryName(presetPath) == path)
                    {
                        // Load the Preset and try to apply it to the importer.
                        var preset = AssetDatabase.LoadAssetAtPath<Preset>(presetPath);
                        if (preset.ApplyTo(assetImporter))
                            return;
                    }
                }

                // Try again in the parent folder.
                path = Path.GetDirectoryName(path);
            }
        }
    }
}

2017–03–27 Page published with limited editorial review New feature in 2018.1 NewIn20181

Batch mode and built-in coroutine compatibility
Support for custom Menu Item and Editor features
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961