c#脚本导致Unity编辑器崩溃

本文关键字:编辑器 崩溃 Unity 脚本 | 更新日期: 2023-09-27 18:13:29

我有什么可能是一个非常简单,或非常复杂的问题在我的统一脚本。简单地说,当尝试在打开列表中使用。add或。insert方法时,Unity编辑器将停止响应任何输入并挂起,直到在任务管理器中强制关闭。

所讨论的方法:

    private void addToOpen(location l){ //causes editor crash
        int fScore = l.getF (); //f score of the location to add
        int count = open.Count; //number of values in the list
        int index = 0; //index to insert the location
        Debug.Log ("count " + count);
        while (index < count) {
            if(fScore >= open[index].getF()){
                break;
            }
            index++;
        }
        open.Insert(index, temp);
    }

代码正在尝试实现A*寻路算法。
我正试图将下面的网页代码翻译成c#。

http://www.raywenderlich.com/4970/how-to-implement-a-pathfinding-with-cocos2d-tutorial

下面是我脚本的大部分内容,以提供上下文:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic; //for lists
    using System; //for broad use of "Exception"
    public class MouseBasedMovement : MonoBehaviour {
public GameObject map; //Holds the map object
private MapDataA mapdataa; //stores reference to data script
public Camera mainCamera;
void Awake(){
    mapdataa = map.GetComponent<MapDataA> (); //Links variable with component
}
//object for storing tile locations
public class location{
    public int xCoor;
    public int yCoor;
    //methods rarely interact with f as it is usually ignored, I tried to keep it isolated from extra interactions and assignments
    public int f;
    location parent; //location the object came from in A* algorithm
    public int g;
    public int h;
    public location(){
        xCoor = 0;
        yCoor = 0;
    }
    public location(int x, int y){
        xCoor = x;
        yCoor = y;
    }
    public location(location l){
        //constructor creates a copy of the location but does not copy the F value
        xCoor = l.getX ();
        yCoor = l.getY ();
    }
    public location(int x, int y, location a, location b){ //constructor for a*
        //x is x coordinate
        //y is y coordinate
        //a is starting position in algorithm ("from" tile)
        //b is target position in algorithm ("to" tile)
        xCoor = x;
        yCoor = y; 
        //g , h , f
        //g = getD(a, new location (x,y));
        //h = getD(new location(x,y), b);
        //f = getD(a, new location (x,y)) + getD(new location(x,y), b);
        //--- seems I can not nest constructors, going to simply do the same job by not using the getD method and using the method's logic + code instead.
        //private int getD(location a, location b){ //used for calculating g and h
            //if G,
            //a is the start point
            //b is the current square
            //if H, 
            //a is the current square
            //b is the endpoint
        //  int total = Math.Abs (a.getY () - b.getY ()) + Math.Abs (a.getX () - b.getX ());
        //  return total;
        //}
        g = Math.Abs (a.getY () - y) + Math.Abs (a.getX () - x);
        h = Math.Abs (y - b.getY ()) + Math.Abs (x - b.getX ());
        f = (Math.Abs (a.getY () - y) + Math.Abs (a.getX () - x)) + (Math.Abs (y - b.getY ()) + Math.Abs (x - b.getX ()));

    }
    public void setX(int x){
        xCoor = x;
    }
    public void setY(int y){
        yCoor = y;
    }
    public int getX(){
        return xCoor;
    }
    public int getY(){
        return yCoor;
    }
    public void setParent(location p){
        parent = p;
    }
    public location getParent(){
        return parent;
    }
    public void setF(int i){
        f = i;
    }
    public int getF(){
        return f;
    }
    public void setG(int gset){
        g = gset;
    }
    public int getG(){
        return g;
    }
    public void setH(int hset){
        h = hset;
    }
    public int getH(){
        return h;
    }
    public bool compareTo(location c){
        if ((c.getX () == this.getX ()) && (c.getY () == this.getY ())){
            return true;
        }else{
            return false;
        }
    }
    public string toString(){
        string str = "x: " + xCoor + " y: " + yCoor;
        return str;
    }
}
List<location> open = new List<location> (); //A* open list
List<location> closed = new List<location> (); //A* closed list
//method for the A* pathfinding algorithm's implementation in player movement
//Helped greatly in writing this section:
//http://www.raywenderlich.com/4946/introduction-to-a-pathfinding
//http://www.raywenderlich.com/4970/how-to-implement-a-pathfinding-with-cocos2d-tutorial
public void aStar(location a, location b){
    //a is starting position
    //b is ending position
    //checks to see if the destination is the same as the current position
    if (a.compareTo (b) == true) {
        Debug.Log ("Already There");
        return;
    }
    //Checks to see if the id of the destination tile is 1, and thus innacessable
    Debug.Log (mapdataa.mapData.GetLength (0));
    Debug.Log (mapdataa.mapData.GetLength (1));
    Debug.Log (b.getY ());
    Debug.Log (b.getX ());
    if (mapdataa.mapData[b.getY(), b.getX ()].getId () == 1){
        Debug.Log ("Destination tile is inaccessable");
        return;
    }
    //checks to see if the tile is highlighted (aka in range)
    GameObject[] inRange = GameObject.FindGameObjectsWithTag ("moveTransparent");
    bool found = false;
    foreach (GameObject trans in inRange) {
        if (trans.transform.position.x == b.getX () && trans.transform.position.y == b.getY ()){
            //tile is within range
            found = true;
        }
    }
    if (found == false) { //This section has worked in testing.
        //tile was never found to be within range
        Debug.Log ("Tile not in range/Tile not highlighted");
        return;
    }
    //begin A* actual algorithm implementation here
    bool pathFound = false;
    open.Add(new location ((int)transform.position.x, (int)transform.position.y, a, b)); //adds initial location of playable to open list 
    //testing
    Debug.Log ("open[0]: " + open[0].toString ());
    Debug.Log (open.Count);
    //end testing
    //above was previously an addToOpen method call, but I changed it to add, it SHOULD not affect logic, and may fix bugs
    location current = new location ();
    location temp = new location ();
    //Something in here (main algorithmic loop) is causing the crash
    do{
        //Get the lowest F score step
        //Because the list is ordered, the first step always has the lowest F score
        current = open[0];
        //add the current step to the closed list
        closed.Add (current);
        //remove the current step from the open list
        open.RemoveAt (0);
        //if current is the desired tile coordinate, the algorithm is complete
        if(current.compareTo (b)){
            pathFound = true;
            temp = current;
            Debug.Log ("Path Found");
            //LOOK INTO THIS! PARENT CHILD STRUCTURE NEEDS TO BE CONSIDERED
            do{
                Debug.Log (temp);
                temp = temp.getParent();
            }while(temp != null);
            break;
        }
        //get the adjacent tiles to the current step
        location[] adjSteps = getAdjacent (current,a,b);
        foreach (location loc in adjSteps){
            //check if the step isn't already in the closed set
            if (closed.Contains (loc)){
                continue; //effectively ignores it
            }
            //check if the step is already in the open list
            //NOTE: the line here in the source material is iffy, there might be functionality that I am not implementing
            if (open.Contains (loc) == false){
                //aka, not in the open list
                //set current step as the parent
                loc.setParent (current);                                                     //3
                //G score is parent g score + 1 (cost to move from parent to it)
                loc.setG (current.getG () + 1);                                              //4
                //compute H score
                loc.setH (getD (loc, b));                                                    //5
                //adding it to the open list
                addToOpen(loc);
            }else{
                //aka, already in the open list
                int index = open.IndexOf (loc); 
                //loc = open[index];
                //check to see if the G score is equal to the parent g score + cost to move(1)
                if(current.getG () + 1 < open[index].getG ()){
                    //The G score is equal to the parent G score + the cost to move to it
                    open[index].setG (current.getG () + 1);
                    //because the G score changed, f may change too
                    //so to keep the open list ordered, we have to remove and reinsert it
                    location locB = new location(open[index]); //added to circumvent bug
                    //remove from the list
                    open.RemoveAt (index);
                    //reinsert it
                    addToOpen (locB); //replaced loc with locB to circumvent bug
                }
            }
        }
    }while(open.Count > 0);
    if (!pathFound) { //no path found
        Debug.Log ("The algorithm failed to discover a path");
    }
}

    private void addToOpen(location l){ //causes editor crash
        int fScore = l.getF (); //f score of the location to add
        int count = open.Count; //number of values in the list
        int index = 0; //index to insert the location
        Debug.Log ("count " + count);
        while (index < count) {
            if(fScore >= open[index].getF()){
                break;
            }
            index++;
        }
        open.Insert(index, temp);
    }
//majorly reworked to prohibit returning null values to avoid editor crash while fixing bugs
private location[] getAdjacent(location l, location a, location b){
    location[] adjacents = new location[4];
    GameObject[] inRange = GameObject.FindGameObjectsWithTag ("moveTransparent");
    bool found = false;
    int counter = 0;
    //up 
    location up = new location (l.getX (), l.getY () + 1,a ,b);
    foreach (GameObject trans in inRange) {
        if (trans.transform.position.x == up.getX () && trans.transform.position.y == up.getY ()){
            //tile is within range
            found = true;
        }
    }
    if ((found == true) && (mapdataa.mapData [up.getY (), up.getX ()].getId () == 0)) { //if tile is within range and is acessable
        adjacents [counter] = up; //add it to the list
        counter++;
    }
    found = false; //reset found variable
    //down
    location down = new location (l.getX (), l.getY () -1,a,b);
    foreach (GameObject trans in inRange) {
        if (trans.transform.position.x == down.getX () && trans.transform.position.y == down.getY ()){
            //tile is within range
            found = true;
        }
    }
    if ((found == true) && (mapdataa.mapData [down.getY (), down.getX ()].getId () == 0)) { //if tile is within range and is acessable
        adjacents [counter] = down; //add it to the list
        counter++;
    }
    found = false; //reset found variable
    //left
    location left = new location (l.getX () -1, l.getY (),a,b);
    foreach (GameObject trans in inRange) {
        if (trans.transform.position.x == left.getX () && trans.transform.position.y == left.getY ()){
            //tile is within range
            found = true;
        }
    }
    if ((found == true) && (mapdataa.mapData [left.getY (), left.getX ()].getId () == 0)) { //if tile is within range and is acessable
        adjacents [counter] = left; //add it to the list
        counter++;
    }
    found = false; //reset found variable
    //right
    location right = new location (l.getX () +1, l.getY (),a,b);
    foreach (GameObject trans in inRange) {
        if (trans.transform.position.x == right.getX () && trans.transform.position.y == right.getY ()){
            //tile is within range
            found = true;
        }
    }
    if ((found == true) && (mapdataa.mapData [right.getY (), right.getX ()].getId () == 0)) { //if tile is within range and is acessable
        adjacents [counter] = right; //add it to the list
        counter++;
    }
    found = false; //reset found variable
    if (counter < 4) { //if statement is logically useless, but for the sake of trying to get to work, I'm leaving it as is.  Once it runs, try removing the if statement and test again.
        adjacents = retract (adjacents, 4-counter);
    }
    return adjacents;
}
private int getD(location a, location b){ //used for calculating g and h
    //implements the "Manhatten method" of determining total vertical and horizontal distance on a grid
    //if G,
    //a is the start point
    //b is the current square
    //if H, 
    //a is the current square
    //b is the endpoint
    int total = Math.Abs (a.getY () - b.getY ()) + Math.Abs (a.getX () - b.getX ());
    return total;
}
bool ran = false;
public void Update(){
    if (ran == false) {
        highlight ();
        ran = true;
    }
    if (Input.GetMouseButtonDown (0)) { //if left click
        Debug.Log ("Begin Update method A* implementation testing");
        aStar (new location((int)transform.position.x,(int)transform.position.y),new location((int)((((mainCamera.transform.position.x * 64) - (.5 * Screen.width)) + Input.mousePosition.x) / 64),(int)((((mainCamera.transform.position.y * 64) - (.5 * Screen.height)) + Input.mousePosition.y) / 64)));
        Debug.Log ("End Update method A* implementation testing");
    }
}

}

c#脚本导致Unity编辑器崩溃

我不能真正调试这个,因为我没有完整的上下文,但这是我从查看源代码的猜测

//This line seems to cause the editor crash
open.Insert (index, l);

让我们看一下这里使用的变量:

  • open:存储元素的列表(count 可以为零)
  • index:索引,从1开始(如果 open.Count==0,则保持在1 )

也许你已经发现了问题:列表有0个元素,你试图在位置1插入一个元素。c#(或者更确切地说是CLR)不太喜欢这样,只是抛出一个异常(至少在Visual Studio中)。

碰撞部分很有趣。因为你使用Unity,我假设你使用MonoDevelop作为你的编辑器。编辑器是否在每次出现异常时都崩溃?(尝试int a=1/0或类似的东西来测试)。如果没有,很可能你在MonoDevelop中发现了一个bug(在这种情况下,你应该提交一个bug报告)。当然,也可能是其他原因导致了崩溃(例如算法中的无限循环)。

首先,open.InsertRange(index, temp)是错误的。改回open.Insert(index, l)

第二,当你添加相邻的贴图时,你并没有更新F分数。如果它是一个新贴图,你设置G和H,但不设置f。如果它已经在打开列表中,你甚至会评论"因为G分数改变了,f可能也会改变",但你不会更新f。f总是会改变,因为f = G + H。

也许可以退一步思考算法应该如何工作。要么使用教程的介绍,要么使用维基百科的文章和一些伪代码。

如果它仍然卡在某个地方,在你的aStar循环中添加一些Debug.Log(),这样你就可以跟踪你的算法在做什么。建议在测试时设置一个小的世界,否则你会迷失方向;)