Skip to content

Commit 0ff821f

Browse files
committed
fix so that the hierarchies match on import
1 parent 329cfd3 commit 0ff821f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
7171
GameObject[] gosToExport = new GameObject[exportSet.Count];
7272
exportSet.CopyTo (gosToExport);
7373

74+
EnforceUniqueNames (gosToExport);
75+
7476
// find common ancestor root & filePath;
7577
string[] filePaths = new string[gosToExport.Length];
7678
string dirPath = Path.Combine (Application.dataPath, "Objects");
@@ -136,6 +138,35 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
136138
return result;
137139
}
138140

141+
/// <summary>
142+
/// Enforces that all object names be unique before exporting.
143+
/// If an object with a duplicate name is found, then it is incremented.
144+
/// e.g. Sphere becomes Sphere 1
145+
/// </summary>
146+
/// <param name="exportSet">Export set.</param>
147+
private static void EnforceUniqueNames(GameObject[] exportSet)
148+
{
149+
Dictionary<string, int> NameToIndexMap = new Dictionary<string, int> ();
150+
string format = "{0} {1}";
151+
152+
Queue<GameObject> queue = new Queue<GameObject> (exportSet);
153+
154+
while(queue.Count > 0){
155+
var go = queue.Dequeue ();
156+
var name = go.name;
157+
if (NameToIndexMap.ContainsKey (name)) {
158+
go.name = string.Format (format, name, NameToIndexMap [name]);
159+
NameToIndexMap [name]++;
160+
} else {
161+
NameToIndexMap [name] = 1;
162+
}
163+
164+
foreach (Transform child in go.transform) {
165+
queue.Enqueue (child.gameObject);
166+
}
167+
}
168+
}
169+
139170
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
140171
{
141172
Transform importedTransform = imported.transform;
@@ -164,9 +195,25 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
164195
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
165196
origTransform.hierarchyCount, importedTransform.hierarchyCount));
166197
}
198+
FixSiblingOrder (orig.transform, imported.transform);
167199
CopyComponentsRecursive (orig, imported);
168200
}
169201

202+
private static void FixSiblingOrder(Transform orig, Transform imported){
203+
foreach (Transform origChild in orig) {
204+
Transform importedChild = imported.Find (origChild.name);
205+
if (importedChild == null) {
206+
Debug.LogWarning (string.Format(
207+
"Warning: Could not find {0} in parented under {1} in import hierarchy",
208+
origChild.name, imported.name
209+
));
210+
continue;
211+
}
212+
importedChild.SetSiblingIndex (origChild.GetSiblingIndex ());
213+
FixSiblingOrder (origChild, importedChild);
214+
}
215+
}
216+
170217
private static void CopyComponentsRecursive(GameObject from, GameObject to){
171218
if (!to.name.StartsWith(from.name) || from.transform.childCount != to.transform.childCount) {
172219
Debug.LogError (string.Format("Error: hierarchies don't match (From: {0}, To: {1})", from.name, to.name));

0 commit comments

Comments
 (0)