Explore the code of the Dungeon Graph Generator sample
Overview
The sample’s scripts are mainly split between the dungeon graph tool’s definition (located in the DungeonGraph folder) and the dungeon graph generator (located in the DungeonGraphGeneration folder).
The dungeon graph nodes are slightly customized; nodes use custom default colors and titles, and the ports that represent paths in the dungeon are implemented with a vertical orientation (using IPortBuilder.AsVertical()). For a more in depth example of node customization, check out the Visual Novel Director sample.
The DungeonGraphGenerationSheet scriptable object has its own custom inspector (DungeonGraphGenerationSheetInspector). It is a simple inspector that adds a Generate button at the end of the default inspector view. Clicking this button calls DungeonGraphGenerator.Generate().
Dungeon Graph Generator
The flow of the DungeonGraphGenerator is as follows:
- Validate the data from the generation sheet; check that
GameObjectsare notnulland that number values are within the right range. - Randomly determine the layout of the dungeon’s rooms; pick the number of rooms per depth and trace the paths between rooms of neighboring depths.
- Create a graph asset that corresponds to the above layout.
The generator also showcases a simple algorithm for positioning your nodes on your graph (see DungeonGraphGenerator.CreateDungeonGraph()).
Opening a window for the generated graph asset
Regardless of whether a dungeon graph asset already exists or not with the dungeon graph name you provided in the generation sheet, the (re)generated asset is always seamlessly immediately opened in a graph tool editor window. If a window is already opened for your dungeon graph, the window will be instantly refreshed and put on focus when your dungeon graph is regenerated. Otherwise, the generator opens a new window and gives it focus.
The generator opens the graph tool editor window with AssetDatabase.OpenAsset(). This is sufficient when creating a new graph, however if you are overwriting a graph that is already opened in a graph tool editor window, you will need to refresh your graph. Graph Toolkit’s undo API automatically handles refreshiing a graph, so wrapping your graph modification code with calls to Graph.UndoBeingRecordGraph() and Graph.UndoEndRecordGraph() updates the window of your overwritten graph with your modifications.
DungeonGraph also contains a ClearGraph helper method for clearing a dungeon graph from all its nodes and variables which is useful when you overwrite a graph. You can only access all the nodes and variables of a graph through IEnumerable. This means that removing all the nodes and variables in your graph requires a memory allocation otherwise you would be iterating on a collection that was modified during the iteration (which is an invalid operation). Reallocating memory every time a graph needs to be cleared is not efficient however so ClearGraph uses ListPool<T> for temporarily storing the nodes and variables that need to be removed.