我是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变量是预制件设置的位置。
任何帮助都将不胜感激。
谢谢!
答案 0 :(得分:0)
我建议这样的事情:
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);
}
}
}
答案 1 :(得分:-1)
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;
}
}