var arr = BinaryDataMgr.Instance.GetData("Height_" + levelId); int index = 0; Vector3 startPos = new Vector3(0, 0, 0); for (int i = 0; i < length; i++) { Vector3 pos = startPos; for (int j = 0; j < width; j++) { GameObject game = new GameObject(); game.transform.SetParent(GameObject.Find("Map").transform); game.transform.localPosition = pos; game.transform.localScale = new Vector3(Gridsize, arr[index] / 1000.0f, Gridsize); game.AddComponent<DrawHeightGround>(); pos.x += Gridsize; pos.y = 4; index++; } startPos.z += Gridsize; } 这段代码如何改成从左上角(0,0,0,)开始遍历格子
时间: 2024-02-14 13:13:27 浏览: 231
可以将 `startPos` 初始化为左上角的位置,即 `(0, 0, 0)`,然后在 `for` 循环中,先对 `j` 进行遍历,再对 `i` 进行遍历,这样就可以从左上角开始遍历格子了。修改后的代码如下:
```
var arr = BinaryDataMgr.Instance.GetData("Height_" + levelId);
int index = 0;
Vector3 startPos = new Vector3(0, 0, 0);
for (int j = 0; j < width; j++)
{
Vector3 pos = startPos;
for (int i = 0; i < length; i++)
{
GameObject game = new GameObject();
game.transform.SetParent(GameObject.Find("Map").transform);
game.transform.localPosition = pos;
game.transform.localScale = new Vector3(Gridsize, arr[index] / 1000.0f, Gridsize);
game.AddComponent<DrawHeightGround>();
pos.x += Gridsize;
pos.y = 4;
index++;
}
startPos.z += Gridsize;
}
```
阅读全文