Skip to content

Commit 54c5276

Browse files
authored
Merge pull request #19 from Unity-Technologies/UNI-21175-export-default-selection-behavior
Uni 21175 export default selection behavior
2 parents 86bb708 + 185e605 commit 54c5276

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -484,18 +484,58 @@ protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode
484484
/// A count of how many GameObjects we are exporting, to have a rough
485485
/// idea of how long creating the scene will take.
486486
/// </summary>
487-
/// <returns>The object count.</returns>
487+
/// <returns>The hierarchy count.</returns>
488488
/// <param name="exportSet">Export set.</param>
489-
public int GetGameObjectCount (IEnumerable<UnityEngine.Object> exportSet)
489+
public int GetHierarchyCount (HashSet<GameObject> exportSet)
490490
{
491491
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){
493518
var unityGo = GetGameObject (obj);
519+
494520
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+
}
496536
}
497537
}
498-
return count;
538+
return toExport;
499539
}
500540

501541
/// <summary>
@@ -561,16 +601,13 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
561601
// export set of object
562602
FbxNode fbxRootNode = fbxScene.GetRootNode ();
563603
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;
574611
}
575612
}
576613

0 commit comments

Comments
 (0)