Skip to content

Commit 5bdbea2

Browse files
committed
create a prefab from the converted GO instead of FBX
- no references lost as we are using the original hierarchy and not copying over components - only copy over mesh and materials from FBX
1 parent 62ea717 commit 5bdbea2

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ public static GameObject Convert (
192192
// Copy the components over to the instance of the FBX.
193193
SetupImportedGameObject (toConvert, unityGO);
194194

195+
// temporary quick fix
196+
var temp = toConvert;
197+
toConvert = unityGO;
198+
unityGO = temp;
199+
195200
// Set up the FbxPrefab component so it will auto-update.
196201
// Make sure to delete whatever FbxPrefab history we had.
197202
var fbxPrefab = unityGO.GetComponent<FbxPrefab>();
@@ -376,29 +381,28 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
376381
}
377382
}
378383

384+
379385
/// <summary>
380-
/// Copy components on the 'from' object which is the object in the
381-
/// scene we exported, over to the 'to' object which is the object
382-
/// in the scene that we imported from the FBX.
386+
/// Copy components on the 'from' object which is the object
387+
/// in the scene that we imported from the FBX,
388+
/// over to the 'to' object which is the object in the
389+
/// scene we exported.
383390
///
384-
/// Exception: don't copy the references to assets in the scene that
385-
/// were also exported, in particular the meshes and materials.
391+
/// Only copy over meshes and materials, since that is all the FBX contains
392+
/// that is not already in the scene.
386393
///
387394
/// The 'from' hierarchy is not modified.
388395
/// </summary>
389-
public static void CopyComponents(GameObject from, GameObject to){
396+
public static void CopyComponents(GameObject to, GameObject from){
390397
var originalComponents = new List<Component>(to.GetComponents<Component> ());
391-
foreach(var component in from.GetComponents<Component> ()) {
392-
// UNI-24379: we don't support SkinnedMeshRenderer right
393-
// now: we just bake the mesh into its current pose. So
394-
// don't copy the SMR over. There will already be a
395-
// MeshFilter and MeshRenderer due to the static mesh.
396-
// Remove this code when we support skinning.
397-
if (component is SkinnedMeshRenderer) {
398-
continue;
399-
}
398+
// copy over meshes, materials, and nothing else
399+
foreach (var component in from.GetComponents<Component>()) {
400400

401401
var json = EditorJsonUtility.ToJson(component);
402+
if (string.IsNullOrEmpty (json)) {
403+
// this happens for missing scripts
404+
continue;
405+
}
402406

403407
System.Type expectedType = component.GetType();
404408
Component toComponent = null;
@@ -415,38 +419,20 @@ public static void CopyComponents(GameObject from, GameObject to){
415419
}
416420

417421
if (!toComponent) {
418-
// It doesn't exist => create and copy.
419-
toComponent = to.AddComponent(component.GetType());
420-
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
421-
} else {
422-
// It exists => copy.
423-
// But we want to override that behaviour in a few
424-
// cases, to avoid clobbering references to the new FBX
425-
// TODO: interpret the object or the json more directly
426-
// TODO: be more generic
427-
// TODO: handle references to other objects in the same hierarchy
428-
429-
if (toComponent is MeshFilter) {
430-
// Don't copy the mesh. But there's nothing else to
431-
// copy, so just don't copy anything.
432-
} else if (toComponent is SkinnedMeshRenderer) {
433-
// Don't want to clobber materials or the mesh.
434-
var skinnedMesh = toComponent as SkinnedMeshRenderer;
435-
var sharedMesh = skinnedMesh.sharedMesh;
436-
var sharedMats = skinnedMesh.sharedMaterials;
437-
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
438-
skinnedMesh.sharedMesh = sharedMesh;
439-
skinnedMesh.sharedMaterials = sharedMats;
440-
} else if (toComponent is Renderer) {
441-
// Don't want to clobber materials.
442-
var renderer = toComponent as Renderer;
443-
var sharedMats = renderer.sharedMaterials;
444-
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
445-
renderer.sharedMaterials = sharedMats;
446-
} else {
447-
// Normal case: copy everything.
448-
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
449-
}
422+
continue;
423+
}
424+
425+
if (toComponent is SkinnedMeshRenderer) {
426+
var skinnedMesh = toComponent as SkinnedMeshRenderer;
427+
var fromSkinnedMesh = component as SkinnedMeshRenderer;
428+
skinnedMesh.sharedMesh = fromSkinnedMesh.sharedMesh;
429+
skinnedMesh.sharedMaterials = fromSkinnedMesh.sharedMaterials;
430+
} else if (toComponent is MeshFilter) {
431+
EditorJsonUtility.FromJsonOverwrite (json, toComponent);
432+
} else if (toComponent is Renderer) {
433+
var toRen = toComponent as Renderer;
434+
var fromRen = component as Renderer;
435+
toRen.sharedMaterials = fromRen.sharedMaterials;
450436
}
451437
}
452438
}

0 commit comments

Comments
 (0)