Skip to content

Commit e2aebe2

Browse files
committed
refactor export model window to abstract class
- export model and convert to prefab windows derive from this class
1 parent a54d123 commit e2aebe2

File tree

4 files changed

+188
-61
lines changed

4 files changed

+188
-61
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public static GameObject[] CreateInstantiatedModelPrefab (
8686
var wasExported = new List<GameObject>();
8787
foreach(var go in toExport) {
8888
try {
89-
wasExported.Add(Convert(go,
90-
directoryFullPath: directoryFullPath));
89+
ConvertToPrefabEditorWindow.Init(go);
90+
wasExported.Add(go);
9191
} catch(System.Exception xcp) {
9292
Debug.LogException(xcp);
9393
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
using FbxExporters.EditorTools;
6+
using UnityEditor.Presets;
7+
8+
namespace FbxExporters
9+
{
10+
namespace Editor
11+
{
12+
public class ConvertToPrefabEditorWindow : ExportOptionsEditorWindow
13+
{
14+
protected override GUIContent m_windowTitle { get { return new GUIContent ("Convert Options"); }}
15+
private GameObject m_toConvert;
16+
17+
public static void Init (GameObject toConvert)
18+
{
19+
ConvertToPrefabEditorWindow window = CreateWindow<ConvertToPrefabEditorWindow> ();
20+
window.InitializeWindow (filename: toConvert.name, singleHierarchyExport: true, exportType: ModelExporter.AnimationExportType.all);
21+
window.SetGameObjectToConvert (toConvert);
22+
window.Show ();
23+
}
24+
25+
public void SetGameObjectToConvert(GameObject toConvert){
26+
m_toConvert = toConvert;
27+
}
28+
29+
protected override void OnEnable ()
30+
{
31+
base.OnEnable ();
32+
33+
if (!m_innerEditor) {
34+
var ms = ExportSettings.instance.exportModelSettings;
35+
if (!ms) {
36+
ExportSettings.LoadSettings ();
37+
ms = ExportSettings.instance.exportModelSettings;
38+
}
39+
m_innerEditor = UnityEditor.Editor.CreateEditor (ms);
40+
this.SetSingleHierarchyExport (m_singleHierarchyExport);
41+
}
42+
}
43+
44+
protected override void Export ()
45+
{
46+
var filePath = ExportSettings.GetExportModelAbsoluteSavePath ();
47+
48+
filePath = System.IO.Path.Combine (filePath, m_exportFileName + ".fbx");
49+
50+
// check if file already exists, give a warning if it does
51+
if (System.IO.File.Exists (filePath)) {
52+
bool overwrite = UnityEditor.EditorUtility.DisplayDialog (
53+
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME),
54+
string.Format("File {0} already exists.", filePath),
55+
"Overwrite", "Cancel");
56+
if (!overwrite) {
57+
this.Close ();
58+
59+
if (GUI.changed) {
60+
SaveExportSettings ();
61+
}
62+
return;
63+
}
64+
}
65+
66+
if (!m_toConvert) {
67+
Debug.LogError ("FbxExporter: missing object for conversion");
68+
}
69+
ConvertToModel.Convert (m_toConvert, fbxFullPath: filePath);
70+
}
71+
72+
protected override void CreateCustomUI ()
73+
{
74+
base.CreateCustomUI ();
75+
}
76+
}
77+
}
78+
}

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/FbxExporters/Editor/ExportModelEditorWindow.cs

Lines changed: 97 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,25 @@ namespace FbxExporters
99
{
1010
namespace Editor
1111
{
12-
public class ExportModelEditorWindow : EditorWindow
12+
public abstract class ExportOptionsEditorWindow : EditorWindow
1313
{
14+
protected const string DefaultWindowTitle = "Export Options";
15+
protected const float SelectableLabelMinWidth = 90;
16+
protected const float BrowseButtonWidth = 25;
17+
protected const float LabelWidth = 175;
18+
protected const float FieldOffset = 18;
19+
protected const float TextFieldAlignOffset = 3;
20+
protected const float ExportButtonWidth = 100;
21+
protected const float FbxExtOffset = -7;
1422

15-
private const string WindowTitle = "Export Options";
16-
private const float SelectableLabelMinWidth = 90;
17-
private const float BrowseButtonWidth = 25;
18-
private const float LabelWidth = 175;
19-
private const float FieldOffset = 18;
20-
private const float TextFieldAlignOffset = 3;
21-
private const float ExportButtonWidth = 100;
22-
private const float FbxExtOffset = -7;
23+
protected virtual GUIContent m_windowTitle { get { return new GUIContent (DefaultWindowTitle); } }
2324

24-
private string m_exportFileName = "";
25-
private ModelExporter.AnimationExportType m_animExportType = ModelExporter.AnimationExportType.all;
26-
private bool m_singleHierarchyExport = true;
25+
protected string m_exportFileName = "";
26+
protected ModelExporter.AnimationExportType m_animExportType = ModelExporter.AnimationExportType.all;
27+
protected bool m_singleHierarchyExport = true;
2728

28-
private ExportModelSettingsEditor m_innerEditor;
29-
private static FbxExportPresetSelectorReceiver m_receiver;
29+
protected UnityEditor.Editor m_innerEditor;
30+
private FbxExportPresetSelectorReceiver m_receiver;
3031

3132
private static GUIContent presetIcon { get { return EditorGUIUtility.IconContent ("Preset.Context"); }}
3233
private static GUIStyle presetIconButton { get { return new GUIStyle("IconButton"); }}
@@ -37,21 +38,11 @@ public class ExportModelEditorWindow : EditorWindow
3738
private GUIStyle m_fbxExtLabelStyle;
3839
private float m_fbxExtLabelWidth;
3940

40-
void OnEnable(){
41+
protected virtual void OnEnable(){
4142
InitializeReceiver ();
4243
m_showOptions = true;
4344
this.minSize = new Vector2 (SelectableLabelMinWidth + LabelWidth + BrowseButtonWidth, 220);
4445

45-
if (!m_innerEditor) {
46-
var ms = ExportSettings.instance.exportModelSettings;
47-
if (!ms) {
48-
ExportSettings.LoadSettings ();
49-
ms = ExportSettings.instance.exportModelSettings;
50-
}
51-
m_innerEditor = UnityEditor.Editor.CreateEditor (ms) as ExportModelSettingsEditor;
52-
m_innerEditor.SetIsSingleHierarchy (m_singleHierarchyExport);
53-
}
54-
5546
m_nameTextFieldStyle = new GUIStyle(GUIStyle.none);
5647
m_nameTextFieldStyle.alignment = TextAnchor.LowerCenter;
5748
m_nameTextFieldStyle.clipping = TextClipping.Clip;
@@ -64,13 +55,19 @@ void OnEnable(){
6455
m_fbxExtLabelWidth = m_fbxExtLabelStyle.CalcSize (new GUIContent (".fbx")).x;
6556
}
6657

67-
public static void Init (string filename = "", bool singleHierarchyExport = true, ModelExporter.AnimationExportType exportType = ModelExporter.AnimationExportType.all)
68-
{
69-
ExportModelEditorWindow window = (ExportModelEditorWindow)EditorWindow.GetWindow <ExportModelEditorWindow>(WindowTitle, focus:true);
70-
window.SetFilename (filename);
71-
window.SetAnimationExportType (exportType);
72-
window.SetSingleHierarchyExport (singleHierarchyExport);
73-
window.Show ();
58+
protected static T CreateWindow<T>() where T : EditorWindow {
59+
return (T)EditorWindow.GetWindow <T>(DefaultWindowTitle, focus:true);
60+
}
61+
62+
protected virtual void InitializeWindow(string filename = "", bool singleHierarchyExport = true, ModelExporter.AnimationExportType exportType = ModelExporter.AnimationExportType.all){
63+
this.SetTitle ();
64+
this.SetFilename (filename);
65+
this.SetAnimationExportType (exportType);
66+
this.SetSingleHierarchyExport (singleHierarchyExport);
67+
}
68+
69+
private void SetTitle(){
70+
this.titleContent = m_windowTitle;
7471
}
7572

7673
private void InitializeReceiver(){
@@ -101,7 +98,10 @@ public void SetSingleHierarchyExport(bool singleHierarchy){
10198
m_singleHierarchyExport = singleHierarchy;
10299

103100
if (m_innerEditor) {
104-
m_innerEditor.SetIsSingleHierarchy (m_singleHierarchyExport);
101+
var exportModelSettingsEditor = m_innerEditor as ExportModelSettingsEditor;
102+
if (exportModelSettingsEditor) {
103+
exportModelSettingsEditor.SetIsSingleHierarchy (m_singleHierarchyExport);
104+
}
105105
}
106106
}
107107

@@ -117,7 +117,14 @@ public void OnPresetSelectionChanged()
117117
this.Repaint ();
118118
}
119119

120-
void OnGUI ()
120+
protected abstract void Export ();
121+
122+
/// <summary>
123+
/// Function to be used by derived classes to add custom UI between the file path selector and export options.
124+
/// </summary>
125+
protected virtual void CreateCustomUI(){}
126+
127+
protected void OnGUI ()
121128
{
122129
// Increasing the label width so that none of the text gets cut off
123130
EditorGUIUtility.labelWidth = LabelWidth;
@@ -196,6 +203,8 @@ void OnGUI ()
196203
}
197204
GUILayout.EndHorizontal();
198205

206+
CreateCustomUI();
207+
199208
EditorGUILayout.Space ();
200209
EditorGUI.indentLevel--;
201210
m_showOptions = EditorGUILayout.Foldout (m_showOptions, "Options");
@@ -213,31 +222,7 @@ void OnGUI ()
213222
}
214223

215224
if (GUILayout.Button ("Export", GUILayout.Width(ExportButtonWidth))) {
216-
var filePath = ExportSettings.GetExportModelAbsoluteSavePath ();
217-
218-
filePath = System.IO.Path.Combine (filePath, m_exportFileName + ".fbx");
219-
220-
// check if file already exists, give a warning if it does
221-
if (System.IO.File.Exists (filePath)) {
222-
bool overwrite = UnityEditor.EditorUtility.DisplayDialog (
223-
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME),
224-
string.Format("File {0} already exists.", filePath),
225-
"Overwrite", "Cancel");
226-
if (!overwrite) {
227-
this.Close ();
228-
229-
if (GUI.changed) {
230-
SaveExportSettings ();
231-
}
232-
return;
233-
}
234-
}
235-
236-
if (ModelExporter.ExportObjects (filePath, exportType: m_animExportType, lodExportType: ExportSettings.GetLODExportType()) != null) {
237-
// refresh the asset database so that the file appears in the
238-
// asset folder view.
239-
AssetDatabase.Refresh ();
240-
}
225+
Export ();
241226
this.Close ();
242227
}
243228
GUILayout.EndHorizontal ();
@@ -247,5 +232,58 @@ void OnGUI ()
247232
}
248233
}
249234
}
235+
236+
public class ExportModelEditorWindow : ExportOptionsEditorWindow
237+
{
238+
public static void Init (string filename = "", bool singleHierarchyExport = true, ModelExporter.AnimationExportType exportType = ModelExporter.AnimationExportType.all)
239+
{
240+
ExportModelEditorWindow window = CreateWindow<ExportModelEditorWindow> ();
241+
window.InitializeWindow (filename, singleHierarchyExport, exportType);
242+
window.Show ();
243+
}
244+
245+
protected override void OnEnable ()
246+
{
247+
base.OnEnable ();
248+
249+
if (!m_innerEditor) {
250+
var ms = ExportSettings.instance.exportModelSettings;
251+
if (!ms) {
252+
ExportSettings.LoadSettings ();
253+
ms = ExportSettings.instance.exportModelSettings;
254+
}
255+
m_innerEditor = UnityEditor.Editor.CreateEditor (ms);
256+
this.SetSingleHierarchyExport (m_singleHierarchyExport);
257+
}
258+
}
259+
260+
protected override void Export(){
261+
var filePath = ExportSettings.GetExportModelAbsoluteSavePath ();
262+
263+
filePath = System.IO.Path.Combine (filePath, m_exportFileName + ".fbx");
264+
265+
// check if file already exists, give a warning if it does
266+
if (System.IO.File.Exists (filePath)) {
267+
bool overwrite = UnityEditor.EditorUtility.DisplayDialog (
268+
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME),
269+
string.Format("File {0} already exists.", filePath),
270+
"Overwrite", "Cancel");
271+
if (!overwrite) {
272+
this.Close ();
273+
274+
if (GUI.changed) {
275+
SaveExportSettings ();
276+
}
277+
return;
278+
}
279+
}
280+
281+
if (ModelExporter.ExportObjects (filePath, exportType: m_animExportType, lodExportType: ExportSettings.GetLODExportType()) != null) {
282+
// refresh the asset database so that the file appears in the
283+
// asset folder view.
284+
AssetDatabase.Refresh ();
285+
}
286+
}
287+
}
250288
}
251289
}

0 commit comments

Comments
 (0)