Skip to content

Commit c38a703

Browse files
committed
code review fix
1 parent febaa48 commit c38a703

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
277277
}
278278

279279
private static void CopyComponents(GameObject from, GameObject to){
280+
var originalComponents = new List<Component>(to.GetComponents<Component> ());
280281
var components = from.GetComponents<Component> ();
281282
for(int i = 0; i < components.Length; i++){
282283
if(components[i] == null){
@@ -285,36 +286,42 @@ private static void CopyComponents(GameObject from, GameObject to){
285286

286287
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (components[i]);
287288
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;
316318
}
317319
}
320+
321+
// component was not part of the original components, so add it
322+
if (!foundComponentOfType) {
323+
success = UnityEditorInternal.ComponentUtility.PasteComponentAsNew (to);
324+
}
318325
}
319326
if (!success) {
320327
Debug.LogWarning (string.Format ("Warning: Failed to copy component {0} from {1} to {2}",

0 commit comments

Comments
 (0)