-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFbxMeshExtractor.cs
More file actions
107 lines (90 loc) · 3.73 KB
/
FbxMeshExtractor.cs
File metadata and controls
107 lines (90 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using UnityEngine;
using UnityEditor;
using System.IO;
namespace BYUtils.EditorTools
{
public class FbxMeshExtractor : EditorWindow
{
private Object targetFbx;
private string outputFolder = "Assets/ExtractedMeshes";
[MenuItem("Tools/BY Utils/FBX Mesh Extractor")]
static void Init()
{
FbxMeshExtractor window = (FbxMeshExtractor)EditorWindow.GetWindow(typeof(FbxMeshExtractor));
window.titleContent = new GUIContent("FBX Mesh Extractor");
window.Show();
}
void OnGUI()
{
GUILayout.Label("Extract All Meshes From FBX", EditorStyles.boldLabel);
targetFbx = EditorGUILayout.ObjectField("Target FBX", targetFbx, typeof(GameObject), false);
outputFolder = EditorGUILayout.TextField("Output Folder", outputFolder);
if (GUILayout.Button("Browse Output Folder"))
{
string path = EditorUtility.SaveFolderPanel("Choose Output Folder", outputFolder, "");
if (!string.IsNullOrEmpty(path))
{
// Convert to project relative path
if (path.StartsWith(Application.dataPath))
{
outputFolder = "Assets" + path.Substring(Application.dataPath.Length);
}
else
{
Debug.LogWarning("Please select a folder within your project's Assets folder.");
}
}
}
EditorGUILayout.Space();
GUI.enabled = targetFbx != null;
if (GUILayout.Button("Extract All Meshes"))
{
ExtractMeshes();
}
GUI.enabled = true;
}
void ExtractMeshes()
{
if (targetFbx == null) return;
// Ensure output folder exists
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}
GameObject fbxObject = targetFbx as GameObject;
if (fbxObject == null)
{
EditorUtility.DisplayDialog("Error", "Selected object is not a valid GameObject/FBX file.", "OK");
return;
}
string fbxPath = AssetDatabase.GetAssetPath(fbxObject);
if (string.IsNullOrEmpty(fbxPath))
{
EditorUtility.DisplayDialog("Error", "Could not determine asset path.", "OK");
return;
}
// Get all meshes
Object[] assets = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
int extractedCount = 0;
foreach (Object asset in assets)
{
if (asset is Mesh)
{
Mesh mesh = asset as Mesh;
string meshName = mesh.name;
// Create new mesh asset
Mesh meshCopy = Object.Instantiate(mesh);
meshCopy.name = meshName;
// Save to target folder
string meshPath = Path.Combine(outputFolder, meshName + ".asset");
AssetDatabase.CreateAsset(meshCopy, meshPath);
extractedCount++;
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Extraction Complete",
$"Successfully extracted {extractedCount} meshes to {outputFolder}.", "OK");
}
}
}