@@ -907,7 +907,24 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
907
907
{
908
908
exportCancelled = false ;
909
909
Verbose = true ;
910
+
911
+ // Export first to a temporary file
912
+ // in case the export is cancelled.
913
+ // This way we won't overwrite existing files.
914
+ try {
915
+ m_tempFilePath = Path . GetTempFileName ( ) ;
916
+ }
917
+ catch ( IOException ) {
918
+ return 0 ;
919
+ }
920
+ m_lastFilePath = LastFilePath ;
921
+
922
+ if ( string . IsNullOrEmpty ( m_tempFilePath ) ) {
923
+ return 0 ;
924
+ }
925
+
910
926
try {
927
+ bool status = false ;
911
928
// Create the FBX manager
912
929
using ( var fbxManager = FbxManager . Create ( ) ) {
913
930
// Configure fbx IO settings.
@@ -926,7 +943,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
926
943
int fileFormat = EditorTools . ExportSettings . instance . embedTextures ? - 1 :
927
944
fbxManager . GetIOPluginRegistry ( ) . FindWriterIDByDescription ( "FBX ascii (*.fbx)" ) ;
928
945
929
- bool status = fbxExporter . Initialize ( LastFilePath , fileFormat , fbxManager . GetIOSettings ( ) ) ;
946
+ status = fbxExporter . Initialize ( m_tempFilePath , fileFormat , fbxManager . GetIOSettings ( ) ) ;
930
947
// Check that initialization of the fbxExporter was successful
931
948
if ( ! status )
932
949
return 0 ;
@@ -983,7 +1000,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
983
1000
exportProgress = this . ExportComponents (
984
1001
unityGo , fbxScene , fbxRootNode , exportProgress ,
985
1002
count , Vector3 . zero , TransformExportType . Reset ) ;
986
- if ( exportProgress < 0 ) {
1003
+ if ( exportCancelled || exportProgress < 0 ) {
987
1004
Debug . LogWarning ( "Export Cancelled" ) ;
988
1005
return 0 ;
989
1006
}
@@ -996,7 +1013,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
996
1013
foreach ( var unityGo in revisedExportSet ) {
997
1014
exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode ,
998
1015
exportProgress , count , center , TransformExportType . Global ) ;
999
- if ( exportProgress < 0 ) {
1016
+ if ( exportCancelled || exportProgress < 0 ) {
1000
1017
Debug . LogWarning ( "Export Cancelled" ) ;
1001
1018
return 0 ;
1002
1019
}
@@ -1008,21 +1025,27 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
1008
1025
// cleanup
1009
1026
fbxScene . Destroy ( ) ;
1010
1027
fbxExporter . Destroy ( ) ;
1028
+ }
1011
1029
1012
- if ( exportCancelled ) {
1013
- Debug . LogWarning ( "Export Cancelled" ) ;
1014
- // delete the file that got created
1015
- EditorApplication . update += DeleteFile ;
1016
- return 0 ;
1017
- }
1018
-
1019
- return status == true ? NumNodes : 0 ;
1030
+ if ( exportCancelled ) {
1031
+ Debug . LogWarning ( "Export Cancelled" ) ;
1032
+ return 0 ;
1020
1033
}
1021
- } finally {
1034
+ // delete old file, move temp file
1035
+ ReplaceFile ( ) ;
1036
+ AssetDatabase . Refresh ( ) ;
1037
+
1038
+ return status == true ? NumNodes : 0 ;
1039
+ }
1040
+ finally {
1022
1041
// You must clear the progress bar when you're done,
1023
1042
// otherwise it never goes away and many actions in Unity
1024
1043
// are blocked (e.g. you can't quit).
1025
1044
EditorUtility . ClearProgressBar ( ) ;
1045
+
1046
+ // make sure the temp file is deleted, no matter
1047
+ // when we return
1048
+ DeleteTempFile ( ) ;
1026
1049
}
1027
1050
}
1028
1051
@@ -1045,20 +1068,49 @@ static bool ExportProgressCallback (float percentage, string status)
1045
1068
return ! cancel ;
1046
1069
}
1047
1070
1048
- static void DeleteFile ( )
1071
+ /// <summary>
1072
+ /// Deletes the file that got created while exporting.
1073
+ /// </summary>
1074
+ private void DeleteTempFile ( )
1049
1075
{
1050
- if ( File . Exists ( LastFilePath ) ) {
1051
- try {
1052
- File . Delete ( LastFilePath ) ;
1053
- } catch ( IOException ) {
1054
- }
1076
+ if ( ! File . Exists ( m_tempFilePath ) ) {
1077
+ return ;
1078
+ }
1055
1079
1056
- if ( File . Exists ( LastFilePath ) ) {
1057
- Debug . LogWarning ( "Failed to delete file: " + LastFilePath ) ;
1058
- }
1059
- } else {
1060
- EditorApplication . update -= DeleteFile ;
1061
- AssetDatabase . Refresh ( ) ;
1080
+ try {
1081
+ File . Delete ( m_tempFilePath ) ;
1082
+ } catch ( IOException ) {
1083
+ }
1084
+
1085
+ if ( File . Exists ( m_tempFilePath ) ) {
1086
+ Debug . LogWarning ( "Failed to delete file: " + m_tempFilePath ) ;
1087
+ }
1088
+ }
1089
+
1090
+ /// <summary>
1091
+ /// Replaces the file we are overwriting with
1092
+ /// the temp file that was exported to.
1093
+ /// </summary>
1094
+ private void ReplaceFile ( )
1095
+ {
1096
+ if ( m_tempFilePath . Equals ( m_lastFilePath ) || ! File . Exists ( m_tempFilePath ) ) {
1097
+ return ;
1098
+ }
1099
+ // delete old file
1100
+ try {
1101
+ File . Delete ( m_lastFilePath ) ;
1102
+ } catch ( IOException ) {
1103
+ }
1104
+
1105
+ if ( File . Exists ( m_lastFilePath ) ) {
1106
+ Debug . LogWarning ( "Failed to delete file: " + m_lastFilePath ) ;
1107
+ }
1108
+
1109
+ // rename the new file
1110
+ try {
1111
+ File . Move ( m_tempFilePath , m_lastFilePath ) ;
1112
+ } catch ( IOException ) {
1113
+ Debug . LogWarning ( string . Format ( "Failed to move file {0} to {1}" , m_tempFilePath , m_lastFilePath ) ) ;
1062
1114
}
1063
1115
}
1064
1116
@@ -1356,6 +1408,8 @@ public void Dispose ()
1356
1408
/// manage the selection of a filename
1357
1409
/// </summary>
1358
1410
static string LastFilePath { get ; set ; }
1411
+ private string m_tempFilePath { get ; set ; }
1412
+ private string m_lastFilePath { get ; set ; }
1359
1413
1360
1414
const string Extension = "fbx" ;
1361
1415
0 commit comments