c# - Unity 3D C#代码,重载电脑无报错

标签 c# function unity3d

问题在于 ShowPath();方法,因为它不断重载此代码应该收集最短路线,然后在找到开始和结束图 block 后突出显示它,它会计算到开始的最短路线

using UnityEngine;
using System;
using System.Collections.Generic;

namespace PathfindingClass
{
    public class pathFinding
    {
            public  bool startFound = false;
    public TileClass.Tile[,] grid = new TileClass.Tile[AStarPath.gridWidth,AStarPath.gridHeight];
    public Vector2 startTile;
    public Vector2 endTile;
    public Vector2 currentTile;

    // create a list that stores the checked tiles
    List<Vector2> openList = new List<Vector2>();
    List<Vector2> closedList = new List<Vector2>();

    public pathFinding (TileClass.Tile[,] grid)
    {
        this.grid = grid;

    }

    public void SearchPath(Vector2 startTile, Vector2 endTile){
        this.startTile = startTile;
        this.endTile = endTile;

        #region Path Validation
        bool canSearch = true;

        if(grid[(int)startTile.x,(int)startTile.y].walkable ==false){
        canSearch = false;
            Console.WriteLine("the start square is not walkable");
        }
        if(grid[(int)endTile.x,(int)endTile.y].walkable ==false){
        canSearch = false;
            Console.WriteLine("the end square is not walkable");
        }

        #endregion

        if(canSearch){
            //add the starting tile to the open list
        openList.Add(startTile);    
            currentTile = new Vector2(-1,-1);

            //while the open list is not empty
            while(openList.Count > 0){
                currentTile = getTyleWithLowestTotal(openList);

                //if the current tile is the end tile stop searching
                if((int)currentTile.x == (int)endTile.x && (int)currentTile.y == (int)endTile.y  ){
                //  if((int)currentTile.x == (int)endTile.x){
                        break;
                    //}
                }else{
                    openList.Remove(currentTile);
                    closedList.Add(currentTile);

                    //get all the adjacent tiles
                    List<Vector2> adjacentTiles = getAdjacentTiles(currentTile);

                    foreach(Vector2 adjacentTile in adjacentTiles){
                    // the adjacent tile is not aloude within eith of the open or closed lists
                        if(!openList.Contains(adjacentTile)){
                            if(!closedList.Contains(adjacentTile)){
                                // move it to the open list
                                openList.Add(adjacentTile);

                                TileClass.Tile tile = grid[(int)adjacentTile.x,(int)adjacentTile.y];

                                tile.cost = grid[(int)adjacentTile.x,(int)adjacentTile.y].cost+1;

                                //calculate the manhattan distance
                                tile.horistic = ManhattanDistance(adjacentTile);

                                //calculate the total cost
                                tile.total = tile.cost + tile.horistic;

                                tile.color = new Vector4(0,0,1,1);
                                tile.Y=2;
                                }
                        }                           
                    }
                }
            }
        }
        grid[(int)startTile.x,(int)startTile.y].color = Color.yellow;
        grid[(int)endTile.x,(int)endTile.y].color = Color.yellow;

        //Show the shortestPath

        ShowPath();
    }


    public void ShowPath(){
        Vector2 currentTile = endTile;
        List<Vector2> PathTiles = new List<Vector2>();

        while(!startFound){
        List<Vector2> adjacentTiles = getAdjacentTiles(currentTile);

            //check to  see what the used current tile is
            foreach(Vector2 adjacentTile in adjacentTiles){
                if(openList.Contains(adjacentTile) || closedList.Contains(adjacentTile)){

                    grid[(int)adjacentTile.x,(int)adjacentTile.y].color = Color.yellow;

                    if(adjacentTile.x == startTile.x){
                        startFound = true;
                    break;  
                    }
                }
            }
        }           
    }



    //calculate the manhattan distance
    public int ManhattanDistance(Vector2 adjacentTile){
    int manhattan = Math.Abs((int)( endTile.x - adjacentTile.x)) + Math.Abs((int)(endTile.y - adjacentTile.y)); 
        return manhattan;
    }

                    //check the adjacent tiles to the current tile
    public List<Vector2> getAdjacentTiles(Vector2 currentTile){
        List<Vector2> adjacentTiles = new List<Vector2>();
        Vector2 adjacentTile;

        //above
        adjacentTile = new Vector2(currentTile.x,currentTile.y+1);
        if(adjacentTile.y < AStarPath.gridHeight && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }
                    //below
        adjacentTile = new Vector2(currentTile.x,currentTile.y-1);
        if(adjacentTile.y >= 0 && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }
                    //right
        adjacentTile = new Vector2(currentTile.x +1,currentTile.y);
        if(adjacentTile.x < AStarPath.gridWidth && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }
                    //left
        adjacentTile = new Vector2(currentTile.x -1,currentTile.y);
        if(adjacentTile.x >= 0 && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }

        //optional to add diagonal checking
        return adjacentTiles;
    }   

    // get the tiles with the lowest total value
    public Vector2 getTyleWithLowestTotal(List<Vector2> openList){
        //temp vars
        Vector2 tileWithLowestTotal = new Vector2(-1,-1);
        int lowestTotal = int.MaxValue;

        // search all the open tiles and get the tile with the lowest total cost
        foreach(Vector2 openTile in openList){
            if(grid[(int)openTile.x,(int)openTile.y].total <= lowestTotal){
            lowestTotal = grid[(int)openTile.x,(int)openTile.y].total;
                tileWithLowestTotal = grid[(int)openTile.x,(int)openTile.y].ID;
            }               
        }
    return tileWithLowestTotal;
    }

}

最佳答案

您有 Unity Pro 吗?如果是这样,您可以使用探查器找到花费更多时间的地方。如果没有,你可以在MonoDevelop中运行Debugger:在代码中打一些断点,然后按下Debug按钮;它将打开另一个 Unity 实例。然后你播放这个项目,它会在你的断点处停止。从那时起,您可以逐步运行并查看它被锁定的位置。更多信息 here .

关于c# - Unity 3D C#代码,重载电脑无报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114552/

相关文章:

c# - Unity3D 中的 Leap Motion 手势识别总是返回 TYPEINVALID 和 STATEINVALID

c# - 从网络共享运行 c# 应用程序

c# - 调用 OnPaint 时 Infragistics UltraGrid 上的 "Index was outside the bounds of the array"

c# - 来自 Access ODBC 驱动程序的无效日期时间格式异常

c++ - 不确定 C++ 中的常量函数是什么

c# - 如何将随机数组与按主题相关的数组连接起来?

c# - 字符串中的引号

function - phpmyadmin:创建一个函数

php - 为什么没有参数的 PHP 函数需要括号?

c# - 实例化后的Unity3D C#面板定位