Unity 3D C#代码,重载计算机没有错误

时间:2012-04-11 21:57:08

标签: c# function unity3d

问题与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;
    }

}

}

1 个答案:

答案 0 :(得分:0)

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