@@ -76,7 +76,11 @@ 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
- filePaths [ n ] = Path . Combine ( dirPath , gosToExport [ n ] . name + ".fbx" ) ;
79
+ var filename = gosToExport [ n ] . name + ".fbx" ;
80
+ if ( File . Exists ( Path . Combine ( dirPath , filename ) ) ) {
81
+ filename = IncrementFileName ( dirPath , filename ) ;
82
+ }
83
+ filePaths [ n ] = Path . Combine ( dirPath , filename ) ;
80
84
}
81
85
82
86
string [ ] fbxFileNames = new string [ filePaths . Length ] ;
@@ -136,6 +140,37 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
136
140
return result ;
137
141
}
138
142
143
+ /// <summary>
144
+ /// Check if the file exists, and if it does, then increment the name.
145
+ /// e.g. if filename is Sphere.fbx and it already exists, change it to Sphere1.fbx.
146
+ /// </summary>
147
+ /// <returns>new file name.</returns>
148
+ /// <param name="filename">Filename.</param>
149
+ private static string IncrementFileName ( string path , string filename )
150
+ {
151
+ string fileWithoutExt = Path . GetFileNameWithoutExtension ( filename ) ;
152
+ string ext = Path . GetExtension ( filename ) ;
153
+ string pattern = string . Format ( @"{0}{1}{2}" , fileWithoutExt , "*" , ext ) ;
154
+ string [ ] files = Directory . GetFiles ( path , pattern , SearchOption . TopDirectoryOnly ) ;
155
+
156
+ int index = 0 ;
157
+ string groupName = "index" ;
158
+ pattern = string . Format ( @"{0}{1}\{2}" , fileWithoutExt , "[ ]*(?<" + groupName + ">[0-9]*?)" , ext ) ;
159
+ foreach ( var file in files ) {
160
+ var match = System . Text . RegularExpressions . Regex . Match ( file , pattern ) ;
161
+ if ( match . Success ) {
162
+ string indexMatch = match . Groups [ groupName ] . Value ;
163
+ if ( ! string . IsNullOrEmpty ( indexMatch ) ) {
164
+ int i = - 1 ;
165
+ if ( int . TryParse ( indexMatch , out i ) && i > index ) {
166
+ index = i ;
167
+ }
168
+ }
169
+ }
170
+ }
171
+ return string . Format ( "{0} {1}{2}" , fileWithoutExt , ( index + 1 ) , ext ) ;
172
+ }
173
+
139
174
private static void SetupImportedGameObject ( GameObject orig , GameObject imported )
140
175
{
141
176
Transform importedTransform = imported . transform ;
0 commit comments