繁体   English   中英

如何在 c# function 中使用 2D GameObject 数组作为参数(2D 统一游戏)

[英]How to use 2D GameObject array as a parameter in a c# function ( 2D unity game)

我是 unity/c# 的新手,我正在使用 unity 编写 2D 国际象棋游戏,但在制作函数时遇到了一些麻烦。 我正在尝试设置一个 function 来渲染板上的碎片,但我似乎无法让它工作。 我想在此 function 中使用 2D 数组作为第三个参数,但我不断收到此错误:错误 CS1503: Argument 3: cannot convert from 'UnityEngine.GameObject' to 'UnityEngine.GameObject[,]' 这是我的板子 class:

void Start()
{
    boardScript = GameObject.FindGameObjectWithTag("BoardScript").GetComponent<Board>();
    spawn = GameObject.FindGameObjectWithTag("GameController").GetComponent<SpawnPieces>();

    boardScript.createBoard();
}

这是我的一门课:

public void spawnQueen(int x, int y, GameObject[,] board)
{
    if (x == 3 && y == 0)
    {
        board[x, y] = Instantiate(whiteQueen, new Vector3(x * 4.49f, y * 4.49f, -1), Quaternion.identity);
    }
    else if (x == 3 && y == 7)
    {
        board[x, y] = Instantiate(blackQueen, new Vector3(x * 4.49f, y * 4.49f, -1), Quaternion.identity);
    }
}

这是 class 将渲染这些片段:

public void spawnPiece()
{
    boardScript = GameObject.FindGameObjectWithTag("BoardScript").GetComponent<Board>();
    GameObject[,] boardArray = boardScript.board;

    rookScript = GameObject.FindGameObjectWithTag("RookScript").GetComponent<RookScript>();
    pawnScript = GameObject.FindGameObjectWithTag("PawnScript").GetComponent<PawnScript>();
    kingScript = GameObject.FindGameObjectWithTag("KingScript").GetComponent<KingScript>();
    bishopScript = GameObject.FindGameObjectWithTag("BishopScript").GetComponent<BishopScript>();
    kinghtScript = GameObject.FindGameObjectWithTag("knightScript").GetComponent<knightScript>();
    queenScript = GameObject.FindGameObjectWithTag("QueenScript").GetComponent<QueenScript>();

    for (int x = 0; x < 8; x++)
    {
        for (int y = 0; y < 8; y++)
        {
            rookScript.spawnRook(x, y, boardArray[x, y]);
            pawnScript.spawnPawn(x, y, boardArray[x, y]);
            knightScript.spawnKnight(x, y, boardArray[x, y]);
            kingScript.spawnKing(x, y, boardArray[x, y]);
            queenScript.spawnQueen(x, y, boardArray[x, y]);
            bishopScript.spawnBishop(x, y, boardArray[x, y]);
        }
    }
}

正如我所提到的,我是 C# 的新手,我们将不胜感激。

通过调用方法的方式,您传递的是 boardArray 的一个element ,而不是整个数组。 尝试调用这样的方法:

bishopScript.spawnBishop(x, y, boardArray);

注意第三个参数是boardArray而不是boardArray[x, y]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM