Skip to content

Commit b4fb717

Browse files
authored
Merge pull request #56 from Unity-Technologies/UNI-21660-instancing-renaming-strategy
UNI-21660 enforce unique names in exported fbx
2 parents 36ac243 + 5784e86 commit b4fb717

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>
@@ -610,6 +621,25 @@ protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbx
610621
return true;
611622
}
612623

624+
/// <summary>
625+
/// Ensures that the inputted name is unique.
626+
/// If a duplicate name is found, then it is incremented.
627+
/// e.g. Sphere becomes Sphere_1
628+
/// </summary>
629+
/// <returns>Unique name</returns>
630+
/// <param name="name">Name</param>
631+
private string GetUniqueName(string name)
632+
{
633+
var uniqueName = name;
634+
if (NameToIndexMap.ContainsKey (name)) {
635+
uniqueName = string.Format (UniqueNameFormat, name, NameToIndexMap [name]);
636+
NameToIndexMap [name]++;
637+
} else {
638+
NameToIndexMap [name] = 1;
639+
}
640+
return uniqueName;
641+
}
642+
613643
/// <summary>
614644
/// Unconditionally export components on this game object
615645
/// </summary>
@@ -625,7 +655,7 @@ protected int ExportComponents (
625655
}
626656

627657
// create an FbxNode and add it as a child of parent
628-
FbxNode fbxNode = FbxNode.Create (fbxScene, unityGo.name);
658+
FbxNode fbxNode = FbxNode.Create (fbxScene, GetUniqueName (unityGo.name));
629659
NumNodes++;
630660

631661
numObjectsExported++;

0 commit comments

Comments
 (0)