源码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEditor;
public class MazeCreate : MonoBehaviour
{
public Tilemap tilemap; //在哪个Tilemap绘制
public RuleTile ruleTile; //用哪个瓦片绘制,可用普通的TileBase替代
public int row; //迷宫的行
public int col; //迷宫的列
private int TotalPointsCount;
private int pointsCount;
public Vector2Int startPoint; //迷宫的起点坐标
public Vector2Int endPoint; //迷宫的终点坐标
public List<List<int>> mapList; //迷宫的二维数组
enum Direction
{
up = 1,
right,
down,
left
}
//0代表未标记点
//1代表墙
//2代表已标记点
private void Start()
{
TotalPointsCount = row * col;
row = row * 2 + 1;
col = col * 2 + 1;
mapList = new List<List<int>>();
for (int i = 0; i < row; i++)
{
mapList.Add(new List<int>());
for (int j = 0; j < col; j++)
{
if (i % 2 == 0 || j % 2 == 0)
{
mapList[i].Add(1);
}
else
{
mapList[i].Add(0);
}
}
}
pointsCount = 1;
mapList[startPoint.x][startPoint.y] = 2;
DFS(startPoint);
DrawTilemap();
}
private void DFS(Vector2Int currentPoint)
{
if (pointsCount == TotalPointsCount || currentPoint == endPoint)
{
return;
}
List<int> accessDir = new List<int>();
while (true)
{
HasAccess(accessDir, currentPoint); //获得当前点可向哪几个方向扩展
if (accessDir.Count > 0)
{
int randomIndex = Random.Range(0, accessDir.Count); // 随机选择一个方向
Vector2Int nextPoint = OpenUp(currentPoint, (Direction)accessDir[randomIndex]); //打通墙
pointsCount++;
accessDir.RemoveAt(randomIndex);
DFS(nextPoint);
}
else
{
break;
}
}
}
private void HasAccess(List<int> dirList, Vector2Int currentPoint)
{
dirList.Clear();
for (int i = (int)Direction.up; i <= (int)Direction.left; i++)
{
Vector2Int neighborPoint = GetNeighborPoint(currentPoint, (Direction)i);
if (neighborPoint.x > 0 && neighborPoint.y > 0 &&
neighborPoint.x < row && neighborPoint.y < col &&
mapList[neighborPoint.x][neighborPoint.y] == 0)
{
dirList.Add(i);
}
}
}
private Vector2Int GetNeighborPoint(Vector2Int currentPoint, Direction direction)
{
switch (direction)
{
case Direction.up:
return new Vector2Int(currentPoint.x, currentPoint.y - 2);
case Direction.right:
return new Vector2Int(currentPoint.x + 2, currentPoint.y);
case Direction.down:
return new Vector2Int(currentPoint.x, currentPoint.y + 2);
case Direction.left:
return new Vector2Int(currentPoint.x - 2, currentPoint.y);
default:
return new Vector2Int(0, 0);
}
}
private Vector2Int OpenUp(Vector2Int currentPoint, Direction direction)
{
Vector2Int nextPoint = GetNeighborPoint(currentPoint, direction);
mapList[nextPoint.x][nextPoint.y] = 2;
switch (direction)
{
case Direction.up:
mapList[currentPoint.x][currentPoint.y - 1] = 2;
break;
case Direction.right:
mapList[currentPoint.x + 1][currentPoint.y] = 2;
break;
case Direction.down:
mapList[currentPoint.x][currentPoint.y + 1] = 2;
break;
case Direction.left:
mapList[currentPoint.x - 1][currentPoint.y] = 2;
break;
default:
break;
}
return nextPoint;
}
private void DrawTilemap()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (mapList[i][j] == 1)
{
tilemap.SetTile(new Vector3Int(i, j, 0), ruleTile);
}
}
}
}
}
最终效果
有什么问题可以问,我会及时回答的。