Skip to content

Commit dc16093

Browse files
authored
Merge pull request #27 from Unity-Technologies/UNI-20986-default-filename-is-root-object
UNI-20986 Set the default filename to be the root gameobject
2 parents ce44e09 + 0484de4 commit dc16093

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public class ConvertToModel : System.IDisposable
2121
const string MenuItemName1 = "Assets/Convert To Model";
2222
const string MenuItemName2 = "GameObject/Convert To Model";
2323

24-
const string RegexCharStart = "[";
25-
const string RegexCharEnd = "]";
26-
2724
/// <summary>
2825
/// Clean up this class on garbage collection
2926
/// </summary>
@@ -79,7 +76,7 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
7976
string dirPath = Path.Combine (Application.dataPath, "Objects");
8077

8178
for(int n = 0; n < gosToExport.Length; n++){
82-
string filename = ConvertToValidFilename (gosToExport [n].name + ".fbx");
79+
string filename = ModelExporter.ConvertToValidFilename (gosToExport [n].name + ".fbx");
8380
filePaths[n] = Path.Combine (dirPath, filename);
8481
}
8582

@@ -95,7 +92,7 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
9592
{
9693
var fbxFileName = fbxFileNames [i];
9794
if (fbxFileName == null) {
98-
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));
9996
continue;
10097
}
10198

@@ -140,12 +137,6 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
140137
return result;
141138
}
142139

143-
private static string ConvertToValidFilename(string filename)
144-
{
145-
return System.Text.RegularExpressions.Regex.Replace (filename,
146-
RegexCharStart + new string(Path.GetInvalidFileNameChars()) + RegexCharEnd, "_");
147-
}
148-
149140
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
150141
{
151142
Transform importedTransform = imported.transform;

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ public class ModelExporter : System.IDisposable
3636

3737
const string ProgressBarTitle = "Fbx Export";
3838

39+
const char MayaNamespaceSeparator = ':';
40+
41+
// replace invalid chars with this one
3942
const char InvalidCharReplacement = '_';
4043

41-
const char MayaNamespaceSeparator = ':';
44+
const string RegexCharStart = "[";
45+
const string RegexCharEnd = "]";
4246

4347
/// <summary>
4448
/// Create instance of example
@@ -243,7 +247,7 @@ public void ExportTexture (Material unityMaterial, string unityPropName,
243247
/// <summary>
244248
/// Get the color of a material, or grey if we can't find it.
245249
/// </summary>
246-
public FbxDouble3? GetMaterialColor (Material unityMaterial, string unityPropName, float defaultValue = 1)
250+
public FbxDouble3 GetMaterialColor (Material unityMaterial, string unityPropName, float defaultValue = 1)
247251
{
248252
if (!unityMaterial) {
249253
return new FbxDouble3(defaultValue);
@@ -733,7 +737,10 @@ public static bool OnValidateMenuItem ()
733737
}
734738

735739
// 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)]
737744
static void OnContextItem (MenuCommand command)
738745
{
739746
OnExport ();
@@ -969,13 +976,19 @@ private static void OnExport ()
969976
? Application.dataPath
970977
: System.IO.Path.GetDirectoryName (LastFilePath);
971978

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+
}
975988

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

978-
var filePath = EditorUtility.SaveFilePanel (title, directory, filename, "");
991+
var filePath = EditorUtility.SaveFilePanel (title, directory, filename, "fbx");
979992

980993
if (string.IsNullOrEmpty (filePath)) {
981994
return;
@@ -1066,6 +1079,14 @@ private static string ConvertToMayaCompatibleName(string name)
10661079
}
10671080
return newName;
10681081
}
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+
}
10691090
}
10701091
}
10711092
}

0 commit comments

Comments
 (0)