@@ -76,8 +76,12 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
76
76
string dirPath = Path . Combine ( Application . dataPath , "Objects" ) ;
77
77
78
78
for ( int n = 0 ; n < gosToExport . Length ; n ++ ) {
79
- string filename = ModelExporter . ConvertToValidFilename ( gosToExport [ n ] . name + ".fbx" ) ;
80
- filePaths [ n ] = Path . Combine ( dirPath , filename ) ;
79
+ var filename = ModelExporter . ConvertToValidFilename ( gosToExport [ n ] . name + ".fbx" ) ;
80
+ var filePath = Path . Combine ( dirPath , filename ) ;
81
+ if ( File . Exists ( filePath ) ) {
82
+ filePath = IncrementFileName ( dirPath , filename ) ;
83
+ }
84
+ filePaths [ n ] = filePath ;
81
85
}
82
86
83
87
string [ ] fbxFileNames = new string [ filePaths . Length ] ;
@@ -137,6 +141,28 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
137
141
return result ;
138
142
}
139
143
144
+ /// <summary>
145
+ /// Check if the file exists, and if it does, then increment the name.
146
+ /// e.g. if filename is Sphere.fbx and it already exists, change it to Sphere 1.fbx.
147
+ /// </summary>
148
+ /// <returns>new file name.</returns>
149
+ /// <param name="filename">Filename.</param>
150
+ private static string IncrementFileName ( string path , string filename )
151
+ {
152
+ string fileWithoutExt = Path . GetFileNameWithoutExtension ( filename ) ;
153
+ string ext = Path . GetExtension ( filename ) ;
154
+
155
+ int index = 1 ;
156
+ string file = null ;
157
+ do {
158
+ file = string . Format ( "{0} {1}{2}" , fileWithoutExt , index , ext ) ;
159
+ file = Path . Combine ( path , file ) ;
160
+ index ++ ;
161
+ } while ( File . Exists ( file ) ) ;
162
+
163
+ return file ;
164
+ }
165
+
140
166
private static void SetupImportedGameObject ( GameObject orig , GameObject imported )
141
167
{
142
168
Transform importedTransform = imported . transform ;
0 commit comments