@@ -484,18 +484,58 @@ protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode
484
484
/// A count of how many GameObjects we are exporting, to have a rough
485
485
/// idea of how long creating the scene will take.
486
486
/// </summary>
487
- /// <returns>The object count.</returns>
487
+ /// <returns>The hierarchy count.</returns>
488
488
/// <param name="exportSet">Export set.</param>
489
- public int GetGameObjectCount ( IEnumerable < UnityEngine . Object > exportSet )
489
+ public int GetHierarchyCount ( HashSet < GameObject > exportSet )
490
490
{
491
491
int count = 0 ;
492
- foreach ( var obj in exportSet ) {
492
+ Queue < GameObject > queue = new Queue < GameObject > ( exportSet ) ;
493
+ while ( queue . Count > 0 ) {
494
+ var obj = queue . Dequeue ( ) ;
495
+ var objTransform = obj . transform ;
496
+ foreach ( Transform child in objTransform ) {
497
+ queue . Enqueue ( child . gameObject ) ;
498
+ }
499
+ count ++ ;
500
+ }
501
+ return count ;
502
+ }
503
+
504
+ /// <summary>
505
+ /// Removes objects that will already be exported anyway.
506
+ /// E.g. if a parent and its child are both selected, then the child
507
+ /// will be removed from the export set.
508
+ /// </summary>
509
+ /// <returns>The revised export set</returns>
510
+ /// <param name="unityExportSet">Unity export set.</param>
511
+ protected HashSet < GameObject > RemoveRedundantObjects ( IEnumerable < UnityEngine . Object > unityExportSet )
512
+ {
513
+ // basically just remove the descendents from the unity export set
514
+ HashSet < GameObject > toExport = new HashSet < GameObject > ( ) ;
515
+ HashSet < UnityEngine . Object > hashedExportSet = new HashSet < Object > ( unityExportSet ) ;
516
+
517
+ foreach ( var obj in unityExportSet ) {
493
518
var unityGo = GetGameObject ( obj ) ;
519
+
494
520
if ( unityGo ) {
495
- count += unityGo . transform . hierarchyCount ;
521
+ // if any of this nodes ancestors is already in the export set,
522
+ // then ignore it, it will get exported already
523
+ bool parentInSet = false ;
524
+ var parent = unityGo . transform . parent ;
525
+ while ( parent != null ) {
526
+ if ( hashedExportSet . Contains ( parent . gameObject ) ) {
527
+ parentInSet = true ;
528
+ break ;
529
+ }
530
+ parent = parent . parent ;
531
+ }
532
+
533
+ if ( ! parentInSet ) {
534
+ toExport . Add ( unityGo ) ;
535
+ }
496
536
}
497
537
}
498
- return count ;
538
+ return toExport ;
499
539
}
500
540
501
541
/// <summary>
@@ -561,16 +601,13 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
561
601
// export set of object
562
602
FbxNode fbxRootNode = fbxScene . GetRootNode ( ) ;
563
603
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
- }
604
+ var revisedExportSet = RemoveRedundantObjects ( unityExportSet ) ;
605
+ int count = GetHierarchyCount ( revisedExportSet ) ;
606
+ foreach ( var unityGo in revisedExportSet ) {
607
+ i = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , i , count ) ;
608
+ if ( i < 0 ) {
609
+ Debug . LogWarning ( "Export Cancelled" ) ;
610
+ return 0 ;
574
611
}
575
612
}
576
613
0 commit comments