@@ -36,9 +36,13 @@ public class ModelExporter : System.IDisposable
36
36
37
37
const string ProgressBarTitle = "Fbx Export" ;
38
38
39
+ const char MayaNamespaceSeparator = ':' ;
40
+
41
+ // replace invalid chars with this one
39
42
const char InvalidCharReplacement = '_' ;
40
43
41
- const char MayaNamespaceSeparator = ':' ;
44
+ const string RegexCharStart = "[" ;
45
+ const string RegexCharEnd = "]" ;
42
46
43
47
/// <summary>
44
48
/// Create instance of example
@@ -243,7 +247,7 @@ public void ExportTexture (Material unityMaterial, string unityPropName,
243
247
/// <summary>
244
248
/// Get the color of a material, or grey if we can't find it.
245
249
/// </summary>
246
- public FbxDouble3 ? GetMaterialColor ( Material unityMaterial , string unityPropName , float defaultValue = 1 )
250
+ public FbxDouble3 GetMaterialColor ( Material unityMaterial , string unityPropName , float defaultValue = 1 )
247
251
{
248
252
if ( ! unityMaterial ) {
249
253
return new FbxDouble3 ( defaultValue ) ;
@@ -733,7 +737,10 @@ public static bool OnValidateMenuItem ()
733
737
}
734
738
735
739
// Add a menu item called "Export Model..." to a GameObject's context menu.
736
- [ MenuItem ( "GameObject/Export Model... %e" , false , 30 ) ]
740
+ // NOTE: The ellipsis at the end of the Menu Item name prevents the context
741
+ // from being passed to command, thus resulting in OnContextItem()
742
+ // being called only once regardless of what is selected.
743
+ [ MenuItem ( "GameObject/Export Model..." , false , 30 ) ]
737
744
static void OnContextItem ( MenuCommand command )
738
745
{
739
746
OnExport ( ) ;
@@ -969,13 +976,19 @@ private static void OnExport ()
969
976
? Application . dataPath
970
977
: System . IO . Path . GetDirectoryName ( LastFilePath ) ;
971
978
972
- var filename = string . IsNullOrEmpty ( LastFilePath )
973
- ? MakeFileName ( basename : FileBaseName , extension : Extension )
974
- : System . IO . Path . GetFileName ( LastFilePath ) ;
979
+ GameObject [ ] selectedGOs = Selection . GetFiltered < GameObject > ( SelectionMode . TopLevel ) ;
980
+ string filename = null ;
981
+ if ( selectedGOs . Length == 1 ) {
982
+ filename = ConvertToValidFilename ( selectedGOs [ 0 ] . name + ".fbx" ) ;
983
+ } else {
984
+ filename = string . IsNullOrEmpty ( LastFilePath )
985
+ ? MakeFileName ( basename : FileBaseName , extension : Extension )
986
+ : System . IO . Path . GetFileName ( LastFilePath ) ;
987
+ }
975
988
976
989
var title = string . Format ( "Export Model FBX ({0})" , FileBaseName ) ;
977
990
978
- var filePath = EditorUtility . SaveFilePanel ( title , directory , filename , "" ) ;
991
+ var filePath = EditorUtility . SaveFilePanel ( title , directory , filename , "fbx " ) ;
979
992
980
993
if ( string . IsNullOrEmpty ( filePath ) ) {
981
994
return ;
@@ -1066,6 +1079,14 @@ private static string ConvertToMayaCompatibleName(string name)
1066
1079
}
1067
1080
return newName ;
1068
1081
}
1082
+
1083
+ public static string ConvertToValidFilename ( string filename )
1084
+ {
1085
+ return System . Text . RegularExpressions . Regex . Replace ( filename ,
1086
+ RegexCharStart + new string ( Path . GetInvalidFileNameChars ( ) ) + RegexCharEnd ,
1087
+ InvalidCharReplacement . ToString ( )
1088
+ ) ;
1089
+ }
1069
1090
}
1070
1091
}
1071
1092
}
0 commit comments