Skip to content

Commit ea48923

Browse files
committed
Simplify unique node name generation logic
This commit improves developer experience as the original node name is kept visible, and the UID is appended to the end [CI BUILD DEV] [CI BUILD]
1 parent d8aa0c7 commit ea48923

File tree

1 file changed

+21
-40
lines changed

1 file changed

+21
-40
lines changed

Assets/Scripts/Model.cs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,8 @@ public IExportableMaterial GetExportableMaterial(Material material)
248248
EnsureCollectorExists();
249249
return m_ImportMaterialCollector.GetExportableMaterial(material);
250250
}
251+
251252

252-
public struct UidToNodeMapItem
253-
{
254-
public Transform nodeTransform;
255-
public string nodeRealName;
256-
}
257-
258-
// initialized when model is loaded
259-
// - initialization uses GetInstanceID() to get unique id for each node in the model
260-
// - this dictionary is currently used for finding subtrees when breaking models apart, in ModelWidget.cs
261-
// - unique identifiers for nodes are needed because node names in e.g., glTF aren't unique
262-
public Dictionary<int, UidToNodeMapItem> UidToNodeMap;
263253

264254
public Model(Location location)
265255
{
@@ -854,8 +844,6 @@ private void CalcBoundsNonGltf(GameObject go)
854844

855845
}
856846

857-
static ProfilerMarker _endCreatePrefabPerfMarker = new ProfilerMarker("Model.EndCreatePrefab");
858-
859847
public void EndCreatePrefab(GameObject go, List<string> warnings)
860848
{
861849

@@ -875,53 +863,46 @@ public void EndCreatePrefab(GameObject go, List<string> warnings)
875863
}
876864
m_ModelParent = go.transform;
877865

878-
_endCreatePrefabPerfMarker.Begin();
879-
880-
InitializeUidToNodeMap(m_ModelParent);
866+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
867+
ProfilerMarker generateUniqueNamesPerfMarker = new ProfilerMarker("Model.GenerateUniqueNames");
868+
generateUniqueNamesPerfMarker.Begin();
869+
#endif
870+
871+
GenerateUniqueNames(m_ModelParent);
881872

882-
_endCreatePrefabPerfMarker.End();
873+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
874+
generateUniqueNamesPerfMarker.End();
875+
#endif
883876

884877
// !!! Add to material dictionary here?
885878

886879
m_Valid = true;
887880
DisplayWarnings(warnings);
888881

889882
}
890-
891-
892883

893-
public string GetNodeRealNameFromID(int uid)
894-
{
895-
if (UidToNodeMap.ContainsKey(uid))
896-
{
897-
return UidToNodeMap[uid].nodeRealName;
898-
}
899-
return null;
900-
}
884+
901885

902-
private void InitializeUidToNodeMap(Transform rootNode)
886+
// This method is called when the model has been loaded and the node tree is available
887+
// This method is necessary because (1) nodes in e.g glTF files don't need to have unique names
888+
// and (2) there's code in at least ModelWidget that searches for specific nodes using node names
889+
private static void GenerateUniqueNames(Transform rootNode)
903890
{
904-
// the immediate children of rootNode are the root nodes of the model
905-
906-
UidToNodeMap = new Dictionary<int, UidToNodeMapItem>();
907891

908-
void ProcessNode(Transform node)
892+
void SetUniqueNameForNode(Transform node)
909893
{
910-
UidToNodeMapItem uidToNodeMapItem = new UidToNodeMapItem();
911-
uidToNodeMapItem.nodeTransform = node;
912-
uidToNodeMapItem.nodeRealName = node.name;
913-
node.name = node.gameObject.GetInstanceID().ToString();
914-
UidToNodeMap[node.gameObject.GetInstanceID()] = uidToNodeMapItem;
915-
894+
// GetInstanceID returns a unique ID for every GameObject during a runtime session
895+
node.name += " uid: " + node.gameObject.GetInstanceID();
896+
916897
foreach (Transform child in node)
917898
{
918-
ProcessNode(child);
899+
SetUniqueNameForNode(child);
919900
}
920901
}
921902

922903
foreach (Transform child in rootNode)
923904
{
924-
ProcessNode(child);
905+
SetUniqueNameForNode(child);
925906
}
926907
}
927908

0 commit comments

Comments
 (0)