简体   繁体   English

根据二维列表实例化预制件,然后替换

[英]Instantiate prefabs according to a 2d list and replace afterwards

I'm new to unity programming and trying to recreate a puzzle game I once created in Java. 我是统一编程的新手,试图重新创建曾经用Java创建的益智游戏。 To generate the "playing field" I have a 2d list which contains 0's, 1's and 2's. 为了生成“比赛场地”,我有一个二维列表,其中包含0、1和2。 Now I want to create a visual matrix which corresponds with the 2d list. 现在,我想创建一个与2d列表相对应的可视矩阵。

I first instantiate a prefab in the visual matrix when the 2d list equals a 1 or a 2 on a specific position, creating a square. 我首先在2d列表在特定位置等于1或2时在可视矩阵中实例化预制件,从而创建一个正方形。 But now my question is how can I change the instantiated prefabs? 但是现在我的问题是如何更改实例化的预制件? The game requires me to change a value in the 2d list from 1 to 0 or vice versa, which also means the visual representation should change. 游戏要求我将2d列表中的值从1更改为0,反之亦然,这也意味着视觉表示应该更改。

So my question is, what is the best way to do this or how can I change an already created prefab if needed? 所以我的问题是,执行此操作的最佳方法是什么?如果需要,如何更改已创建的预制件? (Is there a way to name them or reference them or something?) (是否可以命名或引用它们?)

Let's say this is how you instantiate your object 假设这是实例化对象的方式

GameObject[,] objects = new GameObject[LENGTH, WIDTH];
for(int i = 0; i < LENGTH; i++){
    for(int j = 0; j < WIDTH; j++){
        objects[i, j] = (GameObject)Instantiate(Prefab);
        //Set position and do other initialization stuff here
    }
}

There's a couple ways you can make it change based on changes made to the matrix. 您可以通过几种方法根据对矩阵所做的更改进行更改。

1) Whenever you make a change to the matrix, just directly change the sprite of the object: 1)只要更改矩阵,就可以直接更改对象的精灵:

matrix[1,2] = 1;
objects[1,2].getComponent<SpriteRenderer>().sprite = sprite1;

matrix[2,3] = 0;
objects[2,3].getComponent<SpriteRenderer>().sprite = sprite0;

In this sprite1 and sprite0 would probably be set through the inspector by declaring them as public variables in code. 在这种情况下,可以通过检查器将sprite1和sprite0声明为代码中的公共变量来进行设置。

2) Make an update function that will change the visual representation of the GameObject array based on the integer array 2)制作一个更新函数,该函数将基于整数数组更改GameObject数组的外观

void update(){
    for(int i = 0; i < LENGTH; i++){
        for(int j = 0; j < LENGTH; j++){
            switch(matrix[i, j]){
                case 0:
                    objects[i, j].getComponent<SpriteRenderer>().sprite = sprite0;
                    break;
                case 1:
                    objects[i, j].getComponent<SpriteRenderer>().sprite = sprite1;
                    break;
                case 2:
                    objects[i, j].getComponent<SpriteRenderer>().sprite = sprite2;
                    break;
            }
        }
    }
}

With this update function, whenever you want to update the visual representation of the matrix you just run it. 使用此更新功能,只要您想更新矩阵的外观,就可以运行它。 Keep in mind, the way I've written the code depends on the matrix array and the objects array being public. 请记住,我编写代码的方式取决于矩阵数组和对象数组是否公开。 You could get around this by taking both of them as parameters. 您可以通过将它们都作为参数来解决此问题。

Also, as mentioned before, you would probably set sprite0, sprite1, and sprite2 through the inspector by declaring them as public variables in code. 另外,如前所述,您可能会通过在检查器中将sprite0,sprite1和sprite2声明为代码中的公共变量来设置它们。

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

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