@@ -64,6 +64,11 @@ public static ModelExporter Create ()
64
64
/// </summary>
65
65
Dictionary < string , FbxTexture > TextureMap = new Dictionary < string , FbxTexture > ( ) ;
66
66
67
+ /// <summary>
68
+ /// Map the name of a prefab to an FbxMesh (for preserving instances)
69
+ /// </summary>
70
+ Dictionary < string , FbxMesh > SharedMeshes = new Dictionary < string , FbxMesh > ( ) ;
71
+
67
72
/// <summary>
68
73
/// Export the mesh's attributes using layer 0.
69
74
/// </summary>
@@ -359,10 +364,10 @@ private void AssignLayerElementMaterial(FbxMesh fbxMesh, Mesh mesh, int material
359
364
/// Unconditionally export this mesh object to the file.
360
365
/// We have decided; this mesh is definitely getting exported.
361
366
/// </summary>
362
- public void ExportMesh ( MeshInfo meshInfo , FbxNode fbxNode , FbxScene fbxScene , bool weldVertices = true )
367
+ public FbxMesh ExportMesh ( MeshInfo meshInfo , FbxNode fbxNode , FbxScene fbxScene , bool weldVertices = true )
363
368
{
364
369
if ( ! meshInfo . IsValid )
365
- return ;
370
+ return null ;
366
371
367
372
NumMeshes ++ ;
368
373
NumTriangles += meshInfo . Triangles . Length / 3 ;
@@ -452,6 +457,8 @@ meshInfo.Vertices [v].z*UnitScaleFactor
452
457
// set the fbxNode containing the mesh
453
458
fbxNode . SetNodeAttribute ( fbxMesh ) ;
454
459
fbxNode . SetShadingMode ( FbxNode . EShadingMode . eWireFrame ) ;
460
+
461
+ return fbxMesh ;
455
462
}
456
463
457
464
// get a fbxNode's global default position.
@@ -505,6 +512,40 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
505
512
return ;
506
513
}
507
514
515
+ /// <summary>
516
+ /// if this game object is a model prefab then export with shared components
517
+ /// </summary>
518
+ protected bool ExportInstance ( GameObject unityGo , FbxNode fbxNode , FbxScene fbxScene )
519
+ {
520
+ PrefabType unityPrefabType = PrefabUtility . GetPrefabType ( unityGo ) ;
521
+
522
+ if ( unityPrefabType != PrefabType . PrefabInstance ) return false ;
523
+
524
+ Object unityPrefabParent = PrefabUtility . GetPrefabParent ( unityGo ) ;
525
+
526
+ if ( Verbose )
527
+ Debug . Log ( string . Format ( "exporting instance {0}({1})" , unityGo . name , unityPrefabParent . name ) ) ;
528
+
529
+ FbxMesh fbxMesh = null ;
530
+
531
+ if ( ! SharedMeshes . TryGetValue ( unityPrefabParent . name , out fbxMesh ) )
532
+ {
533
+ bool weldVertices = FbxExporters . EditorTools . ExportSettings . instance . weldVertices ;
534
+ fbxMesh = ExportMesh ( GetMeshInfo ( unityGo ) , fbxNode , fbxScene , weldVertices ) ;
535
+ if ( fbxMesh != null ) {
536
+ SharedMeshes [ unityPrefabParent . name ] = fbxMesh ;
537
+ }
538
+ }
539
+
540
+ if ( fbxMesh == null ) return false ;
541
+
542
+ // set the fbxNode containing the mesh
543
+ fbxNode . SetNodeAttribute ( fbxMesh ) ;
544
+ fbxNode . SetShadingMode ( FbxNode . EShadingMode . eWireFrame ) ;
545
+
546
+ return true ;
547
+ }
548
+
508
549
/// <summary>
509
550
/// Unconditionally export components on this game object
510
551
/// </summary>
@@ -533,8 +574,11 @@ protected int ExportComponents (
533
574
534
575
ExportTransform ( unityGo . transform , fbxNode , exportType ) ;
535
576
536
- bool weldVertices = FbxExporters . EditorTools . ExportSettings . instance . weldVertices ;
537
- ExportMesh ( GetMeshInfo ( unityGo ) , fbxNode , fbxScene , weldVertices ) ;
577
+ // try exporting mesh as an instance, export regularly if we cannot
578
+ if ( ! ExportInstance ( unityGo , fbxNode , fbxScene ) ) {
579
+ bool weldVertices = FbxExporters . EditorTools . ExportSettings . instance . weldVertices ;
580
+ ExportMesh ( GetMeshInfo ( unityGo ) , fbxNode , fbxScene , weldVertices ) ;
581
+ }
538
582
539
583
if ( Verbose )
540
584
Debug . Log ( string . Format ( "exporting {0}" , fbxNode . GetName ( ) ) ) ;
0 commit comments