Welcome! Share code as fast as possible.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class OriginShiftMaze : MonoBehaviour
{
    [Header("Maze Settings")]
    public int width = 10;
    public int height = 10;

    [Header("Generation Mode")]
    public bool regenArea = false;
    public bool randomOrigin = false;
    public bool randomRadius = false;
    public int centerX = 0;
    public int centerY = 0;
    public int radius = 1;

    [Header("Prefabs")]
    public GameObject wallPrefab;
    public GameObject spherePrefab;

    enum WallDir { Right, Top }

    private Dictionary<(Vector2Int, WallDir), GameObject> walls = new();
    private Dictionary<(Vector2Int, WallDir), bool> wallsToUpdate = new();
    private Stack<Cell> stack = new();
    private bool[,] visited;
    private int startX = 0;
    private int startY = 0;

    private struct Cell
    {
        public int x;
        public int y;

        public Cell(int _x, int _y)
        {
            x = _x;
            y = _y;
        }
    }

    private void Start()
    {
        InitWalls();
        GenerateMaze();
       UpdateWallVisibility();

    }

    private void Update()
    {

    }

    private void InitWalls()
    {
        Vector3 centerOffset = new Vector3(width * 0.5f, 0, height * 0.5f);

        //Place borders(outer walls)
        for (int x = 0; x < width; x++)
        {
            Vector3 bottom = new Vector3(x + 0.5f, 1, 0) - centerOffset;
            Vector3 top = new Vector3(x + 0.5f, 1, height) - centerOffset;
            Instantiate(wallPrefab, bottom, Quaternion.identity, transform);
            Instantiate(wallPrefab, top, Quaternion.identity, transform);
        }

        for (int y = 0; y < height; y++)
        {
            Vector3 left = new Vector3(0, 1, y + 0.5f) - centerOffset;
            Vector3 right = new Vector3(width, 1, y + 0.5f) - centerOffset;
            Instantiate(wallPrefab, left, Quaternion.Euler(0, 90, 0), transform);
            Instantiate(wallPrefab, right, Quaternion.Euler(0, 90, 0), transform);
        }

        // Right walls
        for (int x = 0; x < width - 1; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Vector3 pos = new Vector3(x + 1, 1, y + 0.5f) - centerOffset;
                GameObject wall = Instantiate(wallPrefab, pos, Quaternion.Euler(0, 90, 0), transform);
                wall.SetActive(true);

                var wallKey = (new Vector2Int(x, y), WallDir.Right);
                walls[wallKey] = wall;
                wallsToUpdate[wallKey] = true;
            }
        }

        // Top walls
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height - 1; y++)
            {
                Vector3 pos = new Vector3(x + 0.5f, 1, y + 1) - centerOffset;
                GameObject wall = Instantiate(wallPrefab, pos, Quaternion.identity, transform);
                wall.SetActive(true);

                var wallKey = (new Vector2Int(x, y), WallDir.Top);
                walls[wallKey] = wall;
                wallsToUpdate[wallKey] = true;
            }
        }
    }

    private void GenerateMaze()
    {
        ResetGenerationState();

        while (stack.Count > 0)
        {
            Cell current = stack.Pop();
            List<Cell> unvisitedNeighbors = GetUnvisitedNeighbors(current);

            if (unvisitedNeighbors.Count > 0)
            {
                stack.Push(current);
                Cell chosen = unvisitedNeighbors[UnityEngine.Random.Range(0, unvisitedNeighbors.Count)];
                visited[chosen.x, chosen.y] = true;
                stack.Push(chosen);

                RemoveWallBetween(current, chosen);
            }
        }
    }

    private List<Cell> GetUnvisitedNeighbors(Cell cell)
    {
        List<Cell> neighbors = new List<Cell>();
        Vector2Int[] directions = {
            new Vector2Int(0, 1), new Vector2Int(1, 0),
            new Vector2Int(0, -1), new Vector2Int(-1, 0)
        };

        foreach (var dir in directions)
        {
            int nx = cell.x + dir.x;
            int ny = cell.y + dir.y;
            if (nx >= 0 && ny >= 0 && nx < width && ny < height && !visited[nx, ny])
            {
                neighbors.Add(new Cell(nx, ny));
            }
        }
        return neighbors;
    }

    private void RemoveWallBetween(Cell current, Cell next)
    {
        int dx = next.x - current.x;
        int dy = next.y - current.y;

        if (dx == 1) // Right
        {
            wallsToUpdate[(new Vector2Int(current.x, current.y), WallDir.Right)] = false;
        }
        else if (dx == -1) // Left
        {
            wallsToUpdate[(new Vector2Int(next.x, next.y), WallDir.Right)] = false;
        }
        else if (dy == 1) // Up
        {
            wallsToUpdate[(new Vector2Int(current.x, current.y), WallDir.Top)] = false;
        }
        else if (dy == -1) // Down
        {
            wallsToUpdate[(new Vector2Int(next.x, next.y), WallDir.Top)] = false;
        }
    }

    private void UpdateWallVisibility()
    {
        foreach (var entry in wallsToUpdate)
        {
            (Vector2Int position, WallDir direction) = entry.Key;
            bool shouldBeVisible = entry.Value;
            if (walls.ContainsKey((position, direction)))
            {
                GameObject wallObj = walls[(position, direction)];
                if (wallObj != null)
                {
                    wallObj.SetActive(shouldBeVisible);
                }
            }
        }
    }

    private void ResetGenerationState()
    {
        startX = UnityEngine.Random.Range(0, width);
        startY = UnityEngine.Random.Range(0, height);

        visited = new bool[width, height];
        stack.Clear();
        stack.Push(new Cell(startX, startY));
        visited[startX, startY] = true;
    }

}