59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
public class ExtractMaterials : EditorWindow
|
|
{
|
|
[MenuItem("¹¤¾ß/ÌáÈ¡²ÄÁÏ")]
|
|
static void Init()
|
|
{
|
|
ExtractMaterials window = (ExtractMaterials)EditorWindow.GetWindow(typeof(ExtractMaterials));
|
|
window.Show();
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
GUILayout.Label("Extract Materials", EditorStyles.boldLabel);
|
|
|
|
if (GUILayout.Button("Extract"))
|
|
{
|
|
string path = EditorUtility.OpenFolderPanel("Select FBX Folder", "", "");
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
ExtractMaterialsFromFolder(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void ExtractMaterialsFromFolder(string folderPath)
|
|
{
|
|
string[] filePaths = System.IO.Directory.GetFiles(folderPath, "*.fbx", System.IO.SearchOption.AllDirectories);
|
|
|
|
foreach (string filePath in filePaths)
|
|
{
|
|
Object obj = AssetDatabase.LoadAssetAtPath(filePath, typeof(GameObject));
|
|
GameObject go = obj as GameObject;
|
|
|
|
if (go != null)
|
|
{
|
|
Renderer[] renderers = go.GetComponentsInChildren<Renderer>(true);
|
|
|
|
foreach (Renderer renderer in renderers)
|
|
{
|
|
Material[] materials = renderer.sharedMaterials;
|
|
|
|
for (int i = 0; i < materials.Length; i++)
|
|
{
|
|
string materialName = string.Format("{0}_{1}.mat", go.name, i);
|
|
string materialPath = System.IO.Path.Combine(folderPath, materialName);
|
|
|
|
AssetDatabase.CreateAsset(materials[i], materialPath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
}
|