Skip to content

Commit 0318d0d

Browse files
committed
Merge branch 'master' into UNI-41933-convert-UI-dont-overwrite-existing-fbx
2 parents 2eb4a06 + 605273d commit 0318d0d

12 files changed

+922
-212
lines changed

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using UnityEngine;
44
using UnityEditor;
55
using FbxExporters.EditorTools;
6+
#if UNITY_2018_1_OR_NEWER
67
using UnityEditor.Presets;
8+
#endif
79
using System.Linq;
810

911
namespace FbxExporters
@@ -13,13 +15,23 @@ namespace Editor
1315
public class ConvertToPrefabEditorWindow : ExportOptionsEditorWindow
1416
{
1517
protected override GUIContent WindowTitle { get { return new GUIContent ("Convert Options"); }}
16-
protected override float MinWindowHeight { get { return 280; } } // determined by trial and error
18+
protected override float MinWindowHeight { get { return 350; } } // determined by trial and error
1719
protected override string ExportButtonName { get { return "Convert"; } }
18-
private GameObject[] m_toConvert;
1920
private string m_prefabFileName = "";
2021

2122
private float m_prefabExtLabelWidth;
2223

24+
protected override bool DisableNameSelection {
25+
get {
26+
return (ToExport != null && ToExport.Length > 1);
27+
}
28+
}
29+
protected override bool DisableTransferAnim {
30+
get {
31+
return ToExport == null || ToExport.Length > 1;
32+
}
33+
}
34+
2335
public static void Init (IEnumerable<GameObject> toConvert)
2436
{
2537
ConvertToPrefabEditorWindow window = CreateWindow<ConvertToPrefabEditorWindow> ();
@@ -29,10 +41,12 @@ public static void Init (IEnumerable<GameObject> toConvert)
2941
}
3042

3143
protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
32-
m_toConvert = toConvert.OrderBy (go => go.name).ToArray ();
44+
ToExport = toConvert.OrderBy (go => go.name).ToArray ();
3345

34-
if (m_toConvert.Length == 1) {
35-
var go = m_toConvert [0];
46+
TransferAnimationSource = null;
47+
TransferAnimationDest = null;
48+
if (ToExport.Length == 1) {
49+
var go = ModelExporter.GetGameObject (ToExport [0]);
3650
// check if the GameObject is a model instance, use as default filename and path if it is
3751
if(ConvertToModel.IsModelInstance(go)) {
3852
var mainAsset = PrefabUtility.GetPrefabParent (go) as GameObject;
@@ -42,12 +56,20 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
4256

4357
m_prefabFileName = System.IO.Path.GetFileNameWithoutExtension (mainAssetRelPath);
4458
ExportSettings.AddFbxSavePath (System.IO.Path.GetDirectoryName (mainAssetRelPath));
45-
} else {
46-
m_prefabFileName = go.name;
4759
}
48-
} else if (m_toConvert.Length > 1) {
60+
else{
61+
m_prefabFileName = ToExport [0].name;
62+
}
63+
64+
// if only one object selected, set transfer source/dest to this object
65+
if (go) {
66+
TransferAnimationSource = go.transform;
67+
TransferAnimationDest = go.transform;
68+
}
69+
} else if (ToExport.Length > 1) {
4970
m_prefabFileName = "(automatic)";
5071
}
72+
5173
this.SetFilename (m_prefabFileName);
5274
}
5375

@@ -68,13 +90,13 @@ protected override void Export ()
6890
var prefabDirPath = ExportSettings.GetPrefabAbsoluteSavePath ();
6991
var prefabPath = System.IO.Path.Combine (prefabDirPath, m_prefabFileName + ".prefab");
7092

71-
if (m_toConvert == null) {
93+
if (ToExport == null) {
7294
Debug.LogError ("FbxExporter: missing object for conversion");
7395
return;
7496
}
7597

76-
if (m_toConvert.Length == 1) {
77-
var go = m_toConvert [0];
98+
if (ToExport.Length == 1) {
99+
var go = ModelExporter.GetGameObject (ToExport [0]);
78100

79101
if (!OverwriteExistingFile (prefabPath)) {
80102
return;
@@ -106,31 +128,32 @@ protected override void Export ()
106128
return;
107129
}
108130

109-
foreach (var go in m_toConvert) {
131+
foreach (var obj in ToExport) {
132+
var go = ModelExporter.GetGameObject (obj);
110133
ConvertToModel.Convert (
111134
go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
112135
);
113136
}
114137
}
115138

116-
protected override bool DisableNameSelection ()
139+
protected override ExportOptionsSettingsSerializeBase SettingsObject
117140
{
118-
return m_toConvert.Length > 1;
141+
get { return ExportSettings.instance.convertToPrefabSettings.info; }
119142
}
120-
143+
#if UNITY_2018_1_OR_NEWER
121144
protected override void ShowPresetReceiver ()
122145
{
123146
ShowPresetReceiver (ExportSettings.instance.convertToPrefabSettings);
124147
}
125-
148+
#endif
126149
protected override void CreateCustomUI ()
127150
{
128151
GUILayout.BeginHorizontal ();
129152
EditorGUILayout.LabelField(new GUIContent(
130-
"Prefab Name:",
153+
"Prefab Name",
131154
"Filename to save prefab to."),GUILayout.Width(LabelWidth-TextFieldAlignOffset));
132155

133-
EditorGUI.BeginDisabledGroup (DisableNameSelection());
156+
EditorGUI.BeginDisabledGroup (DisableNameSelection);
134157
// Show the export name with an uneditable ".prefab" at the end
135158
//-------------------------------------
136159
EditorGUILayout.BeginVertical ();
@@ -152,7 +175,7 @@ protected override void CreateCustomUI ()
152175

153176
GUILayout.BeginHorizontal();
154177
EditorGUILayout.LabelField(new GUIContent(
155-
"Prefab Path:",
178+
"Prefab Path",
156179
"Relative path for saving Linked Prefabs."),GUILayout.Width(LabelWidth - FieldOffset));
157180

158181
var pathLabels = ExportSettings.GetRelativePrefabSavePaths();
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
using FbxExporters.EditorTools;
6+
#if UNITY_2018_1_OR_NEWER
7+
using UnityEditor.Presets;
8+
#endif
9+
using System.Linq;
10+
11+
namespace FbxExporters
12+
{
13+
namespace Editor
14+
{
15+
public class ConvertToPrefabEditorWindow : ExportOptionsEditorWindow
16+
{
17+
protected override GUIContent WindowTitle { get { return new GUIContent ("Convert Options"); }}
18+
protected override float MinWindowHeight { get { return 350; } } // determined by trial and error
19+
protected override string ExportButtonName { get { return "Convert"; } }
20+
private string m_prefabFileName = "";
21+
22+
private float m_prefabExtLabelWidth;
23+
24+
protected override bool DisableNameSelection {
25+
get {
26+
return (ToExport != null && ToExport.Length > 1);
27+
}
28+
}
29+
protected override bool DisableTransferAnim {
30+
get {
31+
return ToExport == null || ToExport.Length > 1;
32+
}
33+
}
34+
35+
public static void Init (IEnumerable<GameObject> toConvert)
36+
{
37+
ConvertToPrefabEditorWindow window = CreateWindow<ConvertToPrefabEditorWindow> ();
38+
window.InitializeWindow ();
39+
window.SetGameObjectsToConvert (toConvert);
40+
window.Show ();
41+
}
42+
43+
protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
44+
ToExport = toConvert.OrderBy (go => go.name).ToArray ();
45+
46+
TransferAnimationSource = null;
47+
TransferAnimationDest = null;
48+
49+
<<<<<<< HEAD
50+
if (m_toConvert.Length == 1) {
51+
var go = m_toConvert [0];
52+
// check if the GameObject is a model instance, use as default filename and path if it is
53+
if(ConvertToModel.IsModelInstance(go)) {
54+
var mainAsset = PrefabUtility.GetPrefabParent (go) as GameObject;
55+
var mainAssetRelPath = AssetDatabase.GetAssetPath (mainAsset);
56+
// remove Assets/ from beginning of path
57+
mainAssetRelPath = mainAssetRelPath.Substring ("Assets".Length);
58+
59+
m_prefabFileName = System.IO.Path.GetFileNameWithoutExtension (mainAssetRelPath);
60+
ExportSettings.AddFbxSavePath (System.IO.Path.GetDirectoryName (mainAssetRelPath));
61+
} else {
62+
m_prefabFileName = go.name;
63+
}
64+
} else if (m_toConvert.Length > 1) {
65+
=======
66+
if (ToExport.Length == 1) {
67+
m_prefabFileName = ToExport [0].name;
68+
69+
// if only one object selected, set transfer source/dest to this object
70+
var go = ModelExporter.GetGameObject (ToExport [0]);
71+
if (go) {
72+
TransferAnimationSource = go.transform;
73+
TransferAnimationDest = go.transform;
74+
}
75+
} else if (ToExport.Length > 1) {
76+
>>>>>>> master
77+
m_prefabFileName = "(automatic)";
78+
}
79+
80+
this.SetFilename (m_prefabFileName);
81+
}
82+
83+
protected override void OnEnable ()
84+
{
85+
base.OnEnable ();
86+
if (!m_innerEditor) {
87+
m_innerEditor = UnityEditor.Editor.CreateEditor (ExportSettings.instance.convertToPrefabSettings);
88+
}
89+
m_prefabExtLabelWidth = m_fbxExtLabelStyle.CalcSize (new GUIContent (".prefab")).x;
90+
}
91+
92+
protected override void Export ()
93+
{
94+
var fbxDirPath = ExportSettings.GetFbxAbsoluteSavePath ();
95+
var fbxPath = System.IO.Path.Combine (fbxDirPath, m_exportFileName + ".fbx");
96+
97+
var prefabDirPath = ExportSettings.GetPrefabAbsoluteSavePath ();
98+
var prefabPath = System.IO.Path.Combine (prefabDirPath, m_prefabFileName + ".prefab");
99+
100+
<<<<<<< HEAD
101+
if (m_toConvert == null) {
102+
=======
103+
// check if file already exists, give a warning if it does
104+
if (!OverwriteExistingFile (fbxPath) || !OverwriteExistingFile (prefabPath)) {
105+
return;
106+
}
107+
108+
if (ToExport == null) {
109+
>>>>>>> master
110+
Debug.LogError ("FbxExporter: missing object for conversion");
111+
return;
112+
}
113+
114+
<<<<<<< HEAD
115+
if (m_toConvert.Length == 1) {
116+
var go = m_toConvert [0];
117+
118+
if (!OverwriteExistingFile (prefabPath)) {
119+
return;
120+
}
121+
122+
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
123+
if(ConvertToModel.IsModelInstance(go)) {
124+
// don't re-export fbx
125+
// create prefab out of model instance in scene, link to existing fbx
126+
var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject;
127+
var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset);
128+
var mainAssetAbsPath = System.IO.Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath;
129+
var relPrefabPath = ExportSettings.GetProjectRelativePath (prefabPath);
130+
131+
if (string.Equals(System.IO.Path.GetFullPath(fbxPath), System.IO.Path.GetFullPath(mainAssetAbsPath))) {
132+
ConvertToModel.SetupFbxPrefab(go, mainAsset, relPrefabPath, mainAssetAbsPath);
133+
return;
134+
}
135+
}
136+
137+
// check if file already exists, give a warning if it does
138+
if (!OverwriteExistingFile (fbxPath)) {
139+
return;
140+
}
141+
142+
=======
143+
if (ToExport.Length == 1) {
144+
var go = ModelExporter.GetGameObject (ToExport [0]);
145+
>>>>>>> master
146+
ConvertToModel.Convert (
147+
go, fbxFullPath: fbxPath, prefabFullPath: prefabPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
148+
);
149+
return;
150+
}
151+
152+
foreach (var obj in ToExport) {
153+
var go = ModelExporter.GetGameObject (obj);
154+
ConvertToModel.Convert (
155+
go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
156+
);
157+
}
158+
}
159+
160+
protected override ExportOptionsSettingsSerializeBase SettingsObject
161+
{
162+
get { return ExportSettings.instance.convertToPrefabSettings.info; }
163+
}
164+
#if UNITY_2018_1_OR_NEWER
165+
protected override void ShowPresetReceiver ()
166+
{
167+
ShowPresetReceiver (ExportSettings.instance.convertToPrefabSettings);
168+
}
169+
#endif
170+
protected override void CreateCustomUI ()
171+
{
172+
GUILayout.BeginHorizontal ();
173+
EditorGUILayout.LabelField(new GUIContent(
174+
"Prefab Name",
175+
"Filename to save prefab to."),GUILayout.Width(LabelWidth-TextFieldAlignOffset));
176+
177+
EditorGUI.BeginDisabledGroup (DisableNameSelection);
178+
// Show the export name with an uneditable ".prefab" at the end
179+
//-------------------------------------
180+
EditorGUILayout.BeginVertical ();
181+
EditorGUILayout.BeginHorizontal(EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
182+
EditorGUI.indentLevel--;
183+
// continually resize to contents
184+
var textFieldSize = m_nameTextFieldStyle.CalcSize (new GUIContent(m_prefabFileName));
185+
m_prefabFileName = EditorGUILayout.TextField (m_prefabFileName, m_nameTextFieldStyle, GUILayout.Width(textFieldSize.x + 5), GUILayout.MinWidth(5));
186+
m_prefabFileName = ModelExporter.ConvertToValidFilename (m_prefabFileName);
187+
188+
EditorGUILayout.LabelField ("<color=#808080ff>.prefab</color>", m_fbxExtLabelStyle, GUILayout.Width(m_prefabExtLabelWidth));
189+
EditorGUI.indentLevel++;
190+
191+
EditorGUILayout.EndHorizontal();
192+
EditorGUILayout.EndVertical ();
193+
//-----------------------------------
194+
EditorGUI.EndDisabledGroup ();
195+
GUILayout.EndHorizontal ();
196+
197+
GUILayout.BeginHorizontal();
198+
EditorGUILayout.LabelField(new GUIContent(
199+
"Prefab Path",
200+
"Relative path for saving Linked Prefabs."),GUILayout.Width(LabelWidth - FieldOffset));
201+
202+
var pathLabels = ExportSettings.GetRelativePrefabSavePaths();
203+
204+
ExportSettings.instance.selectedPrefabPath = EditorGUILayout.Popup (ExportSettings.instance.selectedPrefabPath, pathLabels, GUILayout.MinWidth(SelectableLabelMinWidth));
205+
206+
if (GUILayout.Button(new GUIContent("...", "Browse to a new location to save prefab to"), EditorStyles.miniButton, GUILayout.Width(BrowseButtonWidth)))
207+
{
208+
string initialPath = Application.dataPath;
209+
210+
string fullPath = EditorUtility.OpenFolderPanel(
211+
"Select Linked Prefab Save Path", initialPath, null
212+
);
213+
214+
// Unless the user canceled, make sure they chose something in the Assets folder.
215+
if (!string.IsNullOrEmpty(fullPath))
216+
{
217+
var relativePath = ExportSettings.ConvertToAssetRelativePath(fullPath);
218+
if (string.IsNullOrEmpty(relativePath))
219+
{
220+
Debug.LogWarning("Please select a location in the Assets folder");
221+
}
222+
else
223+
{
224+
ExportSettings.AddPrefabSavePath(relativePath);
225+
226+
// Make sure focus is removed from the selectable label
227+
// otherwise it won't update
228+
GUIUtility.hotControl = 0;
229+
GUIUtility.keyboardControl = 0;
230+
}
231+
}
232+
}
233+
GUILayout.EndHorizontal();
234+
}
235+
}
236+
}
237+
}

Assets/FbxExporters/Editor/ConvertToPrefabSettings.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,10 @@ public override void OnInspectorGUI ()
5151
EditorGUI.EndDisabledGroup ();
5252
GUILayout.EndHorizontal();
5353

54-
// TODO: add implementation for these options, grey out in the meantime
55-
EditorGUI.BeginDisabledGroup (true);
56-
GUILayout.BeginHorizontal();
57-
EditorGUILayout.LabelField(new GUIContent("Transfer Root Motion To", "Select bone to transfer root motion animation to."), GUILayout.Width(LabelWidth - FieldOffset));
58-
EditorGUILayout.Popup(0, new string[]{"<None>"});
59-
GUILayout.EndHorizontal();
60-
EditorGUI.EndDisabledGroup ();
61-
6254
exportSettings.animatedSkinnedMesh = EditorGUILayout.Toggle ("Animated Skinned Mesh", exportSettings.animatedSkinnedMesh);
6355

6456
exportSettings.mayaCompatibleNaming = EditorGUILayout.Toggle (
65-
new GUIContent ("Compatible Naming:",
57+
new GUIContent ("Compatible Naming",
6658
"In Maya some symbols such as spaces and accents get replaced when importing an FBX " +
6759
"(e.g. \"foo bar\" becomes \"fooFBXASC032bar\"). " +
6860
"On export, convert the names of GameObjects so they are Maya compatible." +

0 commit comments

Comments
 (0)