Skip to content

Commit

Permalink
#201 - Added CollectTilemapSourcesCache2d extension (#202)
Browse files Browse the repository at this point in the history
* #201 - Added CollectTilemapSourcesCache2d extension which caches individual tilemap tile sources for increased navmesh update performance
  • Loading branch information
snarlynarwhal authored Jun 29, 2024
1 parent c5c8af2 commit ef1e438
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
88 changes: 88 additions & 0 deletions NavMeshComponents/Scripts/CollectTilemapSourcesCache2d.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using NavMeshPlus.Components;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Tilemaps;

namespace NavMeshPlus.Extensions
{
[ExecuteAlways]
[AddComponentMenu("Navigation/Navigation CacheTilemapSources2d", 30)]
public class CollectTilemapSourcesCache2d : NavMeshExtension
{
[SerializeField] private Tilemap _tilemap;
[SerializeField] private NavMeshModifier _modifier;
[SerializeField] private NavMeshModifierTilemap _modifierTilemap;

private List<NavMeshBuildSource> _sources;
private Dictionary<Vector3Int, int> _lookup;
private Dictionary<TileBase, NavMeshModifierTilemap.TileModifier> _modifierMap;

protected override void Awake()
{
_modifier ??= _tilemap.GetComponent<NavMeshModifier>();
_modifierTilemap ??= _tilemap.GetComponent<NavMeshModifierTilemap>();
_modifierMap = _modifierTilemap.GetModifierMap();
Order = -1000;
base.Awake();
}

private void OnTilemapTileChanged(Tilemap tilemap, Tilemap.SyncTile[] syncTiles)
{
if (tilemap == _tilemap)
{
foreach (Tilemap.SyncTile syncTile in syncTiles)
{
Vector3Int position = syncTile.position;
if (syncTile.tile != null && _modifierMap.TryGetValue(syncTile.tile, out NavMeshModifierTilemap.TileModifier tileModifier))
{
int i = _lookup[position];
NavMeshBuildSource source = _sources[i];
source.area = tileModifier.area;
_sources[i] = source;
}
else if (_modifier.overrideArea)
{
int i = _lookup[position];
NavMeshBuildSource source = _sources[i];
source.area = _modifier.area;
_sources[i] = source;
}
}
}
}

public AsyncOperation UpdateNavMesh(NavMeshData data)
{
return NavMeshBuilder.UpdateNavMeshDataAsync(data, NavMeshSurfaceOwner.GetBuildSettings(), _sources, data.sourceBounds);
}

public AsyncOperation UpdateNavMesh()
{
return UpdateNavMesh(NavMeshSurfaceOwner.navMeshData);
}

public override void PostCollectSources(NavMeshSurface surface, List<NavMeshBuildSource> sources, NavMeshBuilderState navNeshState)
{
_sources = sources;
if (_lookup == null)
{
_lookup = new Dictionary<Vector3Int, int>();
for (int i = 0; i < _sources.Count; i++)
{
NavMeshBuildSource source = _sources[i];
Vector3Int position = _tilemap.WorldToCell(source.transform.GetPosition());
_lookup[position] = i;
}
}
Tilemap.tilemapTileChanged -= OnTilemapTileChanged;
Tilemap.tilemapTileChanged += OnTilemapTileChanged;
}

protected override void OnDestroy()
{
Tilemap.tilemapTileChanged -= OnTilemapTileChanged;
base.OnDestroy();
}
}
}
11 changes: 11 additions & 0 deletions NavMeshComponents/Scripts/CollectTilemapSourcesCache2d.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef1e438

Please sign in to comment.