Skip to content

Commit 2bac1fc

Browse files
committed
Set the default filename to be the root gameobject
Unless there are multiple, in which case just do what we were doing before
1 parent 329cfd3 commit 2bac1fc

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
7676
string dirPath = Path.Combine (Application.dataPath, "Objects");
7777

7878
for(int n = 0; n < gosToExport.Length; n++){
79-
filePaths[n] = Path.Combine (dirPath, gosToExport[n].name + ".fbx");
79+
string filename = ModelExporter.ConvertToValidFilename (gosToExport [n].name + ".fbx");
80+
filePaths[n] = Path.Combine (dirPath, filename);
8081
}
8182

8283
string[] fbxFileNames = new string[filePaths.Length];
@@ -91,7 +92,7 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
9192
{
9293
var fbxFileName = fbxFileNames [i];
9394
if (fbxFileName == null) {
94-
Debug.Log (string.Format ("Warning: Export failed for GameObject {0}", gosToExport [i].name));
95+
Debug.LogWarning (string.Format ("Warning: Export failed for GameObject {0}", gosToExport [i].name));
9596
continue;
9697
}
9798

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,10 @@ public static bool OnValidateMenuItem ()
725725
}
726726

727727
// Add a menu item called "Export Model..." to a GameObject's context menu.
728-
[MenuItem ("GameObject/Export Model... %e", false, 30)]
728+
// NOTE: The ellipsis at the end of the Menu Item name prevents the context
729+
// from being passed to command, thus resulting in OnContextItem()
730+
// being called only once regardless of what is selected.
731+
[MenuItem ("GameObject/Export Model...", false, 30)]
729732
static void OnContextItem (MenuCommand command)
730733
{
731734
OnExport ();
@@ -956,13 +959,19 @@ private static void OnExport ()
956959
? Application.dataPath
957960
: System.IO.Path.GetDirectoryName (LastFilePath);
958961

959-
var filename = string.IsNullOrEmpty (LastFilePath)
960-
? MakeFileName (basename: FileBaseName, extension: Extension)
961-
: System.IO.Path.GetFileName (LastFilePath);
962+
GameObject [] selectedGOs = Selection.GetFiltered<GameObject> (SelectionMode.TopLevel);
963+
string filename = null;
964+
if (selectedGOs.Length == 1) {
965+
filename = ConvertToValidFilename (selectedGOs [0].name + ".fbx");
966+
} else {
967+
filename = string.IsNullOrEmpty (LastFilePath)
968+
? MakeFileName (basename: FileBaseName, extension: Extension)
969+
: System.IO.Path.GetFileName (LastFilePath);
970+
}
962971

963972
var title = string.Format ("Export Model FBX ({0})", FileBaseName);
964973

965-
var filePath = EditorUtility.SaveFilePanel (title, directory, filename, "");
974+
var filePath = EditorUtility.SaveFilePanel (title, directory, filename, "fbx");
966975

967976
if (string.IsNullOrEmpty (filePath)) {
968977
return;
@@ -1011,6 +1020,11 @@ private static void EnsureDirectory (string path)
10111020
Directory.CreateDirectory (fileInfo.Directory.FullName);
10121021
}
10131022
}
1023+
1024+
public static string ConvertToValidFilename(string filename)
1025+
{
1026+
return System.Text.RegularExpressions.Regex.Replace (filename, "[" + new string(Path.GetInvalidFileNameChars()) + "]", "_");
1027+
}
10141028
}
10151029
}
10161030
}

0 commit comments

Comments
 (0)