Files
megakoop/Game/Scripts/Runtime/Navigation/NavMeshRuntimeUtility.cs
2025-10-26 14:17:31 +01:00

48 lines
1.4 KiB
C#

using System;
using UnityEngine.AI;
namespace Game.Scripts.Runtime.Navigation
{
/// <summary>
/// Lightweight cache around NavMesh availability checks so gameplay code
/// can gracefully degrade when a bake is not present.
/// </summary>
public static class NavMeshRuntimeUtility
{
private static bool? _cachedHasNavMesh;
/// <summary>
/// Returns true if there is any baked NavMesh data loaded at runtime.
/// Result is cached until <see cref="InvalidateCache"/> is called.
/// </summary>
public static bool HasNavMeshData()
{
if (_cachedHasNavMesh.HasValue)
{
return _cachedHasNavMesh.Value;
}
try
{
var triangulation = NavMesh.CalculateTriangulation();
var hasData = triangulation.vertices != null && triangulation.vertices.Length > 0;
_cachedHasNavMesh = hasData;
return hasData;
}
catch (Exception)
{
_cachedHasNavMesh = false;
return false;
}
}
/// <summary>
/// Clears the cached NavMesh availability state. Call this after baking at runtime.
/// </summary>
public static void InvalidateCache()
{
_cachedHasNavMesh = null;
}
}
}