using UnityEngine using UnityEditor public class FBXMeshExtractor priv

 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
using UnityEngine;
using UnityEditor;
public class FBXMeshExtractor
{
private static string _progressTitle = "Extracting Meshes";
private static string _sourceExtension = ".FBX";
private static string _targetExtension = ".asset";
[MenuItem("Assets/Extract Meshes", validate = true)]
private static bool ExtractMeshesMenuItemValidate()
{
for (int i = 0; i < Selection.objects.Length; i++)
{
if (!AssetDatabase.GetAssetPath(Selection.objects[i]).EndsWith(_sourceExtension))
return false;
}
return true;
}
[MenuItem("Assets/Extract Meshes")]
private static void ExtractMeshesMenuItem()
{
EditorUtility.DisplayProgressBar(_progressTitle, "", 0);
for (int i = 0; i < Selection.objects.Length; i++)
{
EditorUtility.DisplayProgressBar(_progressTitle, Selection.objects[i].name, (float)i / (Selection.objects.Length - 1));
ExtractMeshes(Selection.objects[i]);
}
EditorUtility.ClearProgressBar();
}
private static void ExtractMeshes(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 objectFolderPath = parentfolderPath + "/" + objectFolderName;
string meshFolderName = "Meshes";
string meshFolderPath = objectFolderPath + "/" + meshFolderName;
if (!AssetDatabase.IsValidFolder(objectFolderPath))
{
AssetDatabase.CreateFolder(parentfolderPath, objectFolderName);
if (!AssetDatabase.IsValidFolder(meshFolderPath))
{
AssetDatabase.CreateFolder(objectFolderPath, 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.Trim() + _targetExtension);
}
}
//Cleanup
AssetDatabase.MoveAsset(selectedObjectPath, objectFolderPath + "/" + selectedObject.name + _sourceExtension);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}