只有当我取消注释方法时,Unity才会在播放时停止响应

时间:2016-11-17 01:36:20

标签: unity3d playback

当我取消注释该行" ConnectMaze(迷宫)"在Start()方法中,当我点击播放时,Unity会立即停止响应。 Debug.Log语句都不会出现在控制台中。如果我注释掉那个特定的行,我可以在控制台中看到Debug.Log语句,Unity确实会响应。我认为ConnectMaze while循环中可能存在无限循环,但是循环之前的Debug.Log语句是否仍会出现在控制台中?以下是导致问题的代码。

using UnityEngine;
using System.Collections;

public class MazeBase : MonoBehaviour {

    public const int mazeLength = 11;
    private int[,] maze = new int[mazeLength,mazeLength];
    private int numWalls;
    // Use this for initialization

    void Start () {
        Debug.Log ("Initialize Start");
        InitializeMaze (maze);
        Debug.Log ("Initialize End");
        Debug.Log ("Connectivity Start");
        ConnectMaze (maze);
        Debug.Log ("Connectivity End");
        MazeGenerator.Generate (transform,maze);
    }

    // Update is called once per frame
    void Update () {

    }
    private void CreateMazeBlock(){
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 10;
        Vector3 cameraPos = Camera.main.ScreenToWorldPoint (mousePos);
        int cameraPosX = (int)Mathf.Round (cameraPos.x);
        int cameraPosZ = (int)Mathf.Round (cameraPos.z);
        if (maze [cameraPosX, cameraPosZ] != 1) {
            maze [cameraPosX, cameraPosZ] = 1;
            MazeGenerator.GenerateSingleBlock (transform, cameraPosX, cameraPosZ);
        }
    }

    private void DeleteMazeBlock(){
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 10;
        Vector3 cameraPos = Camera.main.ScreenToWorldPoint (mousePos);
        int cameraPosX = (int)Mathf.Round (cameraPos.x);
        int cameraPosZ = (int)Mathf.Round (cameraPos.z);
        if (maze [cameraPosX, cameraPosZ] == 1) {
            maze [cameraPosX, cameraPosZ] = 0;
            MazeGenerator.DeleteSingleBlock (cameraPosX, cameraPosZ);
        }
    }
    private void InitializeMaze(int[,] maze){
        for (int i = 0; i < mazeLength; i++) {
            for (int j = 0; j < mazeLength; j++) {
                if (i == 0 || i == mazeLength - 1 || j == 0 || j == mazeLength - 1) {
                    maze [i, j] = -1;
                    numWalls++;
                } else if (Random.Range (0, 100) < 50) {
                    bool createWall = true;
                    //Following if statements prevent a 2x2 square of walls
                    if (IsWall (maze, i - 1, j - 1) && IsWall (maze, i - 1, j) && IsWall (maze, i, j - 1)) {
                        createWall = false;
                    }else if (IsWall (maze, i - 1, j + 1) && IsWall (maze, i - 1, j) && IsWall (maze, i, j + 1)) {
                        createWall = false;
                    }else if (IsWall (maze, i + 1, j - 1) && IsWall (maze, i + 1, j) && IsWall (maze, i, j - 1)) {
                        createWall = false;
                    }else if (IsWall (maze, i + 1, j + 1) && IsWall (maze, i + 1, j) && IsWall (maze, i, j + 1)) {
                        createWall = false;
                    }
                    if(createWall){
                        maze [i, j] = -1;
                        numWalls++;
                    }
                }
            }
        }

    }
    private bool IsWall(int[,] maze, int i, int j){
        return maze [i, j] < 0;
    }
    private void ConnectMaze(int[,] maze){
        Debug.Log ("GetNextTile Start");
        ArrayList startTile = GetNextTile (maze,0);
        Debug.Log ("GetNextTile End");
        int startX = (int)startTile[0];
        int startZ = (int)startTile[1];
        int numberOfFloods = 1;
        Debug.Log ("Flood Fill 1 Start");
        int numFound = FloodFill(maze,startX,startZ,numberOfFloods);
        Debug.Log ("Flood Fill 1 End");
        while (numFound != (mazeLength * mazeLength) - numWalls) {
            ArrayList nextWallTile = GetNextTile (maze, 1);
            int wallX = (int)nextWallTile[0];
            int wallZ = (int)nextWallTile[1];
            maze [wallX, wallZ] = 0;
            numWalls--;
            numberOfFloods++;
            int numFound2 = FloodFill(maze,startX,startZ,numberOfFloods);
            if(numFound2 == (mazeLength*mazeLength)-numWalls){
                break;
            }
            if (numFound2 - numFound > 1) {
                numFound = numFound2;
            } else {
                maze [wallX, wallZ] = 2;
                numWalls++;
            }
            int temp = (mazeLength * mazeLength) - numWalls;
            Debug.Log ("Number of empty tiles: " + temp);
            Debug.Log ("Number of tiles found: " + numFound);
        }
    }
    private int FloodFill(int[,] maze, int x, int z,int value){
        int numEmptyTilesFound = 0;
        if (x < 0 || z < 0 || x >= mazeLength || z >= mazeLength) {
            return 0;
        }
        if (maze [x, z] == -1 || maze [x, z] == value) {
            return 0;
        }
        maze [x, z] = value;
        numEmptyTilesFound++;
        numEmptyTilesFound += FloodFill (maze, x - 1, z, value);
        numEmptyTilesFound += FloodFill (maze, x + 1, z, value);
        numEmptyTilesFound += FloodFill (maze, x, z - 1, value);
        numEmptyTilesFound += FloodFill (maze, x, z + 1, value);
        return numEmptyTilesFound;
    } 
    private ArrayList GetNextTile(int[,] maze,int value){
        int startX = 0;
        int startZ = 0;
        bool search = true;
        for (int i = 0; i < mazeLength; i++) {
            for (int j = 0; j < mazeLength; j++) {
                if (maze [i, j] == value) {
                    startX = i;
                    startZ = j;
                    search = false;
                }
                if(!search){
                    break;
                }
            }
            if(!search){
                break;
            }
        }
        ArrayList startingPoint = new ArrayList ();
        startingPoint.Add (startX);
        startingPoint.Add (startZ);
        return startingPoint;
    }
}

0 个答案:

没有答案