@@ -291,6 +291,7 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
291
291
}
292
292
293
293
private static void CopyComponents ( GameObject from , GameObject to ) {
294
+ var originalComponents = new List < Component > ( to . GetComponents < Component > ( ) ) ;
294
295
var components = from . GetComponents < Component > ( ) ;
295
296
for ( int i = 0 ; i < components . Length ; i ++ ) {
296
297
if ( components [ i ] == null ) {
@@ -299,34 +300,47 @@ private static void CopyComponents(GameObject from, GameObject to){
299
300
300
301
bool success = UnityEditorInternal . ComponentUtility . CopyComponent ( components [ i ] ) ;
301
302
if ( success ) {
302
- // if to already has this component, then copy the values over
303
- var toComponent = to . GetComponent ( components [ i ] . GetType ( ) ) ;
304
- if ( toComponent == null ) {
305
- success = UnityEditorInternal . ComponentUtility . PasteComponentAsNew ( to ) ;
306
- } else {
307
- // Don't want to copy MeshFilter because then we will replace the
308
- // exported mesh with the old mesh.
309
- if ( toComponent is MeshFilter ) {
310
- continue ;
311
- }
312
- // Don't want to copy materials over either in case the materials are
313
- // embedded in another model.
314
- else if ( toComponent is SkinnedMeshRenderer ) {
315
- var skinnedMesh = toComponent as SkinnedMeshRenderer ;
316
- var sharedMesh = skinnedMesh . sharedMesh ;
317
- var sharedMats = skinnedMesh . sharedMaterials ;
318
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
319
- skinnedMesh . sharedMesh = sharedMesh ;
320
- skinnedMesh . sharedMaterials = sharedMats ;
321
- } else if ( toComponent is Renderer ) {
322
- var renderer = toComponent as Renderer ;
323
- var sharedMats = renderer . sharedMaterials ;
324
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
325
- renderer . sharedMaterials = sharedMats ;
326
- } else {
327
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
303
+ bool foundComponentOfType = false ;
304
+ for ( int j = 0 ; j < originalComponents . Count ; j ++ ) {
305
+ var toComponent = originalComponents [ j ] ;
306
+ // If component already exists, paste values.
307
+ if ( toComponent . GetType ( ) == components [ i ] . GetType ( ) ) {
308
+ // we have found the component we are looking for, remove it so
309
+ // we don't try to copy to it again
310
+ originalComponents . RemoveAt ( j ) ;
311
+ foundComponentOfType = true ;
312
+
313
+ // Don't want to copy MeshFilter because then we will replace the
314
+ // exported mesh with the old mesh.
315
+ if ( toComponent is MeshFilter ) {
316
+ break ;
317
+ }
318
+
319
+ // Don't want to copy materials over in case the materials are
320
+ // embedded in another model.
321
+ if ( toComponent is SkinnedMeshRenderer ) {
322
+ var skinnedMesh = toComponent as SkinnedMeshRenderer ;
323
+ var sharedMesh = skinnedMesh . sharedMesh ;
324
+ var sharedMats = skinnedMesh . sharedMaterials ;
325
+ success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
326
+ skinnedMesh . sharedMesh = sharedMesh ;
327
+ skinnedMesh . sharedMaterials = sharedMats ;
328
+ } else if ( toComponent is Renderer ) {
329
+ var renderer = toComponent as Renderer ;
330
+ var sharedMats = renderer . sharedMaterials ;
331
+ success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
332
+ renderer . sharedMaterials = sharedMats ;
333
+ } else {
334
+ success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
335
+ }
336
+ break ;
328
337
}
329
338
}
339
+
340
+ // component was not part of the original components, so add it
341
+ if ( ! foundComponentOfType ) {
342
+ success = UnityEditorInternal . ComponentUtility . PasteComponentAsNew ( to ) ;
343
+ }
330
344
}
331
345
if ( ! success ) {
332
346
Debug . LogWarning ( string . Format ( "Warning: Failed to copy component {0} from {1} to {2}" ,
0 commit comments