@@ -57,6 +57,11 @@ private static List<GameObject> OnConvertInPlace ()
57
57
58
58
GameObject [ ] unityActiveGOs = Selection . GetFiltered < GameObject > ( SelectionMode . Editable | SelectionMode . TopLevel ) ;
59
59
60
+ // Ensure that the root GameObjects retain their relative order after export
61
+ System . Array . Sort ( unityActiveGOs , delegate ( GameObject x , GameObject y ) {
62
+ return x . transform . GetSiblingIndex ( ) . CompareTo ( y . transform . GetSiblingIndex ( ) ) ;
63
+ } ) ;
64
+
60
65
// find common ancestor root & filePath;
61
66
string filePath = "" ;
62
67
string dirPath = Path . Combine ( Application . dataPath , "Objects" ) ;
@@ -109,6 +114,19 @@ private static List<GameObject> OnConvertInPlace ()
109
114
110
115
unityGO . transform . SetSiblingIndex ( siblingIndex ) ;
111
116
117
+ // copy the components over, assuming that the hierarchy order is unchanged
118
+ if ( unityActiveGOs . Length == 1 ) {
119
+ CopyComponentsRecursive ( unityActiveGOs [ 0 ] , unityGO ) ;
120
+ } else {
121
+ if ( unityActiveGOs . Length != unityGO . transform . childCount ) {
122
+ Debug . LogWarning ( string . Format ( "Warning: Exported {0} objects, but only imported {1}" ,
123
+ unityActiveGOs . Length , unityGO . transform . childCount ) ) ;
124
+ }
125
+ for ( int i = 0 ; i < unityGO . transform . childCount ; i ++ ) {
126
+ CopyComponentsRecursive ( unityActiveGOs [ i ] , unityGO . transform . GetChild ( i ) . gameObject ) ;
127
+ }
128
+ }
129
+
112
130
result . Add ( unityObj as GameObject ) ;
113
131
114
132
// remove (now redundant) gameobjects
@@ -132,6 +150,36 @@ private static List<GameObject> OnConvertInPlace ()
132
150
133
151
return result ;
134
152
}
153
+
154
+ private static void CopyComponentsRecursive ( GameObject from , GameObject to ) {
155
+ if ( ! to . name . StartsWith ( from . name ) || from . transform . childCount != to . transform . childCount ) {
156
+ Debug . LogError ( string . Format ( "Error: hierarchies don't match (From: {0}, To: {1})" , from . name , to . name ) ) ;
157
+ return ;
158
+ }
159
+
160
+ CopyComponents ( from , to ) ;
161
+ for ( int i = 0 ; i < from . transform . childCount ; i ++ ) {
162
+ CopyComponentsRecursive ( from . transform . GetChild ( i ) . gameObject , to . transform . GetChild ( i ) . gameObject ) ;
163
+ }
164
+ }
165
+
166
+ private static void CopyComponents ( GameObject from , GameObject to ) {
167
+ var components = from . GetComponents < Component > ( ) ;
168
+ for ( int i = 0 ; i < components . Length ; i ++ ) {
169
+ // if to already has this component, then skip it
170
+ if ( to . GetComponent ( components [ i ] . GetType ( ) ) != null ) {
171
+ continue ;
172
+ }
173
+ bool success = UnityEditorInternal . ComponentUtility . CopyComponent ( components [ i ] ) ;
174
+ if ( success ) {
175
+ success = UnityEditorInternal . ComponentUtility . PasteComponentAsNew ( to ) ;
176
+ }
177
+ if ( ! success ) {
178
+ Debug . LogWarning ( string . Format ( "Warning: Failed to copy component {0} from {1} to {2}" ,
179
+ components [ i ] . GetType ( ) . Name , from . name , to . name ) ) ;
180
+ }
181
+ }
182
+ }
135
183
}
136
184
}
137
185
}
0 commit comments