- A* version: [enter version here]
- Unity version: 2023.3.23f
Hey I am just writing this post to see if i could potentially get help on generating 2D graphs on runtime. I have managed to get the graphs created but Need help in aligning it to a tilemap and fitting the floor. The reason I need this is because the game is procedural and rooms are randomly spawned. If there is another solution I would love to hear it.
Code for reference.
using System.Linq;
using UnityEngine;
using UnityEngine.Tilemaps;
using Edgar.Unity;
using Pathfinding;
[CreateAssetMenu(menuName = "AI/Pathfinding", fileName = "GridGeneratorPostProcessing")]
public class PathfindingGenerator : DungeonGeneratorPostProcessingGrid2D
{
public override void Run(DungeonGeneratorLevelGrid2D level)
{
foreach (var roomInstance in level.RoomInstances)
{
var roomTemplateInstance = roomInstance.RoomTemplateInstance;
// Find floor tilemap layer
var tilemaps = RoomTemplateUtilsGrid2D.GetTilemaps(roomTemplateInstance);
Tilemap floor = tilemaps.Single(x => x.name == "Floor");
// Use floor to create new graph for the pathfinding
// This holds all graph data
AstarData data = AstarPath.active.data;
// This creates a Grid Graph
GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;
gg.AlignToTilemap(floor.layoutGrid);
}
}
}