2017-12-17 14 views
-2

예를 들어 Inspector에서 너비와 높이 값을 10으로 설정합니다. 따라서 그리드가 10x10이어야합니다. 그러나 게임을 실행하면 관리자의 너비와 높이 값이 11로 11에서 11로 변경됩니다. 그리고 다음 장면보기를보고 너비와 높이를 계산하면 둘 다 9입니다. 9x9 그리드처럼 10x10이 아니라 11x11이 좋습니다.미로를 생성 할 때 그리드가 실제로 10x10 크기가 아닌 이유는 무엇입니까?

나중에 내가 새로운 미로를 생성하기 위해 만든 버튼을 클릭하면 그때마다 너비와 높이를 모두 5로 설정하면 그리드 크기가 4x4가 될 것이고 만약 내가 둘 다 6x6으로 설정한다면 그리드 크기는 5x5가 될 것입니다

너비와 높이에 대한 값을 입력하면 값이 1 씩 줄어 듭니다. 그리고 저는 그것을 가치의 크기로 만들고 싶습니다. 예를 들어 10x10이므로 그리드는 10x10이됩니다.

게임이 실행되는 동안의 스크린 샷입니다. 검사관의 오른쪽에서 5와 5로 값을 변경하고 버튼을 클릭 한 것을 볼 수 있습니다. 그러나 장면보기에 당신은 격자의 크기를 계산 할 수는 4 × 4 그것은 단지 미로를 보여주는 것

입니다하지만 당신은 상상 그리드 어떻게해야 계산하고 5 × 5 만 4 × 4

Grid

아니다 볼 수 있습니다

이것은 5x5의 또 다른 스크린 샷입니다. 이번에는 그리드가 3x3 크기처럼 보일 것입니다. 어떻게 작동하는지, 어떻게 작동하는지 이해하지 못할 수도 있습니다.

미로 클래스를위한 :

Maze tutorial

이것은 내가 지금까지 사용하고 스크립트입니다 :

Grid

은 내가 만들려고 튜토리얼에 대한 링크입니다 모든 설정 및 생성 :

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; 
    } 
} 

이이

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(); 
     } 
    } 
} 

내 문제 @ 버튼 스크립트가

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, 0.5f, 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() { 

    } 
} 

미로 생성기 클래스입니다 것은 격자 크기가 게임을 실행할 때 결과에 ​​맞지 않습니다. 미로는 괜찮지 만 그리드가 10x10 또는 5x5 또는 6x6 인 것 같지 않습니다.

예를 들어 임의의 미로뿐만 아니라 완성 된 그리기를 그리려면 어떻게해야하고 크기는 어떻게 될까요?

어쩌면 내가 어떻게 작동하고 작동해야하는지 이해하지 못했을 것입니다.

+0

글쎄, 그것은 당신이 코딩 한 것을합니다. MazeGenerator를 참조하십시오. 시작 메서드 –

+0

@SirRufo i saw it. 테스트 시작을 위해 mazeWidth ++를 사용하지 않아도; 그리고 mazeHeight ++; 그리고 나는 미로 = 새로운 미로 (mazeWidth, mazeHeight, mazeRG) 라인에서 그것을 본다; mazeWidth 및 mazeHeight 값은 모두 5입니다. 그리드는 여전히 3x3으로 생성됩니다. –

+0

디버거를 사용하여 실제로 사용되는 미로 크기를 확인합니다. –

답변

0

MazeGenerator.Start() 함수는 크기를 늘리면 짝수 차원을 홀수로 만듭니다.

그러면 미로가 반전됩니다. 실제 값이있는 셀은 비어 있어야하며 잘못된 값을 가진 셀은 벽으로 채워야합니다.