@@ -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" ) ;
@@ -136,6 +138,35 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
136
138
return result ;
137
139
}
138
140
141
+ /// <summary>
142
+ /// Enforces that all object names be unique before exporting.
143
+ /// If an object with a duplicate name is found, then it is incremented.
144
+ /// e.g. Sphere becomes Sphere 1
145
+ /// </summary>
146
+ /// <param name="exportSet">Export set.</param>
147
+ private static void EnforceUniqueNames ( GameObject [ ] exportSet )
148
+ {
149
+ Dictionary < string , int > NameToIndexMap = new Dictionary < string , int > ( ) ;
150
+ string format = "{0} {1}" ;
151
+
152
+ Queue < GameObject > queue = new Queue < GameObject > ( exportSet ) ;
153
+
154
+ while ( queue . Count > 0 ) {
155
+ var go = queue . Dequeue ( ) ;
156
+ var name = go . name ;
157
+ if ( NameToIndexMap . ContainsKey ( name ) ) {
158
+ go . name = string . Format ( format , name , NameToIndexMap [ name ] ) ;
159
+ NameToIndexMap [ name ] ++ ;
160
+ } else {
161
+ NameToIndexMap [ name ] = 1 ;
162
+ }
163
+
164
+ foreach ( Transform child in go . transform ) {
165
+ queue . Enqueue ( child . gameObject ) ;
166
+ }
167
+ }
168
+ }
169
+
139
170
private static void SetupImportedGameObject ( GameObject orig , GameObject imported )
140
171
{
141
172
Transform importedTransform = imported . transform ;
@@ -164,9 +195,25 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
164
195
Debug . LogWarning ( string . Format ( "Warning: Exported {0} objects, but only imported {1}" ,
165
196
origTransform . hierarchyCount , importedTransform . hierarchyCount ) ) ;
166
197
}
198
+ FixSiblingOrder ( orig . transform , imported . transform ) ;
167
199
CopyComponentsRecursive ( orig , imported ) ;
168
200
}
169
201
202
+ private static void FixSiblingOrder ( Transform orig , Transform imported ) {
203
+ foreach ( Transform origChild in orig ) {
204
+ Transform importedChild = imported . Find ( origChild . name ) ;
205
+ if ( importedChild == null ) {
206
+ Debug . LogWarning ( string . Format (
207
+ "Warning: Could not find {0} in parented under {1} in import hierarchy" ,
208
+ origChild . name , imported . name
209
+ ) ) ;
210
+ continue ;
211
+ }
212
+ importedChild . SetSiblingIndex ( origChild . GetSiblingIndex ( ) ) ;
213
+ FixSiblingOrder ( origChild , importedChild ) ;
214
+ }
215
+ }
216
+
170
217
private static void CopyComponentsRecursive ( GameObject from , GameObject to ) {
171
218
if ( ! to . name . StartsWith ( from . name ) || from . transform . childCount != to . transform . childCount ) {
172
219
Debug . LogError ( string . Format ( "Error: hierarchies don't match (From: {0}, To: {1})" , from . name , to . name ) ) ;
0 commit comments