Legacy Documentation: Version 2018.2 (Go to current version)
Batch mode and built-in coroutine compatibility
Support for custom Menu Item and Editor features
Other Versions

Applying defaults to assets by folder

For large projects, you might use several Presets for importing the same type of assetAny media or data that can be used in your game or Project. An asset may come from a file created outside of Unity, such as a 3D model, an audio file or an image. You can also create some asset types in Unity, such as an Animator Controller, an Audio Mixer or a Render Texture. More info
See in Glossary
. For example, for texture assets, you might have a Preset for importing Default textures and another for LightmapA pre-rendered texture that contains the effects of light sources on static objects in the scene. Lightmaps are overlaid on top of scene geometry to create the effect of lighting. More info
See in Glossary
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 Manager 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

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答