Skip to content

Instantly share code, notes, and snippets.

@brihernandez
Last active June 20, 2022 19:07
Show Gist options
  • Save brihernandez/2fdeea8a658205f18880a818d73ef805 to your computer and use it in GitHub Desktop.
Save brihernandez/2fdeea8a658205f18880a818d73ef805 to your computer and use it in GitHub Desktop.
Simple wrapper classes for handling Navmesh Area related operations in Unity.
using UnityEngine.AI;
/// <summary>
/// Convenience class for handling NavMesh Area related operations and lookups in Unity.
/// </summary>
public readonly struct UnityNavMeshLayer
{
/// <summary>
/// Name of the Area as defined in the Navigation window in Unity.
/// </summary>
public readonly string Name;
/// <summary>
/// To be used for mask related queries such as <see cref="NavMeshAgent.SamplePathPosition(int, float, out UnityEngine.AI.NavMeshHit)"/>.
/// </summary>
public readonly int Mask;
/// <summary>
/// ID of the Area for doing lookups and comparisons.
/// </summary>
public readonly int ID;
public UnityNavMeshLayer(string name)
{
Name = name;
ID = NavMesh.GetAreaFromName(name);
Mask = 1 << ID;
}
}
/// <summary>
/// Wrapper class for all the NavMesh Areas that the game uses. Can be accessed from anywhere.
/// </summary>
public static class NavMeshAreaIDs
{
// These are the default hardcoded Areas used by Unity. When you add a new Area for your game,
// simply add a new entry using its name.
public static readonly UnityNavMeshLayer Walkable = new UnityNavMeshLayer("Walkable");
public static readonly UnityNavMeshLayer NotWalkable = new UnityNavMeshLayer("Not Walkable");
public static readonly UnityNavMeshLayer Jump = new UnityNavMeshLayer("Jump");
public static readonly UnityNavMeshLayer Road = new UnityNavMeshLayer("Road");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment