int index = 0; RaycastHit hit; Vector3 startPos = new Vector3(0, 0, 0); GameObject cube = new GameObject(); cube.name = "cube"; cube.transform.localScale = new Vector3(this.m_GridSize, this.m_BaseHight, this.m_GridSize); cube.transform.SetParent(node.transform); for (int i = GirdLength-1; i >= 0; i--) { Vector3 pos = startPos; for (int j = 0; j < GirdWidth; j++) { cube.transform.localPosition = pos; pos.x += m_GridSize; Ray ray = new Ray(cube.transform.position + Vector3.up * 1000, Vector3.down); bool res = Physics.Raycast(ray, out hit, 1000f); if (res) { if (hit.transform.gameObject.layer == 10) { var height = ((hit.point.y - m_BaseHight) * HEIGHT_FLOAT_VAL); mapHeightArr[index++] = (short)height; } else { mapHeightArr[index++] = 0; } } else { mapHeightArr[index++] = 0; } IsFinsh = true; } startPos.z += m_GridSize; } 这段代码是从左上角开始遍历格子吗
时间: 2024-02-10 12:22:32 浏览: 168
不是的,这段代码是从左下角开始遍历格子的。
在代码中,首先设置了一个名为 `startPos` 的起始坐标,其值为 `(0,0,0)`,然后在外层循环中,每次循环时将 `startPos` 的 `z` 坐标加上一个 `m_GridSize`,也就是格子的边长,这样就能够往上遍历格子了。
因此,这段代码是从左下角开始遍历,而不是从左上角开始遍历。
相关问题
Vector3 startPos = new Vector3(0, 0, 0); int index = 0; GameObject cube = new GameObject(); cube.name = "cube"; cube.transform.localScale = new Vector3(this.m_GridSize, this.m_BaseHight, this.m_GridSize); for (int i = 0; i < GirdWidth; i++) { Vector3 pos = startPos; for (int j = 0; j < GirdLength; j++) { var go = Instantiate(cube); go.transform.SetParent(node.transform); go.transform.localPosition = pos; pos.x += m_GridSize; Ray ray = new Ray(go.transform.position + Vector3.up * 1000, Vector3.down); RaycastHit hit; bool res = Physics.Raycast(ray, out hit, 1000f); if (res) { if (hit.transform.gameObject.layer == 10) { var height = ((hit.point.y - m_BaseHight) * HEIGHT_FLOAT_VAL); mapHeightArr[index++] = (short)height; go.transform.position = new Vector3(go.transform.position.x, hit.point.y+100, go.transform.position.z); } else { mapHeightArr[index++] = 0; } } else { mapHeightArr[index++] = 0; } IsFinsh = true; } startPos.z += m_GridSize; } 代码如何优化具体代码实现
以下是一些可能的优化方案:
1.使用对象池:在循环中不断创建和销毁游戏对象可能会导致性能问题。使用对象池可以减少创建和销毁游戏对象的次数,提高性能。
2.使用缓存变量:在循环中不断创建变量可能会导致性能问题。使用缓存变量可以减少变量的创建次数,提高性能。
3.使用线程:在进行大量计算时,可以使用线程来将计算分离到另一个线程中,避免主线程被卡住。
4.使用物理材质:在进行射线检测时,可以使用物理材质来避免不必要的计算。
5.缩短射线检测距离:在进行射线检测时,可以将检测距离缩短到实际需要的距离,避免不必要的计算。
下面是一个可能的优化实现:
```
Vector3 startPos = new Vector3(0, 0, 0);
int index = 0;
GameObject cube = new GameObject();
cube.name = "cube";
cube.transform.localScale = new Vector3(this.m_GridSize, this.m_BaseHight, this.m_GridSize);
// 使用对象池
ObjectPool pool = new ObjectPool(cube, GirdWidth * GirdLength);
for (int i = 0; i < GirdWidth; i++)
{
Vector3 pos = startPos;
for (int j = 0; j < GirdLength; j++)
{
// 使用对象池
var go = pool.GetNext();
go.transform.SetParent(node.transform);
go.transform.localPosition = pos;
pos.x += m_GridSize;
// 将射线检测距离缩短到实际需要的距离
Ray ray = new Ray(go.transform.position + Vector3.up * 1000, Vector3.down);
RaycastHit hit;
bool res = Physics.Raycast(ray, out hit, m_BaseHight + 1000f);
if (res)
{
// 使用物理材质
if (hit.collider.sharedMaterial == groundMaterial)
{
var height = ((hit.point.y - m_BaseHight) * HEIGHT_FLOAT_VAL);
mapHeightArr[index++] = (short)height;
go.transform.position = new Vector3(go.transform.position.x, hit.point.y + 100, go.transform.position.z);
}
else
{
mapHeightArr[index++] = 0;
}
}
else
{
mapHeightArr[index++] = 0;
}
IsFinsh = true;
}
startPos.z += m_GridSize;
}
// 清空对象池
pool.Clear();
```
注意:以上建议只是一些可能的优化方案,具体根据实际情况进行优化。
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,)开始遍历格子
可以将 `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;
}
```
阅读全文