그러나 게임이 실행되는 동안 나는 그것을했다. 나는 그것이 미로를 만들 때 그렇게 되길 바래.
이 모든 설정 및 생성을위한 미로 클래스입니다 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Maze
{
//Grid size
public int width;
public int height;
//Store grid
private bool[,] grid;
//Generate random directions to move
private System.Random rg;
//Start position
int startX;
int startY;
//Public getter
public bool[,] Grid
{
get { return grid; }
}
//Constructor of the grid for setting values
public Maze(int width, int height, System.Random rg)
{
this.width = width;
this.height = height;
this.rg = rg;
}
//Generate the grid
public void Generate()
{
grid = new bool[width, height];
startX = 1;
startY = 1;
grid[startX, startY] = true;
MazeDigger(startX, startY);
}
void MazeDigger(int x, int y)
{
int[] directions = new int[] { 1, 2, 3, 4 };
//We create random array of directions
HelpingTools.Shuffle(directions, rg);
//We are looping over all the directions
for (int i = 0; i < directions.Length; i++)
{
if (directions[i] == 1)
{
if (y - 2 <= 0)
continue;
if (grid[x, y - 2] == false)
{
grid[x, y - 2] = true;
grid[x, y - 1] = true;
MazeDigger(x, y - 2);
}
}
if (directions[i] == 2)
{
if (x - 2 <= 0)
continue;
if (grid[x - 2, y] == false)
{
grid[x - 2, y] = true;
grid[x - 1, y] = true;
MazeDigger(x - 2, y);
}
}
if (directions[i] == 3)
{
if (x + 2 >= width - 1)
continue;
if (grid[x + 2, y] == false)
{
grid[x + 2, y] = true;
grid[x + 1, y] = true;
MazeDigger(x + 2, y);
}
}
if (directions[i] == 4)
{
if (y + 2 >= height - 1)
continue;
if (grid[x, y + 2] == false)
{
grid[x, y + 2] = true;
grid[x, y + 1] = true;
MazeDigger(x, y + 2);
}
}
}
}
}
을 그리고 이것은 임의의 미로의 배열을 만들 클래스 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelpingTools : MonoBehaviour
{
public static T[] Shuffle<T>(T[] array, System.Random rg)
{
for (int i = 0; i < array.Length - 1; i++)
{
int randomIndex = rg.Next(i, array.Length);
T tempItem = array[randomIndex];
array[randomIndex] = array[i];
array[i] = tempItem;
}
return array;
}
}
을 그리고 이것은 내가 그리는 방법입니다 maze : CreateMaze 메서드 안의 X에서 회전을 90 씩 인스턴스화 할 때 각 큐브를 변경할 수 있지만 좋은 해결책인지 확실하지 않습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator : MonoBehaviour
{
public Maze maze;
public int mazeWidth;
public int mazeHeight;
public string mazeSeed;
public GameObject wallPrefab;
private GameObject wall;
private GameObject wallCorner;
private System.Random mazeRG;
private GameObject[] bricks;
// Use this for initialization
void Start()
{
mazeRG = new System.Random();
if (mazeWidth % 2 == 0)
mazeWidth++;
if (mazeHeight % 2 == 0)
{
mazeHeight++;
}
maze = new Maze(mazeWidth, mazeHeight, mazeRG);
GenerateMaze();
}
public void GenerateMaze()
{
bricks = GameObject.FindGameObjectsWithTag("MazeBrick");
if (bricks.Length > 0)
DestroyMaze();
maze.Generate();
DrawMaze();
}
private void DestroyMaze()
{
for(int i = 0; i < bricks.Length; i++)
{
DestroyImmediate(bricks[i]);
}
}
void DrawMaze()
{
for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, y);
if (maze.Grid[x, y] == true)
{
CreateMaze(position, transform, 0, mazeRG.Next(0, 3) * 90);
}
}
}
}
void CreateMaze(Vector3 position, Transform parent, int sortingOrder, float rotation)
{
GameObject mazePrefab = Instantiate(wallPrefab, position, Quaternion.identity);
mazePrefab.transform.SetParent(parent);
mazePrefab.transform.Rotate(0, 0, rotation);
mazePrefab.tag = "MazeBrick";
}
// Update is called once per frame
void Update() {
}
}
,
그리고 관리자에서 버튼에 스크립트를 지속 :
: 게임 내가는 MazeGeneratore 스크립트 내부에 새로운 미로를 생성 년대 관리자에서 버튼을 클릭 해요마다 실행되는 동안using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MazeGenerator))]
public class GenerateButton : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
MazeGenerator myScript = (MazeGenerator)target;
if (GUILayout.Button("Generate Maze"))
{
myScript.GenerateMaze();
}
}
}
public void GenerateMaze()
{
bricks = GameObject.FindGameObjectsWithTag("MazeBrick");
if (bricks.Length > 0)
DestroyMaze();
maze.Generate();
DrawMaze();
}
하지만 다시 등장하는 새로운 미로가 생성됩니다.
이 잘 일하고있어 문제는 게임 시작과 내 질문에 언급 깜빡 나는 MazeGenerator 스크립트 안에 GenerateMaze() 메서드가 있습니다. 이 메소드는 다른 스크립트의 버튼에서 호출합니다. 버튼은 관리자가 실행 중이므로 버튼을 클릭하면 새로운 미로가 다시 나타날 때마다 새로운 미로를 생성합니다. –
아! 'transform.localRotation = Quaternion.Euler (90, 0, 0)'을'GenerateMaze' 메소드의 끝 부분에 추가하기 만하면됩니다. 그래도 작동하지 않는다면'transform.localRotation = Quaternion '을 실행 해 보겠습니다.'GenerateMaze'가 호출 된 후에 부모 객체의 Euler (90, 0, 0)'이 호출됩니다. –
버튼 코드와 문제로 제 질문을 업데이트했습니다. 메서드의 맨 아래에 GenerateMaze 메서드 내부에 localRotation 행을 추가하려고했지만 여전히 새로운 서있는 미로를 만들고 있습니다. 다시 회전시키지 않습니다. –