1+ using NavMeshPlus . Components ;
2+ using System . Collections . Generic ;
3+ using UnityEngine ;
4+ using UnityEngine . AI ;
5+ using UnityEngine . Tilemaps ;
6+
7+ namespace NavMeshPlus . Extensions
8+ {
9+ [ ExecuteAlways ]
10+ [ AddComponentMenu ( "Navigation/Navigation CacheTilemapSources2d" , 30 ) ]
11+ public class CollectTilemapSourcesCache2d : NavMeshExtension
12+ {
13+ [ SerializeField ] private Tilemap _tilemap ;
14+ [ SerializeField ] private NavMeshModifier _modifier ;
15+ [ SerializeField ] private NavMeshModifierTilemap _modifierTilemap ;
16+
17+ private List < NavMeshBuildSource > _sources ;
18+ private Dictionary < Vector3Int , int > _lookup ;
19+ private Dictionary < TileBase , NavMeshModifierTilemap . TileModifier > _modifierMap ;
20+
21+ protected override void Awake ( )
22+ {
23+ _modifier ??= _tilemap . GetComponent < NavMeshModifier > ( ) ;
24+ _modifierTilemap ??= _tilemap . GetComponent < NavMeshModifierTilemap > ( ) ;
25+ _modifierMap = _modifierTilemap . GetModifierMap ( ) ;
26+ Order = - 1000 ;
27+ base . Awake ( ) ;
28+ }
29+
30+ private void OnTilemapTileChanged ( Tilemap tilemap , Tilemap . SyncTile [ ] syncTiles )
31+ {
32+ if ( tilemap == _tilemap )
33+ {
34+ foreach ( Tilemap . SyncTile syncTile in syncTiles )
35+ {
36+ Vector3Int position = syncTile . position ;
37+ if ( syncTile . tile != null && _modifierMap . TryGetValue ( syncTile . tile , out NavMeshModifierTilemap . TileModifier tileModifier ) )
38+ {
39+ int i = _lookup [ position ] ;
40+ NavMeshBuildSource source = _sources [ i ] ;
41+ source . area = tileModifier . area ;
42+ _sources [ i ] = source ;
43+ }
44+ else if ( _modifier . overrideArea )
45+ {
46+ int i = _lookup [ position ] ;
47+ NavMeshBuildSource source = _sources [ i ] ;
48+ source . area = _modifier . area ;
49+ _sources [ i ] = source ;
50+ }
51+ }
52+ }
53+ }
54+
55+ public AsyncOperation UpdateNavMesh ( NavMeshData data )
56+ {
57+ return NavMeshBuilder . UpdateNavMeshDataAsync ( data , NavMeshSurfaceOwner . GetBuildSettings ( ) , _sources , data . sourceBounds ) ;
58+ }
59+
60+ public AsyncOperation UpdateNavMesh ( )
61+ {
62+ return UpdateNavMesh ( NavMeshSurfaceOwner . navMeshData ) ;
63+ }
64+
65+ public override void PostCollectSources ( NavMeshSurface surface , List < NavMeshBuildSource > sources , NavMeshBuilderState navNeshState )
66+ {
67+ _sources = sources ;
68+ if ( _lookup == null )
69+ {
70+ _lookup = new Dictionary < Vector3Int , int > ( ) ;
71+ for ( int i = 0 ; i < _sources . Count ; i ++ )
72+ {
73+ NavMeshBuildSource source = _sources [ i ] ;
74+ Vector3Int position = _tilemap . WorldToCell ( source . transform . GetPosition ( ) ) ;
75+ _lookup [ position ] = i ;
76+ }
77+ }
78+ Tilemap . tilemapTileChanged -= OnTilemapTileChanged ;
79+ Tilemap . tilemapTileChanged += OnTilemapTileChanged ;
80+ }
81+
82+ protected override void OnDestroy ( )
83+ {
84+ Tilemap . tilemapTileChanged -= OnTilemapTileChanged ;
85+ base . OnDestroy ( ) ;
86+ }
87+ }
88+ }
0 commit comments