たとえば、インスペクタでは、幅と高さの値を10に設定します。グリッドは10x10にする必要がありますが、ゲームを実行しているときは、インスペクタの幅と高さの値を11に11に変更します。シーンビューを見て、幅と高さを数えると、それらは両方とも9です。9x9グリッドと同じように、10x10ではなく11x11である。なぜ迷路を生成するとき、グリッドは実際に10x10サイズではありませんか?
その後、新しい迷路を生成するために作成したボタンをクリックすると、幅と高さの両方を5に設定するとグリッドサイズは4x4になり、
何らかの理由で、値iは幅と高さを入力して1つ減らします。そして、私は値の大きさでそれを作りたいと思います。たとえば、10x10なので、グリッドは10x10になります。
これは、ゲームの実行中のスクリーンショットです。インスペクタの右側にある値を5と5に変更してボタンをクリックして見ることができます。しかし、シーンビューで、あなたは、グリッドのサイズを数えることができます:それは4x4のそれが唯一の迷路見せて
だが、あなたは想像とグリッドがどうあるべきかカウントし、それが5x5のが、4×4
ではありません見ることができますこれは5x5の別のスクリーンショットです。今回はグリッドが3x3のサイズのように見えるかもしれません。どういう仕組みでどのように動作するのか分かりません。
これは私が作るしようとしているチュートリアルへのリンクです:
これは私がこれまで使用していたスクリプトです:
迷路クラスのすべての設定と生成:
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 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() {
}
}
迷路ジェネレータクラスである
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;
}
}
をゲームを実行しているときに結果に合わない。迷路は問題ありませんが、グリッドは10x10または5x5または6x6のようには見えません
たとえば、ランダムな迷路だけでなく完成したグリッドを描画したい場合、どうすればそのサイズになるのですか?
また、私はそれがどのように動作しているのか理解できず、動作しているはずです。
これはコード化したものです。 MazeGeneratorを参照してください。開始メソッド –
@SirRufo私はそれを見ました。テストのために私がmazeWidth ++を使用していない場合でも。 mazeHeight ++;と私はライン迷路=新しい迷路(mazeWidth、mazeHeight、mazeRG)でそれを参照してください。 mazeWidthとmazeHeightの両方の値が5です。グリッドはまだ3x3として作成されます –
デバッガを使用して実際に使用されている迷路のサイズを調べます。画面上の迷路の表示から実際に使用された迷路の大きさを判断することは不可能です。 –