Skip to content

Commit fb62200

Browse files
committed
simplify solution
this also makes sure that we don't skip over any available numbers
1 parent e6a5fc3 commit fb62200

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
7777

7878
for(int n = 0; n < gosToExport.Length; n++){
7979
var filename = gosToExport [n].name + ".fbx";
80-
if (File.Exists (Path.Combine(dirPath, filename))) {
81-
filename = IncrementFileName (dirPath, filename);
80+
var filePath = Path.Combine (dirPath, filename);
81+
if (File.Exists (filePath)) {
82+
filePath = IncrementFileName (dirPath, filename);
8283
}
83-
filePaths[n] = Path.Combine (dirPath, filename);
84+
filePaths[n] = filePath;
8485
}
8586

8687
string[] fbxFileNames = new string[filePaths.Length];
@@ -142,33 +143,24 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
142143

143144
/// <summary>
144145
/// 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+
/// e.g. if filename is Sphere.fbx and it already exists, change it to Sphere 1.fbx.
146147
/// </summary>
147148
/// <returns>new file name.</returns>
148149
/// <param name="filename">Filename.</param>
149150
private static string IncrementFileName(string path, string filename)
150151
{
151152
string fileWithoutExt = Path.GetFileNameWithoutExtension (filename);
152153
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);
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;
172164
}
173165

174166
private static void SetupImportedGameObject(GameObject orig, GameObject imported)

0 commit comments

Comments
 (0)