Unity Procedural Level Generation

I was unhappy with the assets and approaches I was finding for Unity procedural level generation. I had read about some developers taking prefab rooms and generating maps, but I couldn’t find any examples. However, I didn’t want the Binding of Isaac or Zelda dungeon feel. I wanted to be able t customize rooms, then generate a dungeon based on thematic prefab rooms.

Here’s the result

So whats going on here?

We’ll step through some pseudo code.


Spawn a 4-connection point room at a random x, y coordinate on our map array.

  • Store those 4 connection transforms in a list.
    • I also stored the connection transforms as keys to a dictionary of gameObjects, this way when I pulled a random transform, I look it up in the dictionary, get the gameObject.
    • Set all four of these connection points as available for connection, I just used a bool value on the prefab object to keep track of this.


  • Grab a random key from our available connection list
    • Grab the associated gameobject using the transform as a key for our dictionary of gameObjects
    • Using overlap collider, I check if there is an object present at my random key
      • Iterate over the bool values and find a connection point
      • Instantiate our prefab at the destination transform and apply an offset, so it lines up nicely with the existing gameObject
      • Set some connection flags
      • Rinse -> Repeat

Once we reach the number of rooms we want to spawn. We then initiate a cleanup phase.


  • This method iterates over all the spawned hallways.
  • It evaluates if the hallway has an open connection.
  • If we find an open connection, we attempt to spawn a large 8×8 endcap, if that fails, we spawn a 4×4 endcap, if that fails, we use a 2×2 endcap.

I ran into some issues here with detecting overlap and it resulted in a better generator in my opinion. So, the prefabs were built on entirely the same layer. So, I added another layer just for the floor, now when I get weird overlapping issues, its cutting into wall space and creating some interesting room configurations.

  • The intersected wall objects are removed.

I know this post probably isn’t for everybody, but I wanted to show some of my work and my nontraditional way of tacking level generation. I could probably add some more cleanup to remove oddities such as low value tiles spread throughout the center of the map. This is a good base I believe.

Leave a Reply