Unity 3D C#代码,超载计算机无错误

Unity 3D C#代码,超载计算机无错误,c#,function,unity3d,C#,Function,Unity3d,问题在于ShowPath();方法,因为它一直在重载此代码应该收集最短路径,然后突出显示它一旦找到开始和结束磁贴,它就会计算到目标的最短路径 using UnityEngine; using System; using System.Collections.Generic; namespace PathfindingClass { public class pathFinding { public bool startFound = false;

问题在于ShowPath();方法,因为它一直在重载此代码应该收集最短路径,然后突出显示它一旦找到开始和结束磁贴,它就会计算到目标的最短路径

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

}
使用UnityEngine;
使用制度;
使用System.Collections.Generic;
命名空间路径查找类
{
公共类寻路
{
公共bool startFound=false;
public TileClass.Tile[,]grid=新建TileClass.Tile[AStarPath.gridWidth,AStarPath.gridHeight];
公共向量2 startTile;
公共向量2 endTile;
公共向量2;
//创建一个存储选中分幅的列表
List openList=新列表();
列表关闭列表=新建列表();
公共寻路(TileClass.Tile[,]网格)
{
this.grid=grid;
}
公共无效搜索路径(Vector2 startTile、Vector2 endTile){
this.startTile=startTile;
this.endTile=endTile;
#区域路径验证
bool canSearch=true;
if(grid[(int)startTile.x,(int)startTile.y].walkable==false){
canSearch=false;
Console.WriteLine(“起始方块不可行走”);
}
如果(网格[(int)endTile.x,(int)endTile.y].walkable==false){
canSearch=false;
Console.WriteLine(“末端方形不可行走”);
}
#端区
如果(可以搜索){
//将起始磁贴添加到打开列表中
openList.Add(startTile);
currentTile=新矢量2(-1,-1);
//而打开的列表不是空的
而(openList.Count>0){
currentTile=getTyleWithLowestTotal(openList);
//如果当前磁贴是结束磁贴,请停止搜索
如果((int)currentile.x==(int)endTile.x&&(int)currentile.y==(int)endTile.y){
//如果((int)currentTile.x==(int)endTile.x){
打破
//}
}否则{
openList.Remove(currentTile);
closedList.Add(currentTile);
//获取所有相邻的瓷砖
List adjacentTiles=getAdjacentTiles(currentTile);
foreach(矢量2邻接图块中的邻接图块){
//相邻的磁贴不在打开或关闭列表的范围内
如果(!openList.Contains(adjacentTile)){
如果(!closedList.Contains(adjacentTile)){
//将其移动到“打开”列表
添加(邻接瓷砖);
TileClass.Tile Tile=栅格[(int)adjacentTile.x,(int)adjacentTile.y];
tile.cost=grid[(int)adjacentTile.x,(int)adjacentTile.y].cost+1;
//计算曼哈顿距离
地砖。地砖=曼哈顿地砖(相邻地砖);
//计算总成本
tile.total=tile.cost+tile.horitic;
tile.color=新矢量4(0,0,1,1);
Y=2;
}
}                           
}
}
}
}
网格[(int)startTile.x,(int)startTile.y].color=color.yellow;
网格[(int)endTile.x,(int)endTile.y].color=color.yellow;
//显示最短路径
ShowPath();
}
公共void ShowPath(){
Vector2 currentTile=endTile;
List PathTiles=新列表();
而(!startFound){
List adjacentTiles=getAdjacentTiles(currentTile);
//检查当前使用的磁贴是什么
foreach(矢量2邻接图块中的邻接图块){
if(openList.Contains(adjacentTile)| | closedList.Contains(adjacentTile)){
网格[(int)adjacentTile.x,(int)adjacentTile.y].color=color.yellow;
if(adjacentTile.x==startTile.x){
startFound=true;
打破
}
}
}
}           
}
//计算曼哈顿距离
曼哈顿公共区(矢量2相邻瓷砖){
intmanhattan=Math.Abs((int)(endTile.x-adjacentTile.x))+Math.Abs((int)(endTile.y-adjacentTile.y));
返回曼哈顿;
}
//检查当前磁贴的相邻磁贴
公共列表GetAdjacentTile(矢量2 currentTile){
列表邻接瓷砖=新列表();
矢量2邻接图;
//在上面
邻接瓷砖=新矢量2(currentTile.x,currentTile.y+1);
if(adjacentTile.y=0&&grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
adjacentTiles.Add(adjacentTile);
}
//对
邻接瓷砖=新矢量2(currentTile.x+1,currentTile.y);
if(adjacentTile.x=0&&grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
adjacentTiles.Add(adjacentTile);
}
//可选择添加对角线检查
返回相邻瓷砖;
}   
//获取总值最低的磁贴
公共向量2 getTyleWithLowestTotal(列表openList){
//温度变量
Vector2 tileWithLowestTotal=新Vector2(-1,-1);
int最低总计=i