Skip to content

Commit 44cc2df

Browse files
authored
Merge pull request #63 from Unity-Technologies/UNI-22414-copy-dup-components
UNI-22414 Copy over duplicate components as new components
2 parents 20eb987 + f384fed commit 44cc2df

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
291291
}
292292

293293
private static void CopyComponents(GameObject from, GameObject to){
294+
var originalComponents = new List<Component>(to.GetComponents<Component> ());
294295
var components = from.GetComponents<Component> ();
295296
for(int i = 0; i < components.Length; i++){
296297
if(components[i] == null){
@@ -299,34 +300,47 @@ private static void CopyComponents(GameObject from, GameObject to){
299300

300301
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (components[i]);
301302
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;
328337
}
329338
}
339+
340+
// component was not part of the original components, so add it
341+
if (!foundComponentOfType) {
342+
success = UnityEditorInternal.ComponentUtility.PasteComponentAsNew (to);
343+
}
330344
}
331345
if (!success) {
332346
Debug.LogWarning (string.Format ("Warning: Failed to copy component {0} from {1} to {2}",

0 commit comments

Comments
 (0)