Skip to content

Commit 87a0be7

Browse files
committed
enforce unique names in exported fbx
1 parent 4d9b4c4 commit 87a0be7

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public static ModelExporter Create ()
6969
/// </summary>
7070
Dictionary<string, FbxMesh> SharedMeshes = new Dictionary<string, FbxMesh>();
7171

72+
/// <summary>
73+
/// Map for the Name of an Object to number of objects with this name.
74+
/// Used for enforcing unique names on export.
75+
/// </summary>
76+
Dictionary<string, int> NameToIndexMap = new Dictionary<string, int> ();
77+
78+
/// <summary>
79+
/// Format for creating unique names
80+
/// </summary>
81+
const string UniqueNameFormat = "{0}_{1}";
82+
7283
/// <summary>
7384
/// return layer for mesh
7485
/// </summary>
@@ -587,6 +598,25 @@ protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbx
587598
return true;
588599
}
589600

601+
/// <summary>
602+
/// Ensures that the inputted name is unique.
603+
/// If a duplicate name is found, then it is incremented.
604+
/// e.g. Sphere becomes Sphere 1
605+
/// </summary>
606+
/// <returns>Unique name</returns>
607+
/// <param name="name">Name</param>
608+
private string GetUniqueName(string name)
609+
{
610+
var uniqueName = name;
611+
if (NameToIndexMap.ContainsKey (name)) {
612+
uniqueName = string.Format (UniqueNameFormat, name, NameToIndexMap [name]);
613+
NameToIndexMap [name]++;
614+
} else {
615+
NameToIndexMap [name] = 1;
616+
}
617+
return uniqueName;
618+
}
619+
590620
/// <summary>
591621
/// Unconditionally export components on this game object
592622
/// </summary>
@@ -602,7 +632,7 @@ protected int ExportComponents (
602632
}
603633

604634
// create an FbxNode and add it as a child of parent
605-
FbxNode fbxNode = FbxNode.Create (fbxScene, unityGo.name);
635+
FbxNode fbxNode = FbxNode.Create (fbxScene, GetUniqueName (unityGo.name));
606636
NumNodes++;
607637

608638
numObjectsExported++;

0 commit comments

Comments
 (0)