@@ -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>
@@ -335,10 +340,10 @@ private void AssignLayerElementMaterial(FbxMesh fbxMesh)
335
340
/// Unconditionally export this mesh object to the file.
336
341
/// We have decided; this mesh is definitely getting exported.
337
342
/// </summary>
338
- public void ExportMesh ( MeshInfo meshInfo , FbxNode fbxNode , FbxScene fbxScene , bool weldVertices = true )
343
+ public FbxMesh ExportMesh ( MeshInfo meshInfo , FbxNode fbxNode , FbxScene fbxScene , bool weldVertices = true )
339
344
{
340
345
if ( ! meshInfo . IsValid )
341
- return ;
346
+ return null ;
342
347
343
348
NumMeshes ++ ;
344
349
NumTriangles += meshInfo . Triangles . Length / 3 ;
@@ -424,6 +429,8 @@ meshInfo.Vertices [v].z*UnitScaleFactor
424
429
// set the fbxNode containing the mesh
425
430
fbxNode . SetNodeAttribute ( fbxMesh ) ;
426
431
fbxNode . SetShadingMode ( FbxNode . EShadingMode . eWireFrame ) ;
432
+
433
+ return fbxMesh ;
427
434
}
428
435
429
436
// get a fbxNode's global default position.
@@ -477,6 +484,40 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
477
484
return ;
478
485
}
479
486
487
+ /// <summary>
488
+ /// if this game object is a model prefab then export with shared components
489
+ /// </summary>
490
+ protected bool ExportInstance ( GameObject unityGo , FbxNode fbxNode , FbxScene fbxScene )
491
+ {
492
+ PrefabType unityPrefabType = PrefabUtility . GetPrefabType ( unityGo ) ;
493
+
494
+ if ( unityPrefabType != PrefabType . PrefabInstance ) return false ;
495
+
496
+ Object unityPrefabParent = PrefabUtility . GetPrefabParent ( unityGo ) ;
497
+
498
+ if ( Verbose )
499
+ Debug . Log ( string . Format ( "exporting instance {0}({1})" , unityGo . name , unityPrefabParent . name ) ) ;
500
+
501
+ FbxMesh fbxMesh = null ;
502
+
503
+ if ( ! SharedMeshes . TryGetValue ( unityPrefabParent . name , out fbxMesh ) )
504
+ {
505
+ bool weldVertices = FbxExporters . EditorTools . ExportSettings . instance . weldVertices ;
506
+ fbxMesh = ExportMesh ( GetMeshInfo ( unityGo ) , fbxNode , fbxScene , weldVertices ) ;
507
+ if ( fbxMesh != null ) {
508
+ SharedMeshes [ unityPrefabParent . name ] = fbxMesh ;
509
+ }
510
+ }
511
+
512
+ if ( fbxMesh == null ) return false ;
513
+
514
+ // set the fbxNode containing the mesh
515
+ fbxNode . SetNodeAttribute ( fbxMesh ) ;
516
+ fbxNode . SetShadingMode ( FbxNode . EShadingMode . eWireFrame ) ;
517
+
518
+ return true ;
519
+ }
520
+
480
521
/// <summary>
481
522
/// Unconditionally export components on this game object
482
523
/// </summary>
@@ -505,8 +546,11 @@ protected int ExportComponents (
505
546
506
547
ExportTransform ( unityGo . transform , fbxNode , exportType ) ;
507
548
508
- bool weldVertices = FbxExporters . EditorTools . ExportSettings . instance . weldVertices ;
509
- ExportMesh ( GetMeshInfo ( unityGo ) , fbxNode , fbxScene , weldVertices ) ;
549
+ // try exporting mesh as an instance, export regularly if we cannot
550
+ if ( ! ExportInstance ( unityGo , fbxNode , fbxScene ) ) {
551
+ bool weldVertices = FbxExporters . EditorTools . ExportSettings . instance . weldVertices ;
552
+ ExportMesh ( GetMeshInfo ( unityGo ) , fbxNode , fbxScene , weldVertices ) ;
553
+ }
510
554
511
555
if ( Verbose )
512
556
Debug . Log ( string . Format ( "exporting {0}" , fbxNode . GetName ( ) ) ) ;
0 commit comments