@@ -57,25 +57,40 @@ private static List<GameObject> OnConvertInPlace ()
57
57
58
58
GameObject [ ] unityActiveGOs = Selection . GetFiltered < GameObject > ( SelectionMode . Editable | SelectionMode . TopLevel ) ;
59
59
60
+ var exportSet = ModelExporter . RemoveDuplicateObjects ( unityActiveGOs ) ;
61
+ GameObject [ ] gosToExport = new GameObject [ exportSet . Count ] ;
62
+ exportSet . CopyTo ( gosToExport ) ;
63
+
60
64
// find common ancestor root & filePath;
61
- string filePath = "" ;
65
+ string [ ] filePaths = new string [ gosToExport . Length ] ;
62
66
string dirPath = Path . Combine ( Application . dataPath , "Objects" ) ;
63
67
64
- GameObject unityCommonAncestor = null ;
65
- int siblingIndex = - 1 ;
66
-
67
- foreach ( GameObject goObj in unityActiveGOs ) {
68
- siblingIndex = goObj . transform . GetSiblingIndex ( ) ;
69
- unityCommonAncestor = ( goObj . transform . parent != null ) ? goObj . transform . parent . gameObject : null ;
70
- filePath = Path . Combine ( dirPath , goObj . name + ".fbx" ) ;
68
+ Transform [ ] unityCommonAncestors = new Transform [ gosToExport . Length ] ;
69
+ int [ ] siblingIndices = new int [ gosToExport . Length ] ;
71
70
72
- break ;
71
+ for ( int n = 0 ; n < gosToExport . Length ; n ++ ) {
72
+ GameObject goObj = gosToExport [ n ] ;
73
+ unityCommonAncestors [ n ] = goObj . transform . parent ;
74
+ siblingIndices [ n ] = goObj . transform . GetSiblingIndex ( ) ;
75
+ filePaths [ n ] = Path . Combine ( dirPath , goObj . name + ".fbx" ) ;
73
76
}
74
77
75
- string fbxFileName = FbxExporters . Editor . ModelExporter . ExportObjects ( filePath , unityActiveGOs ) as string ;
78
+ string [ ] fbxFileNames = new string [ filePaths . Length ] ;
79
+
80
+ for ( int j = 0 ; j < gosToExport . Length ; j ++ ) {
81
+ fbxFileNames [ j ] = FbxExporters . Editor . ModelExporter . ExportObjects ( filePaths [ j ] ,
82
+ new UnityEngine . Object [ ] { gosToExport [ j ] } ) as string ;
83
+ }
76
84
77
- if ( fbxFileName != null )
85
+ List < GameObject > selection = new List < GameObject > ( ) ;
86
+ for ( int i = 0 ; i < fbxFileNames . Length ; i ++ )
78
87
{
88
+ var fbxFileName = fbxFileNames [ i ] ;
89
+ if ( fbxFileName == null ) {
90
+ Debug . Log ( string . Format ( "Warning: Export failed for GameObject {0}" , gosToExport [ i ] . name ) ) ;
91
+ continue ;
92
+ }
93
+
79
94
// make filepath relative to project folder
80
95
if ( fbxFileName . StartsWith ( Application . dataPath , System . StringComparison . CurrentCulture ) )
81
96
{
@@ -100,33 +115,29 @@ private static List<GameObject> OnConvertInPlace ()
100
115
unityGO . name = unityMainAsset . name ;
101
116
102
117
// configure transform and maintain local pose
103
- if ( unityCommonAncestor != null ) {
104
- unityGO . transform . SetParent ( unityCommonAncestor . transform , false ) ;
105
- }
118
+ unityGO . transform . SetParent ( unityCommonAncestors [ i ] , false ) ;
106
119
107
- unityGO . transform . SetSiblingIndex ( siblingIndex ) ;
120
+ unityGO . transform . SetSiblingIndex ( siblingIndices [ i ] ) ;
108
121
109
122
result . Add ( unityObj as GameObject ) ;
110
123
111
- // remove (now redundant) gameobjects
112
- for ( int i = 0 ; i < unityActiveGOs . Length ; i ++ ) {
124
+ // remove (now redundant) gameobject
113
125
#if UNI_19965
114
- Object . DestroyImmediate ( unityActiveGOs [ i ] ) ;
126
+ Object . DestroyImmediate ( unityActiveGOs [ i ] ) ;
115
127
#else
116
- // rename and put under scene root in case we need to check values
117
- unityActiveGOs [ i ] . name = "_safe_to_delete_" + unityActiveGOs [ i ] . name ;
118
- unityActiveGOs [ i ] . transform . parent = null ;
119
- unityActiveGOs [ i ] . SetActive ( false ) ;
128
+ // rename and put under scene root in case we need to check values
129
+ gosToExport [ i ] . name = "_safe_to_delete_" + gosToExport [ i ] . name ;
130
+ gosToExport [ i ] . SetActive ( false ) ;
120
131
#endif
121
- }
122
-
123
132
// select the instanced Model Prefab
124
- Selection . objects = new GameObject [ ] { unityGO } ;
133
+ selection . Add ( unityGO ) ;
125
134
}
126
135
}
127
136
128
137
}
129
138
139
+ Selection . objects = selection . ToArray ( ) ;
140
+
130
141
return result ;
131
142
}
132
143
}
0 commit comments