@@ -277,6 +277,7 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
277
277
}
278
278
279
279
private static void CopyComponents ( GameObject from , GameObject to ) {
280
+ var originalComponents = new List < Component > ( to . GetComponents < Component > ( ) ) ;
280
281
var components = from . GetComponents < Component > ( ) ;
281
282
for ( int i = 0 ; i < components . Length ; i ++ ) {
282
283
if ( components [ i ] == null ) {
@@ -285,36 +286,42 @@ private static void CopyComponents(GameObject from, GameObject to){
285
286
286
287
bool success = UnityEditorInternal . ComponentUtility . CopyComponent ( components [ i ] ) ;
287
288
if ( success ) {
288
- // if "to" already has this component, and it is not a MeshFilter, Transform, or Renderer, then paste as new.
289
- // We can't have multiple MeshFilters, Transforms, or Renderers, but we can have multiple
290
- // of other components.
291
- var toComponent = to . GetComponent ( components [ i ] . GetType ( ) ) ;
292
- if ( toComponent == null || ! ( toComponent is MeshFilter || toComponent is Transform || toComponent is Renderer ) ) {
293
- success = UnityEditorInternal . ComponentUtility . PasteComponentAsNew ( to ) ;
294
- } else {
295
- // Don't want to copy MeshFilter because then we will replace the
296
- // exported mesh with the old mesh.
297
- if ( toComponent is MeshFilter ) {
298
- continue ;
299
- }
300
- // Don't want to copy materials over either in case the materials are
301
- // embedded in another model.
302
- else if ( toComponent is SkinnedMeshRenderer ) {
303
- var skinnedMesh = toComponent as SkinnedMeshRenderer ;
304
- var sharedMesh = skinnedMesh . sharedMesh ;
305
- var sharedMats = skinnedMesh . sharedMaterials ;
306
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
307
- skinnedMesh . sharedMesh = sharedMesh ;
308
- skinnedMesh . sharedMaterials = sharedMats ;
309
- } else if ( toComponent is Renderer ) {
310
- var renderer = toComponent as Renderer ;
311
- var sharedMats = renderer . sharedMaterials ;
312
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
313
- renderer . sharedMaterials = sharedMats ;
314
- } else {
315
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
289
+ bool foundComponentOfType = false ;
290
+ for ( int j = 0 ; j < originalComponents . Count ; j ++ ) {
291
+ var toComponent = originalComponents [ j ] ;
292
+ // If component already exists, paste values.
293
+ if ( toComponent . GetType ( ) == components [ i ] . GetType ( ) ) {
294
+ // Don't want to copy MeshFilter because then we will replace the
295
+ // exported mesh with the old mesh.
296
+ if ( ! ( toComponent is MeshFilter ) ) {
297
+ // Don't want to copy materials over in case the materials are
298
+ // embedded in another model.
299
+ if ( toComponent is SkinnedMeshRenderer ) {
300
+ var skinnedMesh = toComponent as SkinnedMeshRenderer ;
301
+ var sharedMesh = skinnedMesh . sharedMesh ;
302
+ var sharedMats = skinnedMesh . sharedMaterials ;
303
+ success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
304
+ skinnedMesh . sharedMesh = sharedMesh ;
305
+ skinnedMesh . sharedMaterials = sharedMats ;
306
+ } else if ( toComponent is Renderer ) {
307
+ var renderer = toComponent as Renderer ;
308
+ var sharedMats = renderer . sharedMaterials ;
309
+ success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
310
+ renderer . sharedMaterials = sharedMats ;
311
+ } else {
312
+ success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
313
+ }
314
+ }
315
+ originalComponents . RemoveAt ( j ) ;
316
+ foundComponentOfType = true ;
317
+ break ;
316
318
}
317
319
}
320
+
321
+ // component was not part of the original components, so add it
322
+ if ( ! foundComponentOfType ) {
323
+ success = UnityEditorInternal . ComponentUtility . PasteComponentAsNew ( to ) ;
324
+ }
318
325
}
319
326
if ( ! success ) {
320
327
Debug . LogWarning ( string . Format ( "Warning: Failed to copy component {0} from {1} to {2}" ,
0 commit comments