-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFbxBrowserWindow.cs
More file actions
169 lines (137 loc) · 4.98 KB
/
FbxBrowserWindow.cs
File metadata and controls
169 lines (137 loc) · 4.98 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using Object = UnityEngine.Object;
public sealed class FbxBrowserWindow : EditorWindow
{
private const string ExpFilter = ".fbx";
private const string ModelFilter = "t:Model";
private Vector2 scrollPosition;
private readonly List<bool> selected = new();
private readonly List<string> fbxPaths = new();
private bool generateLightmapUVs = true;
private float hardAngle = 60f;
private float angleDistortion = 8f;
private float areaDistortion = 15f;
private float packMargin = 4f;
[MenuItem("Tools/FBX Browser")]
private static void ShowWindow()
{
var window = GetWindow<FbxBrowserWindow>();
window.titleContent = new GUIContent("FBX Browser");
window.Refresh();
}
private void OnEnable() =>
Refresh();
private void Refresh()
{
var guids = AssetDatabase.FindAssets(ModelFilter);
fbxPaths.Clear();
selected.Clear();
foreach (var guid in guids)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
if (path.EndsWith(ExpFilter, StringComparison.OrdinalIgnoreCase))
{
fbxPaths.Add(path);
selected.Add(false);
}
}
}
private void OnGUI()
{
DrawToolbar();
DrawList();
DrawBatchSettings();
}
private void DrawToolbar()
{
using var h = new GUILayout.HorizontalScope(EditorStyles.toolbar);
if (GUILayout.Button("Refresh", EditorStyles.toolbarButton))
Refresh();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Select All", EditorStyles.toolbarButton))
{
for (var i = 0; i < selected.Count; i++)
selected[i] = true;
}
if (GUILayout.Button("Deselect All", EditorStyles.toolbarButton))
{
for (var i = 0; i < selected.Count; i++)
selected[i] = false;
}
}
private void DrawList()
{
using var scroll = new GUILayout.ScrollViewScope(scrollPosition);
scrollPosition = scroll.scrollPosition;
for (var i = 0; i < fbxPaths.Count; i++)
{
using var h = new GUILayout.HorizontalScope();
selected[i] = GUILayout.Toggle(selected[i], GUIContent.none, GUILayout.Width(20));
var obj = AssetDatabase.LoadAssetAtPath<Object>(fbxPaths[i]);
if (!GUILayout.Button(obj.name, EditorStyles.linkLabel))
continue;
EditorGUIUtility.PingObject(obj);
Selection.activeObject = obj;
}
if (GUILayout.Button("Select In Project"))
Selection.objects = fbxPaths.Where((_, i) => selected[i]).Select(AssetDatabase.LoadAssetAtPath<Object>).ToArray();
}
private void DrawBatchSettings()
{
GUILayout.Space(10);
GUILayout.Label("Batch Settings", EditorStyles.boldLabel);
generateLightmapUVs = GUILayout.Toggle(generateLightmapUVs, "Generate Lightmap UVs");
using (new GUILayout.VerticalScope("box"))
{
hardAngle = EditorGUILayout.Slider("Hard Angle", hardAngle, 0f, 180f);
angleDistortion = EditorGUILayout.Slider("Angle Distortion", angleDistortion, 1f, 75f);
areaDistortion = EditorGUILayout.Slider("Area Distortion", areaDistortion, 1f, 100f);
packMargin = EditorGUILayout.Slider("Pack Margin", packMargin, 1f, 64f);
}
if (GUILayout.Button("Apply Lightmap UV Settings To Selected"))
{
ApplyBatch(importer =>
{
importer.generateSecondaryUV = generateLightmapUVs;
importer.secondaryUVHardAngle = hardAngle;
importer.secondaryUVAngleDistortion = angleDistortion;
importer.secondaryUVAreaDistortion = areaDistortion;
importer.secondaryUVPackMargin = packMargin;
});
}
GUILayout.Space(10);
using var h = new GUILayout.HorizontalScope();
if (GUILayout.Button("Toggle Read/Write"))
ApplyBatch(i => i.isReadable = !i.isReadable);
if (GUILayout.Button("Toggle Optimize Mesh Polygons"))
ApplyBatch(i => i.optimizeMeshPolygons = !i.optimizeMeshPolygons);
}
private void ApplyBatch(Action<ModelImporter> mutator)
{
var paths = fbxPaths.Where((_, i) => selected[i]).ToList();
if (paths.Count == 0)
{
EditorUtility.DisplayDialog("FBX Browser", "No FBX selected.", "OK");
return;
}
try
{
AssetDatabase.StartAssetEditing();
foreach (var importer in paths.Select(path => (ModelImporter)AssetImporter.GetAtPath(path)))
{
mutator(importer);
importer.SaveAndReimport();
}
}
finally
{
AssetDatabase.StopAssetEditing();
}
}
}
#endif