@@ -71,6 +71,8 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
71
71
GameObject [ ] gosToExport = new GameObject [ exportSet . Count ] ;
72
72
exportSet . CopyTo ( gosToExport ) ;
73
73
74
+ EnforceUniqueNames ( gosToExport ) ;
75
+
74
76
// find common ancestor root & filePath;
75
77
string [ ] filePaths = new string [ gosToExport . Length ] ;
76
78
string dirPath = Path . Combine ( Application . dataPath , "Objects" ) ;
@@ -163,6 +165,44 @@ private static string IncrementFileName(string path, string filename)
163
165
return file ;
164
166
}
165
167
168
+ /// <summary>
169
+ /// Enforces that all object names be unique before exporting.
170
+ /// If an object with a duplicate name is found, then it is incremented.
171
+ /// e.g. Sphere becomes Sphere 1
172
+ /// </summary>
173
+ /// <param name="exportSet">Export set.</param>
174
+ private static void EnforceUniqueNames ( GameObject [ ] exportSet )
175
+ {
176
+ Dictionary < string , int > NameToIndexMap = new Dictionary < string , int > ( ) ;
177
+ string format = "{0} {1}" ;
178
+
179
+ Queue < GameObject > queue = new Queue < GameObject > ( exportSet ) ;
180
+
181
+ while ( queue . Count > 0 ) {
182
+ var go = queue . Dequeue ( ) ;
183
+ var name = go . name ;
184
+ if ( NameToIndexMap . ContainsKey ( name ) ) {
185
+ go . name = string . Format ( format , name , NameToIndexMap [ name ] ) ;
186
+ NameToIndexMap [ name ] ++ ;
187
+ } else {
188
+ NameToIndexMap [ name ] = 1 ;
189
+ }
190
+
191
+ foreach ( Transform child in go . transform ) {
192
+ queue . Enqueue ( child . gameObject ) ;
193
+ }
194
+ }
195
+ }
196
+
197
+ /// <summary>
198
+ /// Sets up the imported GameObject to match the original.
199
+ /// - Updates the name to be the same as original (i.e. remove the "(Clone)")
200
+ /// - Moves the imported object to the correct position in the hierarchy
201
+ /// - Updates the transform of the imported GameObject to match the original
202
+ /// - Copy over missing components and component values
203
+ /// </summary>
204
+ /// <param name="orig">Original GameObject.</param>
205
+ /// <param name="imported">Imported GameObject.</param>
166
206
private static void SetupImportedGameObject ( GameObject orig , GameObject imported )
167
207
{
168
208
Transform importedTransform = imported . transform ;
@@ -191,9 +231,25 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
191
231
Debug . LogWarning ( string . Format ( "Warning: Exported {0} objects, but only imported {1}" ,
192
232
origTransform . hierarchyCount , importedTransform . hierarchyCount ) ) ;
193
233
}
234
+ FixSiblingOrder ( orig . transform , imported . transform ) ;
194
235
CopyComponentsRecursive ( orig , imported ) ;
195
236
}
196
237
238
+ private static void FixSiblingOrder ( Transform orig , Transform imported ) {
239
+ foreach ( Transform origChild in orig ) {
240
+ Transform importedChild = imported . Find ( origChild . name ) ;
241
+ if ( importedChild == null ) {
242
+ Debug . LogWarning ( string . Format (
243
+ "Warning: Could not find {0} in parented under {1} in import hierarchy" ,
244
+ origChild . name , imported . name
245
+ ) ) ;
246
+ continue ;
247
+ }
248
+ importedChild . SetSiblingIndex ( origChild . GetSiblingIndex ( ) ) ;
249
+ FixSiblingOrder ( origChild , importedChild ) ;
250
+ }
251
+ }
252
+
197
253
private static void CopyComponentsRecursive ( GameObject from , GameObject to ) {
198
254
if ( ! to . name . StartsWith ( from . name ) || from . transform . childCount != to . transform . childCount ) {
199
255
Debug . LogError ( string . Format ( "Error: hierarchies don't match (From: {0}, To: {1})" , from . name , to . name ) ) ;
0 commit comments