@@ -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
{
@@ -96,19 +111,14 @@ private static List<GameObject> OnConvertInPlace ()
96
111
GameObject unityGO = unityObj as GameObject ;
97
112
Transform unityGOTransform = unityGO . transform ;
98
113
99
- // configure name
100
- const string cloneSuffix = "(Clone)" ;
101
-
102
- if ( unityGO . name . EndsWith ( cloneSuffix , System . StringComparison . CurrentCulture ) ) {
103
- unityGO . name = unityGO . name . Remove ( cloneSuffix . Length - 1 ) ;
104
- }
114
+ // Set the name to be the name of the instantiated asset.
115
+ // This will get rid of the "(Clone)" if it's added
116
+ unityGO . name = unityMainAsset . name ;
105
117
106
118
// configure transform and maintain local pose
107
- if ( unityCommonAncestor != null ) {
108
- unityGOTransform . SetParent ( unityCommonAncestor . transform , false ) ;
109
- }
119
+ unityGO . transform . SetParent ( unityCommonAncestors [ i ] , false ) ;
110
120
111
- unityGOTransform . SetSiblingIndex ( siblingIndex ) ;
121
+ unityGO . transform . SetSiblingIndex ( siblingIndices [ i ] ) ;
112
122
113
123
// copy the components over, assuming that the hierarchy order is unchanged
114
124
if ( unityActiveGOs . Length == 1 ) {
@@ -125,25 +135,23 @@ private static List<GameObject> OnConvertInPlace ()
125
135
126
136
result . Add ( unityObj as GameObject ) ;
127
137
128
- // remove (now redundant) gameobjects
129
- for ( int i = 0 ; i < unityActiveGOs . Length ; i ++ ) {
138
+ // remove (now redundant) gameobject
130
139
#if UNI_19965
131
- Object . DestroyImmediate ( unityActiveGOs [ i ] ) ;
140
+ Object . DestroyImmediate ( unityActiveGOs [ i ] ) ;
132
141
#else
133
- // rename and put under scene root in case we need to check values
134
- unityActiveGOs [ i ] . name = "_safe_to_delete_" + unityActiveGOs [ i ] . name ;
135
- unityActiveGOs [ i ] . transform . parent = null ;
136
- unityActiveGOs [ i ] . SetActive ( false ) ;
142
+ // rename and put under scene root in case we need to check values
143
+ gosToExport [ i ] . name = "_safe_to_delete_" + gosToExport [ i ] . name ;
144
+ gosToExport [ i ] . SetActive ( false ) ;
137
145
#endif
138
- }
139
-
140
146
// select the instanced Model Prefab
141
- Selection . objects = new GameObject [ ] { unityGO } ;
147
+ selection . Add ( unityGO ) ;
142
148
}
143
149
}
144
150
145
151
}
146
152
153
+ Selection . objects = selection . ToArray ( ) ;
154
+
147
155
return result ;
148
156
}
149
157
0 commit comments