-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
c5c8af2
commit ef1e438
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
NavMeshComponents/Scripts/CollectTilemapSourcesCache2d.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.