@@ -486,16 +486,52 @@ protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode
486
486
/// </summary>
487
487
/// <returns>The object count.</returns>
488
488
/// <param name="exportSet">Export set.</param>
489
- public int GetGameObjectCount ( IEnumerable < UnityEngine . Object > exportSet )
489
+ public int GetGameObjectCount ( HashSet < GameObject > exportSet )
490
490
{
491
491
int count = 0 ;
492
492
foreach ( var obj in exportSet ) {
493
+ count += obj . transform . hierarchyCount ;
494
+ }
495
+ return count ;
496
+ }
497
+
498
+ /// <summary>
499
+ /// Removes objects that will already be exported anyway.
500
+ /// E.g. if a parent and its child are both selected, then the child
501
+ /// will be removed from the export set.
502
+ /// </summary>
503
+ /// <returns>The revised export set</returns>
504
+ /// <param name="unityExportSet">Unity export set.</param>
505
+ protected HashSet < GameObject > RemoveDuplicateObjects ( IEnumerable < UnityEngine . Object > unityExportSet )
506
+ {
507
+ // basically just remove the descendents from the unity export set
508
+ HashSet < GameObject > toExport = new HashSet < GameObject > ( ) ;
509
+ HashSet < UnityEngine . Object > hashedExportSet = new HashSet < Object > ( unityExportSet ) ;
510
+
511
+ Queue < UnityEngine . Object > queue = new Queue < Object > ( unityExportSet ) ;
512
+ while ( queue . Count > 0 ) {
513
+ var obj = queue . Dequeue ( ) ;
493
514
var unityGo = GetGameObject ( obj ) ;
515
+
494
516
if ( unityGo ) {
495
- count += unityGo . transform . hierarchyCount ;
517
+ // if any of this nodes ancestors is already in the export set,
518
+ // then ignore it, it will get exported already
519
+ bool parentInSet = false ;
520
+ var parent = unityGo . transform . parent ;
521
+ while ( parent != null ) {
522
+ if ( hashedExportSet . Contains ( parent . gameObject ) ) {
523
+ parentInSet = true ;
524
+ break ;
525
+ }
526
+ parent = parent . parent ;
527
+ }
528
+
529
+ if ( ! parentInSet ) {
530
+ toExport . Add ( unityGo ) ;
531
+ }
496
532
}
497
533
}
498
- return count ;
534
+ return toExport ;
499
535
}
500
536
501
537
/// <summary>
@@ -561,16 +597,13 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
561
597
// export set of object
562
598
FbxNode fbxRootNode = fbxScene . GetRootNode ( ) ;
563
599
int i = 0 ;
564
- int count = GetGameObjectCount ( unityExportSet ) ;
565
- foreach ( var obj in unityExportSet ) {
566
- var unityGo = GetGameObject ( obj ) ;
567
-
568
- if ( unityGo ) {
569
- i = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , i , count ) ;
570
- if ( i < 0 ) {
571
- Debug . LogWarning ( "Export Cancelled" ) ;
572
- return 0 ;
573
- }
600
+ var revisedExportSet = RemoveDuplicateObjects ( unityExportSet ) ;
601
+ int count = GetGameObjectCount ( revisedExportSet ) ;
602
+ foreach ( var unityGo in revisedExportSet ) {
603
+ i = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , i , count ) ;
604
+ if ( i < 0 ) {
605
+ Debug . LogWarning ( "Export Cancelled" ) ;
606
+ return 0 ;
574
607
}
575
608
}
576
609
0 commit comments