I have more than one agent types and want to change the agentTypeId of an agent during per script.
The AgentTypes are defined as Strings e.g. “Humanoid” (the default one).
The variable which stores the information in the gameObject is Integer.
Is there a way to get the Integer-Value out of the String-Value added in the Editor?
The only way I found so far was to set The agents agentTypeId to my custom ones and then switch the inspector to “Debug”, which showed me the value. But that seems to be a very odd way to do that.
And the value itselöf is odd too: Humanoid = 0, NewOne1 = -1372625422
Annoyingly, there does not appear to be a function that maps string name to agent type ID. But you can write one like this:
private int? GetNavMeshAgentID(string name)
{
for (int i = 0; i < NavMesh.GetSettingsCount(); i++)
{
NavMeshBuildSettings settings = NavMesh.GetSettingsByIndex(index: i);
if (name == NavMesh.GetSettingsNameFromID(agentTypeID: settings.agentTypeID))
{
return settings.agentTypeID;
}
}
return null;
}
This is needed if dynamically creating nav mesh surfaces. In my case, I’m creating nav meshes from room plan information obtained from an AR system. I have an inspector field that lets me specify the agent type by name and then I do:
int? agentTypeID = GetNavMeshAgentID(name: _navMeshAgentName);
MeshRenderer meshRenderer = anchor.GetComponentInChildren<MeshRenderer>();
if (meshRenderer != null)
{
NavMeshSurface surface = meshRenderer.gameObject.AddComponent<NavMeshSurface>();
if (agentTypeID != null)
{
surface.agentTypeID = agentTypeID.Value;
}
surface.layerMask = _navMeshLayerMask;
surface.AddData();
surface.overrideVoxelSize = true;
surface.voxelSize = 0.1f * surface.voxelSize; // fix the offset from surface that occurs with the new NavMesh system
surface.BuildNavMesh();
}