Skip to content

Commit c2ee2a5

Browse files
committed
add back enforce unique names
1 parent 7e2cb21 commit c2ee2a5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ public static GameObject Convert (
135135
}
136136
var projectRelativePath = EditorTools.ExportSettings.GetProjectRelativePath (fbxFullPath);
137137

138+
// Make sure that the object names in the hierarchy are unique.
139+
// The import back in to Unity would do this automatically but
140+
// we prefer to control it so that the Maya artist can see the
141+
// same names as exist in Unity.
142+
EnforceUniqueNames(new GameObject[] { toConvert });
143+
138144
// Export to FBX. It refreshes the database.
139145
{
140146
var fbxActualPath = ModelExporter.ExportObject (
@@ -268,6 +274,40 @@ public static string IncrementFileName(string path, string filename)
268274
return file;
269275
}
270276

277+
/// <summary>
278+
/// Enforces that all object names be unique before exporting.
279+
/// If an object with a duplicate name is found, then it is incremented.
280+
/// e.g. Sphere becomes Sphere 1
281+
/// </summary>
282+
/// <param name="exportSet">Export set.</param>
283+
public static void EnforceUniqueNames(IEnumerable<GameObject> exportSet)
284+
{
285+
Dictionary<string, int> NameToIndexMap = new Dictionary<string, int>();
286+
string format = "{0} {1}";
287+
288+
Queue<GameObject> queue = new Queue<GameObject>(exportSet);
289+
290+
while (queue.Count > 0)
291+
{
292+
var go = queue.Dequeue();
293+
var name = go.name;
294+
if (NameToIndexMap.ContainsKey(name))
295+
{
296+
go.name = string.Format(format, name, NameToIndexMap[name]);
297+
NameToIndexMap[name]++;
298+
}
299+
else
300+
{
301+
NameToIndexMap[name] = 1;
302+
}
303+
304+
foreach (Transform child in go.transform)
305+
{
306+
queue.Enqueue(child.gameObject);
307+
}
308+
}
309+
}
310+
271311
/// <summary>
272312
/// Updates the meshes and materials of the exported GameObjects
273313
/// to link to those imported from the FBX.

Assets/FbxExporters/Editor/UnitTests/ConvertToModelTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public void TestStaticHelpers()
4545
Assert.AreEqual (filename2, ConvertToModel.IncrementFileName (tempPath, filename1));
4646
}
4747

48+
// Test EnforceUniqueNames
49+
{
50+
var a = new GameObject("a");
51+
var b = new GameObject("b");
52+
var a1 = new GameObject("a");
53+
var a2 = new GameObject("a");
54+
ConvertToModel.EnforceUniqueNames(new GameObject[] { a, b, a1, a2 });
55+
Assert.AreEqual("a", a.name);
56+
Assert.AreEqual("b", b.name);
57+
Assert.AreEqual("a 1", a1.name);
58+
Assert.AreEqual("a 2", a2.name);
59+
}
60+
4861
// Test CopyComponents
4962
{
5063
var a = GameObject.CreatePrimitive (PrimitiveType.Cube);

0 commit comments

Comments
 (0)