Skip to content

Commit 8815d2c

Browse files
authored
Merge pull request #34 from Unity-Technologies/UNI-21369-convert-model-hierarchy-fix
UNI-21369 fix so that the hierarchies match on import
2 parents 5f4dfa0 + c479448 commit 8815d2c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 56 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");
@@ -163,6 +165,44 @@ private static string IncrementFileName(string path, string filename)
163165
return file;
164166
}
165167

168+
/// <summary>
169+
/// Enforces that all object names be unique before exporting.
170+
/// If an object with a duplicate name is found, then it is incremented.
171+
/// e.g. Sphere becomes Sphere 1
172+
/// </summary>
173+
/// <param name="exportSet">Export set.</param>
174+
private static void EnforceUniqueNames(GameObject[] exportSet)
175+
{
176+
Dictionary<string, int> NameToIndexMap = new Dictionary<string, int> ();
177+
string format = "{0} {1}";
178+
179+
Queue<GameObject> queue = new Queue<GameObject> (exportSet);
180+
181+
while(queue.Count > 0){
182+
var go = queue.Dequeue ();
183+
var name = go.name;
184+
if (NameToIndexMap.ContainsKey (name)) {
185+
go.name = string.Format (format, name, NameToIndexMap [name]);
186+
NameToIndexMap [name]++;
187+
} else {
188+
NameToIndexMap [name] = 1;
189+
}
190+
191+
foreach (Transform child in go.transform) {
192+
queue.Enqueue (child.gameObject);
193+
}
194+
}
195+
}
196+
197+
/// <summary>
198+
/// Sets up the imported GameObject to match the original.
199+
/// - Updates the name to be the same as original (i.e. remove the "(Clone)")
200+
/// - Moves the imported object to the correct position in the hierarchy
201+
/// - Updates the transform of the imported GameObject to match the original
202+
/// - Copy over missing components and component values
203+
/// </summary>
204+
/// <param name="orig">Original GameObject.</param>
205+
/// <param name="imported">Imported GameObject.</param>
166206
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
167207
{
168208
Transform importedTransform = imported.transform;
@@ -191,9 +231,25 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
191231
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
192232
origTransform.hierarchyCount, importedTransform.hierarchyCount));
193233
}
234+
FixSiblingOrder (orig.transform, imported.transform);
194235
CopyComponentsRecursive (orig, imported);
195236
}
196237

238+
private static void FixSiblingOrder(Transform orig, Transform imported){
239+
foreach (Transform origChild in orig) {
240+
Transform importedChild = imported.Find (origChild.name);
241+
if (importedChild == null) {
242+
Debug.LogWarning (string.Format(
243+
"Warning: Could not find {0} in parented under {1} in import hierarchy",
244+
origChild.name, imported.name
245+
));
246+
continue;
247+
}
248+
importedChild.SetSiblingIndex (origChild.GetSiblingIndex ());
249+
FixSiblingOrder (origChild, importedChild);
250+
}
251+
}
252+
197253
private static void CopyComponentsRecursive(GameObject from, GameObject to){
198254
if (!to.name.StartsWith(from.name) || from.transform.childCount != to.transform.childCount) {
199255
Debug.LogError (string.Format("Error: hierarchies don't match (From: {0}, To: {1})", from.name, to.name));

0 commit comments

Comments
 (0)