Untitled

                Never    
C#
       
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MazeGen : MonoBehaviour
{
    [SerializeField]
    int n;

    [SerializeField]
    GameObject tileWhitePrefab;
    [SerializeField]
    GameObject tileBlackPrefab;

    GameObject[,] tiles = null;

    readonly Vector2Int TILE_NONE = new Vector2Int(-1, -1);

    readonly Vector2Int TILE_LEFT = new Vector2Int(-1, 0);
    readonly Vector2Int TILE_RIGHT = new Vector2Int(1, 0);
    readonly Vector2Int TILE_DOWN = new Vector2Int(0, -1);
    readonly Vector2Int TILE_UP = new Vector2Int(0, 1);

    bool IsTileValid(Vector2Int tile)
    {
        return 0 <= tile.x && tile.x < n && 0 <= tile.y && tile.y < n;
    }

    int Distance(Vector2Int tile1, Vector2Int tile2)
    {
        return Mathf.Abs(tile2.x - tile1.x) + Mathf.Abs(tile2.y - tile1.y);
    }

    List<Vector2Int> GenerateMainPath(Vector2Int start, Vector2Int door)
    {
        bool[,] visited = new bool[n, n];
        Vector2Int[,] prev = new Vector2Int[n, n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                visited[i, j] = false;
                prev[i, j] = TILE_NONE;
            }
        }

        Queue<Vector2Int> queue = new Queue<Vector2Int>();
        queue.Enqueue(start);
        while (queue.Count > 0) {
            Vector2Int tile = queue.Dequeue();
            Vector2Int left = tile + TILE_LEFT;
            if (IsTileValid(left)) {
                if (!visited[left.x, left.y]) {
                    queue.Enqueue(left);
                }
            }
            Vector2Int right = tile + TILE_RIGHT;
            Vector2Int up = tile + TILE_UP;
            Vector2Int down = tile + TILE_DOWN;
        }

        List<Vector2Int> path = new List<Vector2Int>();

        return path;
    }

    void GenerateMaze()
    {
        if (tiles != null) {
            for (int i = 0; i < tiles.GetLength(0); i++) {
                for (int j = 0; j < tiles.GetLength(1); j++) {
                    Destroy(tiles[i, j]);
                }
            }
        }
        tiles = new GameObject[n, n];

        // probably not uniform random, whatevs
        // (biased to corners? but side is predetermined by prev maze door)
        int side = Random.Range(0, 4); // 0 to 3: bottom, top, left, right
        int tileInd = Random.Range(0, n);

        Vector2Int start = Vector2Int.zero;
        if (side == 0) {
            start.x = tileInd;
            start.y = 0;
        }
        else if (side == 1) {
            start.x = tileInd;
            start.y = n - 1;
        }
        else if (side == 2) {
            start.x = 0;
            start.y = tileInd;
        }
        else {
            start.x = n - 1;
            start.y = tileInd;
        }

        // pick door
        List<Vector2Int> choices = new List<Vector2Int>(); // ugh
        for (int i = 0; i < n; i++) {
            Vector2Int tile = new Vector2Int(i, 0);
            if (Distance(start, tile) >= n) {
                choices.Add(tile);
            }
        }
        for (int i = 0; i < n; i++) {
            Vector2Int tile = new Vector2Int(i, n - 1);
            if (Distance(start, tile) >= n) {
                choices.Add(tile);
            }
        }
        for (int j = 1; j < n - 1; j++) {
            Vector2Int tile = new Vector2Int(0, j);
            if (Distance(start, tile) >= n) {
                choices.Add(tile);
            }
        }
        for (int j = 1; j < n - 1; j++) {
            Vector2Int tile = new Vector2Int(n - 1, j);
            if (Distance(start, tile) >= n) {
                choices.Add(tile);
            }
        }
        Vector2Int door = choices[Random.Range(0, choices.Count)];

        List<Vector2Int> mainPath = GenerateMainPath(start, door);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                Vector3 pos = new Vector3((float)i, 0.0f, (float)j);
                Quaternion rot = tileWhitePrefab.transform.rotation;
                if (i == start.x && j == start.y) {
                    tiles[i, j] = Instantiate(tileWhitePrefab, pos, rot, transform);
                }
                else if (i == doorTile.x && j == doorTile.y) {
                    tiles[i, j] = Instantiate(tileWhitePrefab, pos, rot, transform);
                }
                else {
                    tiles[i, j] = Instantiate(tileBlackPrefab, pos, rot, transform);
                }
            }
        }
    }

	void Start()
    {
        GenerateMaze();
    }
	
	void Update()
    {
        if (Input.GetKeyDown(KeyCode.G)) {
            GenerateMaze();
        }
	}
}

Raw Text