Skip to content

Commit f5b6f44

Browse files
authored
Merge pull request #23 from Unity-Technologies/UNI-21302-copy-component-values
UNI-21302 copy over component values if the component already exists
2 parents d2d1bf5 + 2ed9af8 commit f5b6f44

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,30 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to){
172172
private static void CopyComponents(GameObject from, GameObject to){
173173
var components = from.GetComponents<Component> ();
174174
for(int i = 0; i < components.Length; i++){
175-
// if to already has this component, then skip it
176-
if(components[i] == null || to.GetComponent(components[i].GetType()) != null){
175+
if(components[i] == null){
177176
continue;
178177
}
178+
179179
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (components[i]);
180180
if (success) {
181-
success = UnityEditorInternal.ComponentUtility.PasteComponentAsNew (to);
181+
// if to already has this component, then copy the values over
182+
var toComponent = to.GetComponent (components [i].GetType ());
183+
if (toComponent != null) {
184+
// don't want to copy MeshFilter because then we will replace the
185+
// exported mesh with the old mesh
186+
if (!(toComponent is MeshFilter)) {
187+
if (toComponent is SkinnedMeshRenderer) {
188+
var skinnedMesh = toComponent as SkinnedMeshRenderer;
189+
var sharedMesh = skinnedMesh.sharedMesh;
190+
success = UnityEditorInternal.ComponentUtility.PasteComponentValues (toComponent);
191+
skinnedMesh.sharedMesh = sharedMesh;
192+
} else {
193+
success = UnityEditorInternal.ComponentUtility.PasteComponentValues (toComponent);
194+
}
195+
}
196+
} else {
197+
success = UnityEditorInternal.ComponentUtility.PasteComponentAsNew (to);
198+
}
182199
}
183200
if (!success) {
184201
Debug.LogWarning (string.Format ("Warning: Failed to copy component {0} from {1} to {2}",

0 commit comments

Comments
 (0)