如何在没有内存异常的情况下创建向量的3D网格-Unity 3D

时间:2015-03-05 23:47:43

标签: c# vector unity3d

下面是一些简单的代码行,是uni技术演示的一部分。试图在给定区域内创建矢量的3D网格。

到目前为止,我的解决方案是在起始X和Y点处创建2D网格,然后沿Z重复此过程。 作为临时可视化,我然后实例化Sphere预制件。 其目的是使用此向量网格作为深度优先搜索路径算法的模型,我将使用该算法为程序生成的轨道输入向量(当前控制点通过编辑器方法手动设置)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GridLayout : MonoBehaviour {
public int GridWidth;
public int GridLength;
public int GridHeight;
public int resolution;

private int ResW;
private int ResL;
private int ResH;
private List<Vector3> GridPoints = new List<Vector3>();
private bool GridCompleted = false;

private GameObject tempObject; 
// Use this for initialization
void Start () {

    //Area box Start square
    GridPoints.Add(new Vector3(0,0,0)); 
    GridPoints.Add(new Vector3(0,GridHeight,0)); 
    GridPoints.Add(new Vector3(GridWidth,0,0)); 
    GridPoints.Add(new Vector3(GridWidth,GridHeight,0)); 

    ResW = GridWidth/resolution;
    ResH = GridHeight/resolution;
    ResL = GridLength/resolution;
} 

// Update is called once per frame
void Update () {



if (GridCompleted == false)
            CreateGrid();

else
{
    for(int i = 0; i <= GridPoints.Count; i++)
    {
            tempObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            tempObject.transform.position = GridPoints[i];
    }
}
}   //Area Box End square
//  GridPoints.Add(new Vector3(0,0,GridLength)); 
//  GridPoints.Add(new Vector3(0,GridHeight,GridLength)); 
//  GridPoints.Add(new Vector3(GridWidth,0,GridLength)); 
//  GridPoints.Add(new Vector3(GridWidth,GridHeight,GridLength)); 


void CreateGrid()
{
 if(ResW != GridWidth | ResL != GridLength | ResH != GridHeight)
   {
    for(int l = 1;ResL <= GridLength; l++)
       {
      GridPoints.Add (new Vector3(0,0,ResL));
       ResL = (GridLength/(resolution))*l;

       for(int w = 1;ResW <= GridWidth; w++)
          {
          GridPoints.Add (new Vector3(ResW,0,0));
          ResW = (GridWidth/(resolution))*w;

          for(int h = 1;ResW <= GridHeight; h++)
             {
             GridPoints.Add (new Vector3(0,ResH,0));
             ResH = (GridHeight/(resolution))*h;

             }
         }
    }
 }
    else 
    {
        GridCompleted = true;
    }

}

}

不幸的是,这个三重for循环会导致内存异常 - 这是一台高端PC但是我将被迫在4GB内存的笔记本电脑上运行我的项目。

考虑到这一点:是否有一种更有效的内存创建vector3网格的方法。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

在行中键入失败

for(int h = 1;ResW <= GridHeight; h++)

导致你的记忆力问题。最内层循环无限运行,它应该是ResH。因此,我建议检查for语句中的for-variable而不是其他内容:

for(int h = 1; h < wharever; h++)

其次:错误的代码格式化和缩进 最后,到目前为止,list<object>是一维结构。第三种结构是list<list<list<object>>>或数组object [][][]

Vector3 [][][] vector3grid;
vector3grid = new vector3[lenX][][];
for (int x=0; x<lenX; x++)
{
  vector3grid[x] = new vector3 [lenY][];
  for (int y=0; y<lenY; y++)
  {
    vector3grid[x][y] = new vector3 [lenZ];
    // init if needed:
    for(int z=0; ...
      vector3grid[x][y][z] = ...
  }
}

修改 我刚刚注意到我的答案并非100%正确。上面的示例是创建3D阵列的两种(或更多)方法中的一种。其中一个更容易遵循:

在C ++ / cli中:

  Array<vector3, 3>^ vector3grid = gcnew array<vector3, 3>(lenX, lenY, lenZ);

对于c#和VB.net,我需要先查找语法。

现在这是一个真正的3D阵列。 ; - )

编辑2: c#中的3D:

Vector3 [,,] vector3grid = New vector3[lenX,lenY,lenZ];