@@ -299,58 +299,52 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
299
299
300
300
public static void CopyComponents ( GameObject from , GameObject to ) {
301
301
var originalComponents = new List < Component > ( to . GetComponents < Component > ( ) ) ;
302
- var components = from . GetComponents < Component > ( ) ;
303
- for ( int i = 0 , n = components . Length ; i < n ; ++ i ) {
304
- if ( ! components [ i ] ) {
305
- continue ;
306
- }
307
-
308
- var json = EditorJsonUtility . ToJson ( components [ i ] ) ;
309
- {
310
- bool foundComponentOfType = false ;
311
- for ( int j = 0 ; j < originalComponents . Count ; j ++ ) {
312
- var toComponent = originalComponents [ j ] ;
313
- // If component already exists, paste values.
314
- if ( toComponent . GetType ( ) == components [ i ] . GetType ( ) ) {
315
- // we have found the component we are looking for, remove it so
316
- // we don't try to copy to it again
317
- originalComponents . RemoveAt ( j ) ;
318
- foundComponentOfType = true ;
319
-
320
- // Don't want to copy MeshFilter because then
321
- // we will replace the exported mesh with the
322
- // old mesh. TODO: just remove some entries
323
- // from the json.
324
- if ( toComponent is MeshFilter ) {
325
- break ;
326
- } else if ( toComponent is SkinnedMeshRenderer ) {
327
- // Don't want to copy materials over in
328
- // case the materials are embedded in
329
- // another model.
330
- var skinnedMesh = toComponent as SkinnedMeshRenderer ;
331
- var sharedMesh = skinnedMesh . sharedMesh ;
332
- var sharedMats = skinnedMesh . sharedMaterials ;
333
- EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
334
- skinnedMesh . sharedMesh = sharedMesh ;
335
- skinnedMesh . sharedMaterials = sharedMats ;
336
- } else if ( toComponent is Renderer ) {
337
- // Don't want to copy materials over in
338
- // case the materials are embedded in
339
- // another model.
340
- var renderer = toComponent as Renderer ;
341
- var sharedMats = renderer . sharedMaterials ;
342
- EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
343
- renderer . sharedMaterials = sharedMats ;
344
- } else {
345
- EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
346
- }
347
- break ;
348
- }
302
+ foreach ( var component in from . GetComponents < Component > ( ) ) {
303
+ var json = EditorJsonUtility . ToJson ( component ) ;
304
+
305
+ System . Type expectedType = component . GetType ( ) ;
306
+ Component toComponent = null ;
307
+
308
+ // Find the component to copy to.
309
+ for ( int i = 0 , n = originalComponents . Count ; i < n ; i ++ ) {
310
+ if ( originalComponents [ i ] . GetType ( ) == expectedType ) {
311
+ // We have found the component we are looking for,
312
+ // remove it so we don't try to copy to it again
313
+ toComponent = originalComponents [ i ] ;
314
+ originalComponents . RemoveAt ( i ) ;
315
+ break ;
349
316
}
317
+ }
350
318
351
- // component was not part of the original components, so add it
352
- if ( ! foundComponentOfType ) {
353
- var toComponent = to . AddComponent ( components [ i ] . GetType ( ) ) ;
319
+ if ( ! toComponent ) {
320
+ // It doesn't exist => create and copy.
321
+ toComponent = to . AddComponent ( component . GetType ( ) ) ;
322
+ EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
323
+ } else {
324
+ // It exists => copy.
325
+ // But we want to override that behaviour in a few
326
+ // cases, to avoid clobbering references to the new FBX
327
+ // TODO: just modify the json directly.
328
+
329
+ if ( toComponent is MeshFilter ) {
330
+ // Don't copy the mesh. But there's nothing else to
331
+ // copy, so just don't copy anything.
332
+ } else if ( toComponent is SkinnedMeshRenderer ) {
333
+ // Don't want to clobber materials or the mesh.
334
+ var skinnedMesh = toComponent as SkinnedMeshRenderer ;
335
+ var sharedMesh = skinnedMesh . sharedMesh ;
336
+ var sharedMats = skinnedMesh . sharedMaterials ;
337
+ EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
338
+ skinnedMesh . sharedMesh = sharedMesh ;
339
+ skinnedMesh . sharedMaterials = sharedMats ;
340
+ } else if ( toComponent is Renderer ) {
341
+ // Don't want to clobber materials.
342
+ var renderer = toComponent as Renderer ;
343
+ var sharedMats = renderer . sharedMaterials ;
344
+ EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
345
+ renderer . sharedMaterials = sharedMats ;
346
+ } else {
347
+ // Normal case: copy everything.
354
348
EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
355
349
}
356
350
}
0 commit comments