Pathfinding: Custom Adapters
While DunGen provides built-in integration adapters for popular pathfinding solutions like Unity NavMesh Components and A* Pathfinding Project Pro, you might be using a different system. DunGen offers a flexible way to integrate any third-party system (including custom pathfinding solutions) by creating your own Custom Adapter.
What's an Adapter?
DunGen Adapters are C# script components that hook into the dungeon generation lifecycle, specifically during the post-processing phase. This phase occurs after the main dungeon layout (Tiles and connections) has been generated and placed in the scene.
By creating a script that inherits from DunGen's NavMeshAdapter class, you can execute your custom logic at the right time, allowing you to trigger actions in your chosen pathfinding system, such as:
- Scanning or baking navigation data based on the generated geometry.
- Placing specific pathfinding-related objects or markers.
- Updating pathfinding graphs based on door states or other dynamic elements.
The NavMeshAdapter Class
To create a custom adapter, your script must inherit from DunGen.Adapters.NavMeshAdapter.
using UnityEngine;
using DunGen;
using DunGen.Adapters;
public class MyCustomPathfindingAdapter : NavMeshAdapter
{
// Your custom settings can go here (exposed in the Inspector)
// [SerializeField] private float scanResolution = 0.5f;
// You MUST override the abstract Generate method
public override void Generate(Dungeon dungeon)
{
Debug.Log("My Custom Pathfinding Adapter is running!");
// --- Add your custom pathfinding logic here ---
// Example: Trigger your pathfinding system's API
// MyPathfindingSystem.Instance.ScanNewArea(dungeon.AllTiles);
}
}
Key points about NavMeshAdapter:
- It inherits from
MonoBehaviour, so it's a standard Unity component. - It includes a virtual
Priorityproperty (defaulting to 0) which you can override if you need to control the execution order relative to other DunGen adapters (lower priority runs first). - It defines the abstract
Generatemethod, which you must implement.
The Generate Method
This is the core of your custom adapter. DunGen automatically calls this method during its post-processing phase.
- Parameter: It receives a
Dungeonobject as input. DungeonObject: This object contains comprehensive information about the just-generated dungeon layout, including:dungeon.AllTiles: A collection of allTileinstances placed in the scene.dungeon.MainPathTiles: Tiles belonging to the main path.dungeon.BranchPathTiles: Tiles belonging to branch paths.dungeon.Connections: A collection ofDoorwayConnectionobjects detailing which doorways were connected.dungeon.Root: The root GameObject containing the generated dungeon.- ... and other useful properties about the layout.
- Timing: This method executes after all tiles, props (via
Global Props), doors (viaDoorwayConnectors), and Lock & Key elements have been placed.
Implementation Steps
- Create Script: Create a new C# script in your Unity project (e.g.,
MyCustomPathfindingAdapter.cs). - Inherit & Implement: Make the class inherit from
DunGen.Adapters.NavMeshAdapterand implement the requiredpublic override void Generate(Dungeon dungeon)method. Include the necessaryusingstatements. - Add Logic: Inside the
Generatemethod, write the code needed to interact with your specific pathfinding system's API. Use the provideddungeonobject to get information about the layout (e.g., bounds of tiles, doorway connection points). - Attach Component: Add your custom adapter script as a component to the same GameObject in your scene that has the
Runtime Dungeoncomponent. DunGen automatically detects and runs allBaseAdaptercomponents attached to this GameObject. - (Optional) Set Priority: If the execution order matters relative to other adapters (like built-in ones or other custom ones), override the
Priorityproperty in your script.
Considerations
- Performance: Be mindful that complex pathfinding calculations performed synchronously within the
Generatemethod can cause a noticeable hitch or delay when the dungeon generation completes. Consider if your pathfinding system offers asynchronous updating options that you can trigger from the adapter. - Dependencies: Ensure any necessary manager objects or setup for your pathfinding system are already present and initialized in the scene before DunGen generation starts.
- Error Handling: Implement robust error checking (e.g., checking for nulls, using try-catch blocks) within your
Generatemethod to prevent issues in your pathfinding logic from breaking the entire generation process.
By creating a custom adapter, you can seamlessly integrate virtually any pathfinding system with DunGen's powerful procedural generation capabilities.