46 lines
1.7 KiB
C#
46 lines
1.7 KiB
C#
using UnityEditor;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
|
|
public class FBXMaterialRemapper : Editor
|
|
{
|
|
[MenuItem("Assets/材质工具/Remap FBX 材质到已有材质", true)]
|
|
static bool ValidateSelection()
|
|
{
|
|
return Selection.assetGUIDs.Any(guid =>
|
|
{
|
|
string path = AssetDatabase.GUIDToAssetPath(guid);
|
|
return path.EndsWith(".fbx", System.StringComparison.OrdinalIgnoreCase);
|
|
});
|
|
}
|
|
|
|
[MenuItem("Assets/材质工具/Remap FBX 材质到已有材质")]
|
|
static void RemapSelectedFBXMaterials()
|
|
{
|
|
foreach (var guid in Selection.assetGUIDs)
|
|
{
|
|
string path = AssetDatabase.GUIDToAssetPath(guid);
|
|
if (!path.EndsWith(".fbx", System.StringComparison.OrdinalIgnoreCase))
|
|
continue;
|
|
|
|
var importer = AssetImporter.GetAtPath(path) as ModelImporter;
|
|
if (importer == null) continue;
|
|
|
|
// ✅ 正确设置材质导入方式
|
|
importer.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
|
|
importer.materialLocation = ModelImporterMaterialLocation.InPrefab;
|
|
importer.materialName = ModelImporterMaterialName.BasedOnMaterialName;
|
|
importer.materialSearch = ModelImporterMaterialSearch.Everywhere;
|
|
|
|
importer.SearchAndRemapMaterials(ModelImporterMaterialName.BasedOnMaterialName,
|
|
ModelImporterMaterialSearch.Everywhere);
|
|
|
|
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
|
|
|
|
Debug.Log($"✅ 已重新设置 FBX 材质导入方式: {path}");
|
|
}
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
} |