对于大项目,可使用多个预设来导入相同类型的资源。例如,对于纹理资源,您可能具有用于导入默认纹理的预设以及用于光照贴图纹理的另一个预设。在项目的 Assets 文件夹中,已经为其中每种类型的纹理设置单独文件夹。
以下脚本根据添加资源的文件夹而应用预设。此脚本选择与资源位于同一文件夹中的预设。如果此文件夹中没有预设,则该脚本将搜索父文件夹。如果父文件夹中没有预设,Unity 将使用 Preset 窗口指定的默认预设。
为了使用此脚本,请在 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 页面已发布 2018.1 中的新功能 NewIn20181
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.