Skip to content

Commit 82f2136

Browse files
committed
export selections as separate models
This way the hierarchy remains the same without having to break any prefab connections
1 parent cbc950b commit 82f2136

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,36 @@ private static List<GameObject> OnConvertInPlace ()
5757

5858
GameObject [] unityActiveGOs = Selection.GetFiltered<GameObject> (SelectionMode.Editable | SelectionMode.TopLevel);
5959

60+
var exportSet = ModelExporter.RemoveDuplicateObjects (unityActiveGOs);
61+
GameObject[] gosToExport = new GameObject[exportSet.Count];
62+
exportSet.CopyTo (gosToExport);
63+
6064
// find common ancestor root & filePath;
61-
string filePath = "";
65+
string[] filePaths = new string[gosToExport.Length];
6266
string dirPath = Path.Combine (Application.dataPath, "Objects");
6367

64-
GameObject unityCommonAncestor = null;
65-
int siblingIndex = -1;
66-
67-
foreach (GameObject goObj in unityActiveGOs) {
68-
siblingIndex = goObj.transform.GetSiblingIndex ();
69-
unityCommonAncestor = (goObj.transform.parent != null) ? goObj.transform.parent.gameObject : null;
70-
filePath = Path.Combine (dirPath, goObj.name + ".fbx");
68+
Transform[] unityCommonAncestors = new Transform[gosToExport.Length];
69+
int[] siblingIndices = new int[gosToExport.Length];
7170

72-
break;
71+
for(int n = 0; n < gosToExport.Length; n++){
72+
GameObject goObj = gosToExport[n];
73+
unityCommonAncestors[n] = goObj.transform.parent;
74+
siblingIndices [n] = goObj.transform.GetSiblingIndex ();
75+
filePaths[n] = Path.Combine (dirPath, goObj.name + ".fbx");
7376
}
7477

75-
string fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filePath, unityActiveGOs) as string;
78+
string[] fbxFileNames = new string[filePaths.Length];
7679

77-
if (fbxFileName != null)
80+
for (int j = 0; j < gosToExport.Length; j++) {
81+
fbxFileNames[j] = FbxExporters.Editor.ModelExporter.ExportObjects (filePaths[j],
82+
new UnityEngine.Object[] {gosToExport[j]}) as string;
83+
}
84+
85+
List<GameObject> selection = new List<GameObject> ();
86+
for(int i = 0; i < fbxFileNames.Length; i++)
7887
{
88+
var fbxFileName = fbxFileNames [i];
89+
7990
// make filepath relative to project folder
8091
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
8192
{
@@ -103,33 +114,30 @@ private static List<GameObject> OnConvertInPlace ()
103114
}
104115

105116
// configure transform and maintain local pose
106-
if (unityCommonAncestor != null) {
107-
unityGO.transform.SetParent (unityCommonAncestor.transform, false);
108-
}
117+
unityGO.transform.SetParent (unityCommonAncestors[i], false);
109118

110-
unityGO.transform.SetSiblingIndex (siblingIndex);
119+
unityGO.transform.SetSiblingIndex (siblingIndices[i]);
111120

112121
result.Add (unityObj as GameObject);
113122

114-
// remove (now redundant) gameobjects
115-
for (int i = 0; i < unityActiveGOs.Length; i++) {
123+
// remove (now redundant) gameobject
116124
#if UNI_19965
117-
Object.DestroyImmediate (unityActiveGOs [i]);
125+
Object.DestroyImmediate (unityActiveGOs [i]);
118126
#else
119-
// rename and put under scene root in case we need to check values
120-
unityActiveGOs [i].name = "_safe_to_delete_" + unityActiveGOs [i].name;
121-
unityActiveGOs [i].transform.parent = null;
122-
unityActiveGOs [i].SetActive (false);
127+
// rename and put under scene root in case we need to check values
128+
gosToExport [i].name = "_safe_to_delete_" + gosToExport[i].name;
129+
gosToExport [i].transform.parent = null;
130+
gosToExport [i].SetActive (false);
123131
#endif
124-
}
125-
126132
// select the instanced Model Prefab
127-
Selection.objects = new GameObject[] {unityGO};
133+
selection.Add(unityGO);
128134
}
129135
}
130136

131137
}
132138

139+
Selection.objects = selection.ToArray ();
140+
133141
return result;
134142
}
135143
}

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public int GetGameObjectCount (HashSet<GameObject> exportSet)
502502
/// </summary>
503503
/// <returns>The revised export set</returns>
504504
/// <param name="unityExportSet">Unity export set.</param>
505-
protected HashSet<GameObject> RemoveDuplicateObjects(IEnumerable<UnityEngine.Object> unityExportSet)
505+
public static HashSet<GameObject> RemoveDuplicateObjects(IEnumerable<UnityEngine.Object> unityExportSet)
506506
{
507507
// basically just remove the descendents from the unity export set
508508
HashSet<GameObject> toExport = new HashSet<GameObject> ();
@@ -832,7 +832,7 @@ public MeshInfo (GameObject gameObject, Mesh mesh)
832832
/// <summary>
833833
/// Get the GameObject
834834
/// </summary>
835-
private GameObject GetGameObject (Object obj)
835+
private static GameObject GetGameObject (Object obj)
836836
{
837837
if (obj is UnityEngine.Transform) {
838838
var xform = obj as UnityEngine.Transform;

0 commit comments

Comments
 (0)