Skip to content

Commit 5f4dfa0

Browse files
authored
Merge pull request #33 from Unity-Technologies/UNI-21414-convert-overwrites-file
Uni 21414 fix so convert doesn't overwrite file
2 parents f812b0b + dccbe18 commit 5f4dfa0

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
7676
string dirPath = Path.Combine (Application.dataPath, "Objects");
7777

7878
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;
8185
}
8286

8387
string[] fbxFileNames = new string[filePaths.Length];
@@ -137,6 +141,28 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
137141
return result;
138142
}
139143

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+
140166
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
141167
{
142168
Transform importedTransform = imported.transform;

0 commit comments

Comments
 (0)