@@ -789,42 +789,6 @@ 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
-
828
792
/// <summary>
829
793
/// Export all the objects in the set.
830
794
/// Return the number of objects in the set that we exported.
@@ -834,22 +798,23 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
834
798
exportCancelled = false ;
835
799
Verbose = true ;
836
800
837
- // If we are overwriting a file, export first to a temporary file
801
+ // Export first to a temporary file
838
802
// 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 ) ;
803
+ // This way we won't overwrite existing files.
804
+ try {
805
+ m_tempFilePath = Path . GetTempFileName ( ) ;
806
+ }
807
+ catch ( IOException ) {
808
+ return 0 ;
846
809
}
810
+ m_lastFilePath = LastFilePath ;
847
811
848
- if ( string . IsNullOrEmpty ( TempFilePath ) ) {
812
+ if ( string . IsNullOrEmpty ( m_tempFilePath ) ) {
849
813
return 0 ;
850
814
}
851
815
852
816
try {
817
+ bool status = false ;
853
818
// Create the FBX manager
854
819
using ( var fbxManager = FbxManager . Create ( ) ) {
855
820
// Configure fbx IO settings.
@@ -868,7 +833,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
868
833
int fileFormat = EditorTools . ExportSettings . instance . embedTextures ? - 1 :
869
834
fbxManager . GetIOPluginRegistry ( ) . FindWriterIDByDescription ( "FBX ascii (*.fbx)" ) ;
870
835
871
- bool status = fbxExporter . Initialize ( TempFilePath , fileFormat , fbxManager . GetIOSettings ( ) ) ;
836
+ status = fbxExporter . Initialize ( m_tempFilePath , fileFormat , fbxManager . GetIOSettings ( ) ) ;
872
837
// Check that initialization of the fbxExporter was successful
873
838
if ( ! status )
874
839
return 0 ;
@@ -916,7 +881,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
916
881
exportProgress = this . ExportComponents (
917
882
unityGo , fbxScene , fbxRootNode , exportProgress ,
918
883
count , Vector3 . zero , TransformExportType . Reset ) ;
919
- if ( exportProgress < 0 ) {
884
+ if ( exportCancelled || exportProgress < 0 ) {
920
885
Debug . LogWarning ( "Export Cancelled" ) ;
921
886
return 0 ;
922
887
}
@@ -929,7 +894,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
929
894
foreach ( var unityGo in revisedExportSet ) {
930
895
exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode ,
931
896
exportProgress , count , center , TransformExportType . Global ) ;
932
- if ( exportProgress < 0 ) {
897
+ if ( exportCancelled || exportProgress < 0 ) {
933
898
Debug . LogWarning ( "Export Cancelled" ) ;
934
899
return 0 ;
935
900
}
@@ -942,23 +907,27 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
942
907
// cleanup
943
908
fbxScene . Destroy ( ) ;
944
909
fbxExporter . Destroy ( ) ;
910
+ }
945
911
946
- if ( exportCancelled ) {
947
- Debug . LogWarning ( "Export Cancelled" ) ;
948
- // delete the file that got created
949
- EditorApplication . update += DeleteFile ;
950
- return 0 ;
951
- }
952
- // delete old file, move temp file
953
- EditorApplication . update += ReplaceFile ;
954
-
955
- return status == true ? NumNodes : 0 ;
912
+ if ( exportCancelled ) {
913
+ Debug . LogWarning ( "Export Cancelled" ) ;
914
+ return 0 ;
956
915
}
957
- } finally {
916
+ // delete old file, move temp file
917
+ ReplaceFile ( ) ;
918
+ AssetDatabase . Refresh ( ) ;
919
+
920
+ return status == true ? NumNodes : 0 ;
921
+ }
922
+ finally {
958
923
// You must clear the progress bar when you're done,
959
924
// otherwise it never goes away and many actions in Unity
960
925
// are blocked (e.g. you can't quit).
961
926
EditorUtility . ClearProgressBar ( ) ;
927
+
928
+ // make sure the temp file is deleted, no matter
929
+ // when we return
930
+ DeleteTempFile ( ) ;
962
931
}
963
932
}
964
933
@@ -982,51 +951,49 @@ static bool ExportProgressCallback (float percentage, string status)
982
951
}
983
952
984
953
/// <summary>
985
- /// On Editor update, deletes the file that got created while exporting.
954
+ /// Deletes the file that got created while exporting.
986
955
/// </summary>
987
- static void DeleteFile ( )
956
+ private void DeleteTempFile ( )
988
957
{
989
- if ( File . Exists ( TempFilePath ) ) {
990
- try {
991
- File . Delete ( TempFilePath ) ;
992
- } catch ( IOException ) {
993
- }
958
+ if ( ! File . Exists ( m_tempFilePath ) ) {
959
+ return ;
960
+ }
994
961
995
- if ( File . Exists ( TempFilePath ) ) {
996
- Debug . LogWarning ( "Failed to delete file: " + TempFilePath ) ;
997
- }
998
- } else {
999
- EditorApplication . update -= DeleteFile ;
1000
- AssetDatabase . Refresh ( ) ;
962
+ try {
963
+ File . Delete ( m_tempFilePath ) ;
964
+ } catch ( IOException ) {
965
+ }
966
+
967
+ if ( File . Exists ( m_tempFilePath ) ) {
968
+ Debug . LogWarning ( "Failed to delete file: " + m_tempFilePath ) ;
969
+ EditorApplication . update -= DeleteTempFile ;
1001
970
}
1002
971
}
1003
972
1004
973
/// <summary>
1005
- /// On Editor update, replaces the file we are overwriting with
974
+ /// Replaces the file we are overwriting with
1006
975
/// the temp file that was exported to.
1007
976
/// </summary>
1008
- static void ReplaceFile ( )
977
+ private void ReplaceFile ( )
1009
978
{
1010
- if ( ! TempFilePath . Equals ( LastFilePath ) && File . Exists ( TempFilePath ) ) {
1011
- // delete old file
1012
- try {
1013
- File . Delete ( LastFilePath ) ;
1014
- } catch ( IOException ) {
1015
- }
979
+ if ( m_tempFilePath . Equals ( m_lastFilePath ) || ! File . Exists ( m_tempFilePath ) ) {
980
+ return ;
981
+ }
982
+ // delete old file
983
+ try {
984
+ File . Delete ( m_lastFilePath ) ;
985
+ } catch ( IOException ) {
986
+ }
1016
987
1017
- if ( File . Exists ( LastFilePath ) ) {
1018
- Debug . LogWarning ( "Failed to delete file: " + LastFilePath ) ;
1019
- }
988
+ if ( File . Exists ( m_lastFilePath ) ) {
989
+ Debug . LogWarning ( "Failed to delete file: " + m_lastFilePath ) ;
990
+ }
1020
991
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
- }
1027
- } else {
1028
- EditorApplication . update -= ReplaceFile ;
1029
- AssetDatabase . Refresh ( ) ;
992
+ // rename the new file
993
+ try {
994
+ File . Move ( m_tempFilePath , m_lastFilePath ) ;
995
+ } catch ( IOException ) {
996
+ Debug . LogWarning ( string . Format ( "Failed to move file {0} to {1}" , m_tempFilePath , m_lastFilePath ) ) ;
1030
997
}
1031
998
}
1032
999
@@ -1277,7 +1244,8 @@ public void Dispose ()
1277
1244
/// manage the selection of a filename
1278
1245
/// </summary>
1279
1246
static string LastFilePath { get ; set ; }
1280
- static string TempFilePath { get ; set ; }
1247
+ private string m_tempFilePath { get ; set ; }
1248
+ private string m_lastFilePath { get ; set ; }
1281
1249
1282
1250
const string Extension = "fbx" ;
1283
1251
0 commit comments