用程序设计统一的棋盘方块

本文关键字:方块 程序设计 | 更新日期: 2023-09-27 18:27:40

我是Unity的新手,正在尝试在Unity中创建棋盘背景。我需要使用我创建的预制件使场景充满立方体。当计数器为偶数时,立方体设置为黑色,其他设置为红色。我不知道到底发生了什么,因为当z为0时,什么都没有显示。这是我的密码。。

public GameObject pre;
    public int counter = 0;
    public int worldWidth = 20;
    public int worldHeight = 20;
    private float positionX;
    private float positionY;
    void Start()
    {
        chessBoardCube = Resources.Load<GameObject>("Prefabs/pre");
        positionX = -11.24f;
        positionY = 4.8f;
        counter = 0;
    }
    void Update()
    {
        for (int x = 0; x < worldWidth; x++)
        {
            for (int z = 0; z < worldHeight; z++)
            {
                if (counter % 2 == 0)
                {
                    GameObject block = Instantiate(pre, Vector3.zero, Quaternion.identity) as GameObject;
                    block.renderer.material.color = Color.black;
                    block.transform.parent = transform;
                    float xP = positionX * 3;
                    float yP = positionY * z;
                    block.transform.localScale = new Vector3(2, 2, 1);
                    block.transform.localPosition = new Vector3(xP, 0);
                }
                else
                {
                    GameObject block = Instantiate(pre, Vector3.zero, Quaternion.identity) as GameObject;
                    block.renderer.material.color = Color.red;
                    block.transform.parent = transform;
                    float xP = positionX * x;
                    float yP = positionY * z;
                    block.transform.localScale = new Vector3(2, 2, 1);
                    block.transform.localPosition = new Vector3(xP, 0);
                }
            }
            counter++;
        }
    }

位置X和位置Y变量是预制件设置的位置。

如有任何帮助,我们将不胜感激。

谢谢!

用程序设计统一的棋盘方块

我建议这样做:

    public GameObject pre;
    public int worldWidth = 20;
    public int worldHeight = 20;
    public float cubeSize = 2;
    private float positionX;
    private float positionY;
    private float positionZ;
    void Start()
    {
        chessBoardCube = Resources.Load<GameObject>("Prefabs/pre");
        positionX = -11.24f;
        positionY = 4.8f;
        positionZ = 0.0f;
    }
    //Probably unwanted in update
    void Update()
    {
        for (int x = 0; x < worldWidth; x++)
        {
            for (int y = 0; y < worldHeight; y++)
            {
                GameObject block = Instantiate(pre, Vector3.zero, Quaternion.identity) as GameObject;
                block.transform.parent = transform;
                if(x%2 == 0) {
                    if(y%2 == 0) {
                        block.renderer.material.color = Color.black;
                    } else {
                        block.renderer.material.color = Color.red;
                    }
                } else {
                    if(y%2 == 0) {
                        block.renderer.material.color = Color.red;
                    } else {
                        block.renderer.material.color = Color.black;
                    }
                }
                float xP = positionX + x*cubeSize;
                float yP = positionY + y*cubeSize;
                block.transform.localScale = new Vector3(cubeSize, cubeSize, 1);
                block.transform.localPosition = new Vector3(xP, yP, positionZ);
            }
        }
    }
void generateBoard(){
        float tempX = cubePrefab.transform.localScale.x;
        float tempY = cubePrefab.transform.localScale.y;
        float tempZ = cubePrefab.transform.localScale.z;
        for (int i = 0; i < boardLength; i++) {
            tempX = cubePrefab.transform.localScale.x;
            for (int j = 0; j < boardWidth; j++) {
                GameObject block = Instantiate(cubePrefab, parentTransform) as GameObject;
                block.transform.localPosition = new Vector3(tempX , tempY , tempZ);
                tempX += cubePrefab.transform.localScale.x;
                if(i%2 == 0){
                    if(j%2 == 0){
                        block.GetComponent<Renderer>().material.color = Color.white;
                    }else{
                        block.GetComponent<Renderer>().material.color = Color.black;
                    }
                }else{
                    if(j%2 == 0){
                        block.GetComponent<Renderer>().material.color = Color.black;
                    }else{
                        block.GetComponent<Renderer>().material.color = Color.white;
                    }
                }
            }
            tempZ += cubePrefab.transform.localScale.z;
        }
    }