繁体   English   中英

以编程方式统一设计棋盘立方体

[英]Programatically design chess board cubes in 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++;
        }
    }

positionX和positionY变量是设置预制件的位置。

任何帮助,将不胜感激。

谢谢!

我建议这样的事情:

    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;
        }
    }

暂无
暂无

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

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