Need help with incorporating 2D dungeon algorithm into Unity.

So, I have went through dozens of the answers already in the forum and watched every tutorial I can locate. I can’t seem to get my Unity to do what it does in the videos. I’ve landed on the conclusion that its either me failing miserably or something to do with the different versions that the person and I am using.

In Visual Studio, I have all the code to make an old school *, &, @, # kind of dungeon but no idea how to combine the two. The way I have it now, it makes a blank board of a given character, then iterates through the blank board generating random rooms, checking to make sure they fit, and if they do, iterate back through and create the room. Super Basic.

        //Creating a blank board 

        for (int i = x - 1; i >= 0; i--)
        {
            for (int j = y - 1; j >= 0; j--)
            {
                board[i, j] = new Tile("▓", ConsoleColor.DarkGreen);
            }

        }

        //Gathers room sizes for checking
        for (int i = 0; i <= 11; i++)
        {
            int roomX = StaticRandom.Instance.Next(1, height -2);
            int roomHeight = StaticRandom.Instance.Next(roomX +1, height - 2);

            int roomY = StaticRandom.Instance.Next(1, length - 2);
            int roomLength = StaticRandom.Instance.Next(roomY +1, length - 1);

            bool result = CheckRoom(roomX, roomY, roomHeight, roomLength);

            if (result == false)
            {
                i--; continue;
            }
            else
            {
                MakeRoom(roomX, roomY, roomHeight, roomLength);
            }
        }
    }

    public bool CheckRoom(int x, int y, int h, int l)
    {
        for (int i = x; i <= h; i++) {

            for (int j = y; j <= l; j++)
            {
                if (board[i, j].symbol != "▓")
                {
                    return false;
                }
            }
        }

        return true;
    }

    //iterate through the blank board and changes spaces to create 'rooms'
    public void MakeRoom(int x, int y, int h, int l)
    {   int midX = (x + h) / 2;
        int midY = (l + y) / 2;
        

        for (int i = x; i <= h; i++)
        {
            for (int j = y; j <= l; j++)
            {
                board[i, j] = new Tile("░", ConsoleColor.DarkGray);
                
                
            }
        }
        midPoints.Add(new int[] { midX, midY });
    }

I’ll admit that its incredibly possible that this question has been answered, but there are SO many answers and I didn’t have any luck going through them.

It sounds like you just want to add a graphics layer to an already existing code base.

Given you have some classes already created for this purpose, you could generate graphical tiles based on your Ascii tiles.

It sounds like it boils down to two questions, at least for getting graphics going.

  • How will you render one tile?

You could create a game object per tile if you wanted, or if you want, you could generate one mesh with all of them if your tiles are atlased.

  • How will you map one ascii tile to a texture?

This is pretty straight forward. For any given character you have, bind data to that character that can represent your graphical image. It could be a loose texture, or a texture and texture coordinates.

So you would write a class that takes a tilemap as input, and spits out a mesh or lots of gameobjects as output.


Example

So I took the time to write up an example on how to do with generating a single mesh.

Ascii folder

Contains code regarding Ascii tilemap, a simple procedural ascii tile generator. Think of this as the system you already likely have in place.

Bridge folder

Contains code regarding converting Ascii into Sprite tiles. It is the glue which separates Ascii from Sprite. Sprite knows nothing about Ascii and Ascii knows nothing about Sprite. They are totally separated. Bridge is the glue between them.

Sprite folder

Contains the definition for SpriteTile and code to generate the mesh based on a SpriteTilemap. Note that SpriteTilemap is an interface, as such there is actually no code written specially to handle a tilemap of sprites. Instead, I use the bridge, which uses ascii.

Answer.cs

Well, cheeky file name apart, this is the entry point you should look at.

The code is very sparsly commented, so if you have questions, please drop a comment and I’ll try to explain better and update the answer. If you get confused about the mesh generation, I’d recommend that you check out the Mesh class or reading up in general about how meshes operate. I can also provide a simpler implementation which creates lots and lots of game objects instead if you want. I need some time to sort out a proper github repo for all answers threads for that to happen though.

Note: The SpriteMeshBuilder is providing vertex colors, but the material I used does not use vertex colors. You’ll need to use a shader which uses vertex color to see any color effects!