Is it possible to merge 2D colliders?

Hello!
I am trying to set up a simple rogue-like game, and i am using a nice asset for random dungeon generation… The problem is that every tile is placed in the scene alone, even when building rooms and corridors, so i have a lot of adjacent tiles with single colliders. So i would like to know: is it possible to merge these colliders in a single one somehow?
I don’t need to have the whole dungeon walls linked with a single collider, just reduce the number of them without having one per single tile.
If anyone could help me with this issue, i would really appreciate it!

Thanks in advance,
Kae

Yes, you can read all the colliders and create a combined Polygon2D collider from the others. See PolygonCollider2D in the docs, specifically pathCount and SetPath.

private void SetMixBoxCollider2D()
{
float min_x = 999999;
float max_x = -99999;
float min_y = 999999;
float max_y = -99999;

        foreach (BoxCollider2D boxCollider in BoxColliderMix)
        {
            float new_min_x = boxCollider.bounds.min.x;
            float new_max_x = boxCollider.bounds.max.x;
            float new_min_y = boxCollider.bounds.min.y;
            float new_max_y = boxCollider.bounds.max.y;

            if (new_min_x < min_x)
            {
                min_x = new_min_x;
            }
            if (new_max_x > max_x)
            {
                max_x = new_max_x;
            }

            if (new_min_y < min_y)
            {
                min_y = new_min_y;
            }
            if (new_max_y > max_y)
            {
                max_y = new_max_y;
            }

        }
        BoxCollider2D getNewBoxCollider = gameObject.AddComponent<BoxCollider2D>();
        getNewBoxCollider.isTrigger = true;

        float Xdistance = max_x - min_x;
        float Ydistance = max_y - min_y;
        getNewBoxCollider.size = new Vector2(Xdistance, Ydistance);

        float offsetX = (Mathf.Abs(getNewBoxCollider.bounds.min.x - min_x));
        float offsetY = (Mathf.Abs(getNewBoxCollider.bounds.min.y - min_y));

        getNewBoxCollider.offset = new Vector2(offsetX, offsetY);
    }

Pick one!

  1. You cannot merge colliders. But you can use a mesh collider and them add all the dungeon walls on the mesh (procedurally generated). (Quite hard)

  2. Create script to SendMessageUpwards just to resend OnCollision and OnTrigger events, put your script that need this calls on a GameObject and make sure that all walls is a parent of this same GO.

  3. Do the same as described in 2 but using static functions, instead of SendMessageUpwards, to avoid the need of all walls been parent of the same GO.