-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBXMeshExtractor.cs
More file actions
138 lines (108 loc) · 4.07 KB
/
Copy pathFBXMeshExtractor.cs
File metadata and controls
138 lines (108 loc) · 4.07 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class FBXMeshExtractor : EditorWindow
{
private static string _progressTitle = "Extracting Meshes";
private static string _sourceExtension_1 = ".fbx";
private static string _sourceExtension_2 = ".FBX";
private static string _targetExtension = ".asset";
public delegate bool ExtractElement(FBXMeshExtractor extractor, Object obj);
[MenuItem("Assets/Extract From FBX", validate = true)]
private static bool ExtractMeshesMenuItemValidate()
{
if (GetAssetObject() == null)
return false;
return true;
}
private static Object GetAssetObject()
{
for (int i = 0; i < Selection.objects.Length; i++)
{
if (AssetDatabase.GetAssetPath(Selection.objects[i]).EndsWith(_sourceExtension_1) )
return Selection.objects[i];
else if (AssetDatabase.GetAssetPath(Selection.objects[i]).EndsWith(_sourceExtension_2))
return Selection.objects[i];
}
return null;
}
protected Object selectedAsset;
[MenuItem("Assets/Extract From FBX")]
private static void ExtractMeshesMenuItem()
{
FBXMeshExtractor windo = EditorWindow.GetWindow<FBXMeshExtractor>();
windo.FBX = GetAssetObject();
windo.path = AssetDatabase.GetAssetPath(windo.FBX);
windo.guid = AssetDatabase.AssetPathToGUID(windo.guid);
windo.init();
}
public Object FBX;
public string guid;
public string path;
private bool[] selected;
private Object[] allAssets;
void init()
{
allAssets = AssetDatabase.LoadAllAssetsAtPath(path);
Debug.Log("paths " + allAssets.Length + " " + path);
selected = new bool[allAssets.Length];
}
Vector2 scroll = Vector2.zero;
void OnGUI()
{
scroll = EditorGUILayout.BeginScrollView(scroll);
for (int i = 0; i < allAssets.Length; i++)
{
selected[i] = EditorGUILayout.Toggle(allAssets[i].name, selected[i]);
}
EditorGUILayout.EndScrollView();
if (GUILayout.Button("extract"))
{
for (int i = 0; i < allAssets.Length; i++)
{
if (selected[i] == false)
continue;
Extract(allAssets[i]);
}
Close();
}
}
public void Extract(Object[] objs)
{
EditorUtility.DisplayProgressBar(_progressTitle, "", 0);
for (int i = 0; i < objs.Length; i++)
{
EditorUtility.DisplayProgressBar(_progressTitle, Selection.objects[i].name, (float)i / (objs.Length - 1));
Extract(objs[i]);
}
EditorUtility.ClearProgressBar();
}
private static void Extract(Object selectedObject)
{
//Create Folder Hierarchy
string selectedObjectPath = AssetDatabase.GetAssetPath(selectedObject);
string parentfolderPath = selectedObjectPath.Substring(0, selectedObjectPath.Length - (selectedObject.name.Length + 5));
string objectFolderName = selectedObject.name;
string meshFolderName = "Meshes";
string meshFolderPath = parentfolderPath + "/" + meshFolderName;
if (!AssetDatabase.IsValidFolder(meshFolderPath))
{
AssetDatabase.CreateFolder(parentfolderPath, meshFolderName);
}
//Create Meshes
Object[] objects = AssetDatabase.LoadAllAssetsAtPath(selectedObjectPath);
for (int i = 0; i < objects.Length; i++)
{
if (objects[i] is Mesh)
{
EditorUtility.DisplayProgressBar(_progressTitle, selectedObject.name + " : " + objects[i].name, (float)i / (objects.Length - 1));
Mesh mesh = Object.Instantiate(objects[i]) as Mesh;
AssetDatabase.CreateAsset(mesh, meshFolderPath + "/" + objects[i].name + _targetExtension);
}
}
//Cleanup
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}