Search Unity

help with choosing proper data structure

Discussion in 'Scripting' started by pretender, Dec 18, 2014.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    Hi!

    I have level that consists of cubes. it will be vary in size but lets say it will be like this
    width: 8 cubes
    heigth 8 cubes
    length 16 cubes

    cube is 1 scale

    I currently generate it by looping and translating by 1 unit like this:

    Code (CSharp):
    1. IEnumerator Create()
    2.     {
    3.         for (int i = 0; i < Length; i++)
    4.         {
    5.             for (int j = 0; j < Height; j++)
    6.             {
    7.                 for (int k = 0; k < Width; k++)
    8.                 {
    9.                     var g = NGUITools.AddChild(Root, Prefab);
    10.                     if (g != null)
    11.                     {
    12.                         g.transform.position = Vector3.zero;
    13.                         g.transform.rotation = Quaternion.identity;
    14.                        
    15.                         g.transform.Translate(new Vector3(k-Width/2f+ 0.5f,j-Height/2f+ 0.5f,i-Length/2f+ 0.5f));
    16.                     }
    17.  
    18.                     yield return null;
    19.                 }
    20.             }
    21.          
    22.         }
    23.     }
    I used NGUITools.AddChild because cube has some ui elements that are displayed on one of its sides. this propery creates the cubes, i am just wondering how to store this information so i can easily access cubes that are on the surface or by some meanful way. any ideas? jagged arrays? lists,dict or something else?

    any idea is appreciated!
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Fit the solution to the problem. Since it's only a maximum of 1024 objects, you can probably get by with a simple 3-dimensional array. If you have more objects, you could use a BSP tree, octree, or similar space partitioning technique. I think the solution will depend on what your requirements are for getting information in "some meaningful way".