【问题标题】:can't intantiate an deck of cards on multiple locations generated from the script无法在脚本生成的多个位置实例化一副牌
【发布时间】:2020-08-25 14:51:29
【问题描述】:

我有一个脚本,我必须在脚本生成的 100 个不同位置(即 10 行和 10 列)实例化 100 张卡片。因此,当我运行 play 时,它会在 100 个不同的位置中的每个位置生成 100 张不同的卡片,这意味着它会生成 10,000 张卡片。我需要做的就是在 100 个位置上生成 100 张卡片。我是编程的新手,所以我相信这对你们所有的专业人士来说都是愚蠢的,但任何帮助都是好的,我的脚本如下:

'''

private void GenerateGrid()

{
    {

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                float posX = col * tilesize;
                float posY = row * tilesize;
                {
                    float xoffset = -4f;
                    float yoffset = 4f;

                    transform.position = new Vector2 (posX + xoffset, posY + yoffset);


                   foreach (string card in deck) {


                        GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity));

                        //GameObject newCard = (GameObject)Instantiate (cardPrefab);


                        newCard.name = card;
                    }


                }

            }

        }
    }

【问题讨论】:

    标签: c# unity3d scripting instantiation


    【解决方案1】:

    考虑一下。你有 10 行,10 列,100 张卡片。您有 3 个嵌套的 for 循环,因此您最终会调用 GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity)); 10*10*100 = 10 000 次。

    您实际上不需要遍历卡片。您需要循环遍历行和列(您已经在执行此操作),而不是遍历卡片,您只想将 一个 卡片添加到该位置,每次选择下一张卡片。所以你需要有一个计数器变量来计算接下来要选择哪张卡片。

        int counter = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                float posX = col * tilesize;
                float posY = row * tilesize;
                {
                    float xoffset = -4f;
                    float yoffset = 4f;
                    transform.position = new Vector2 (posX + xoffset, posY + yoffset);
    
                    GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity));
                    // Select card from deck
                    newCard.name = deck[counter];
                    // Increase counter by 1
                    counter++;
    
                }
            }
        }
    

    现在,如果您想成为一名出色的程序员,您可能会意识到您的前两个 for 循环可以充当您的计数器,因此您不需要那个变量。本质上,您有 2D 网格(行 x 列),您需要将其映射到 1D 字符串数组。 Here is another post that explains how to work it out

    您现在可以将我上面的代码重写为:

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                float posX = col * tilesize;
                float posY = row * tilesize;
                {
                    float xoffset = -4f;
                    float yoffset = 4f;
                    transform.position = new Vector2 (posX + xoffset, posY + yoffset);
    
                    GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity));
                    // Select card from deck
                    // Multiplied by "10" because that's width of your row
                    newCard.name = deck[row * 10 + col];
    
                }
            }
        }
    

    【讨论】:

    • 非常感谢您的帮助,它就像一个魅力和大声笑@成为一个很酷的程序员!!!我试着成为其中的一员,它奏效了。
    • 好的,我有点卡在另一件事情上,希望您能提供帮助。所以我们以随机顺序实例化了卡片,但我想在提到的行和列中以特定顺序实例化它们。请从下面给出卡片顺序的链接下载图像。共有 8 副牌,每副 12 张牌,编号从 1 到 8。彩色框是牌组从 wetransfer.com/downloads/… 开始的位置
    • 图片无法下载。链接坏了。卡片不是随机顺序的,它从一开始就以一为增量通过您的套牌变量。卡片的实例化顺序与它们在该卡片数组中的顺序相同。你需要去你创建(实例化)“卡片”变量的地方,并确保它遵循你想要的顺序。
    • 谢谢,但布局不是那么容易,我刚刚学会了如何在堆栈上共享图像。让我解释一下,总共有 100 张牌,因为它们只是像小丑一样的角落。其余的 96 是 8 套每 12 卡。在图像中,您会看到每组从哪里开始,所以有点卡在那里
    • ![布局]shared-assets.adobe.com/link/…>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2017-02-28
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多