@@ -789,6 +789,42 @@ private Vector3 GetRecenteredTranslation(Transform t, Vector3 center)
789
789
790
790
public enum TransformExportType { Local , Global , Reset } ;
791
791
792
+ protected const string DefaultFileNamePrefix = "_safe_to_delete__" ;
793
+ protected const string DefaultFileNameExt = ".fbx" ;
794
+
795
+ private string MakeFileName ( string baseName = null , string prefixName = null , string extName = null )
796
+ {
797
+ if ( baseName == null )
798
+ baseName = Path . GetRandomFileName ( ) ;
799
+
800
+ if ( prefixName == null )
801
+ prefixName = DefaultFileNamePrefix ;
802
+
803
+ if ( extName == null )
804
+ extName = DefaultFileNameExt ;
805
+
806
+ return prefixName + baseName + extName ;
807
+ }
808
+
809
+ protected string GetRandomFileNamePath ( string pathName , string prefixName = null , string extName = null )
810
+ {
811
+ string temp ;
812
+
813
+ if ( prefixName == null )
814
+ prefixName = DefaultFileNamePrefix ;
815
+
816
+ if ( extName == null )
817
+ extName = DefaultFileNameExt ;
818
+
819
+ // repeat until you find a file that does not already exist
820
+ do {
821
+ temp = Path . Combine ( pathName , MakeFileName ( prefixName : prefixName , extName : extName ) ) ;
822
+
823
+ } while ( File . Exists ( temp ) ) ;
824
+
825
+ return temp ;
826
+ }
827
+
792
828
/// <summary>
793
829
/// Export all the objects in the set.
794
830
/// Return the number of objects in the set that we exported.
@@ -797,6 +833,22 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
797
833
{
798
834
exportCancelled = false ;
799
835
Verbose = true ;
836
+
837
+ // If we are overwriting a file, export first to a temporary file
838
+ // in case the export is cancelled.
839
+ TempFilePath = LastFilePath ;
840
+ if ( File . Exists ( LastFilePath ) ) {
841
+ var directory = string . IsNullOrEmpty ( LastFilePath )
842
+ ? Application . dataPath
843
+ : System . IO . Path . GetDirectoryName ( LastFilePath ) ;
844
+
845
+ TempFilePath = GetRandomFileNamePath ( directory ) ;
846
+ }
847
+
848
+ if ( string . IsNullOrEmpty ( TempFilePath ) ) {
849
+ return 0 ;
850
+ }
851
+
800
852
try {
801
853
// Create the FBX manager
802
854
using ( var fbxManager = FbxManager . Create ( ) ) {
@@ -816,7 +868,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
816
868
int fileFormat = EditorTools . ExportSettings . instance . embedTextures ? - 1 :
817
869
fbxManager . GetIOPluginRegistry ( ) . FindWriterIDByDescription ( "FBX ascii (*.fbx)" ) ;
818
870
819
- bool status = fbxExporter . Initialize ( LastFilePath , fileFormat , fbxManager . GetIOSettings ( ) ) ;
871
+ bool status = fbxExporter . Initialize ( TempFilePath , fileFormat , fbxManager . GetIOSettings ( ) ) ;
820
872
// Check that initialization of the fbxExporter was successful
821
873
if ( ! status )
822
874
return 0 ;
@@ -897,6 +949,8 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
897
949
EditorApplication . update += DeleteFile ;
898
950
return 0 ;
899
951
}
952
+ // delete old file, move temp file
953
+ EditorApplication . update += ReplaceFile ;
900
954
901
955
return status == true ? NumNodes : 0 ;
902
956
}
@@ -927,9 +981,34 @@ static bool ExportProgressCallback (float percentage, string status)
927
981
return ! cancel ;
928
982
}
929
983
984
+ /// <summary>
985
+ /// On Editor update, deletes the file that got created while exporting.
986
+ /// </summary>
930
987
static void DeleteFile ( )
931
988
{
932
- if ( File . Exists ( LastFilePath ) ) {
989
+ if ( File . Exists ( TempFilePath ) ) {
990
+ try {
991
+ File . Delete ( TempFilePath ) ;
992
+ } catch ( IOException ) {
993
+ }
994
+
995
+ if ( File . Exists ( TempFilePath ) ) {
996
+ Debug . LogWarning ( "Failed to delete file: " + TempFilePath ) ;
997
+ }
998
+ } else {
999
+ EditorApplication . update -= DeleteFile ;
1000
+ AssetDatabase . Refresh ( ) ;
1001
+ }
1002
+ }
1003
+
1004
+ /// <summary>
1005
+ /// On Editor update, replaces the file we are overwriting with
1006
+ /// the temp file that was exported to.
1007
+ /// </summary>
1008
+ static void ReplaceFile ( )
1009
+ {
1010
+ if ( ! TempFilePath . Equals ( LastFilePath ) && File . Exists ( TempFilePath ) ) {
1011
+ // delete old file
933
1012
try {
934
1013
File . Delete ( LastFilePath ) ;
935
1014
} catch ( IOException ) {
@@ -938,8 +1017,15 @@ static void DeleteFile ()
938
1017
if ( File . Exists ( LastFilePath ) ) {
939
1018
Debug . LogWarning ( "Failed to delete file: " + LastFilePath ) ;
940
1019
}
1020
+
1021
+ // rename the new file
1022
+ try {
1023
+ File . Move ( TempFilePath , LastFilePath ) ;
1024
+ } catch ( IOException ) {
1025
+ Debug . LogWarning ( string . Format ( "Failed to move file {0} to {1}" , TempFilePath , LastFilePath ) ) ;
1026
+ }
941
1027
} else {
942
- EditorApplication . update -= DeleteFile ;
1028
+ EditorApplication . update -= ReplaceFile ;
943
1029
AssetDatabase . Refresh ( ) ;
944
1030
}
945
1031
}
@@ -1191,6 +1277,7 @@ public void Dispose ()
1191
1277
/// manage the selection of a filename
1192
1278
/// </summary>
1193
1279
static string LastFilePath { get ; set ; }
1280
+ static string TempFilePath { get ; set ; }
1194
1281
1195
1282
const string Extension = "fbx" ;
1196
1283
0 commit comments