I know I can use this helper script in order to create multidimensional builtin arrays in Unity Javascript -- but can I create multidimensional builtin VECTOR3 arrays? I don't know C# so I tried tinkering with the helper script to no avail.
As a side question, are multidimensional builtin arrays as fast in Unity as single-dimensional builtin arrays? I'd rather have performance and a little extra coding...
You can make a new class containing an array of vector3's
e.g.
class ThreeVecs
{
var v3Arr : Vector3[] = new Vector3[3]; // Array of 3 x Vector3
function ThreeVecs(){} // constructor - init the class variables here if you like
}
You can then create an array of ThreeVecs:
var multiArr : ThreeVecs[] = new ThreeVecs[10]; // Array of 10 ThreeVecs
multiArr[0] = new ThreeVecs();
multiArr[0].v3Arr[0] = Vector3(1,2,3);
multiArr[0].v3Arr[1] = Vector3(4,5,6);
multiArr[0].v3Arr[2] = Vector3(7,8,9);
Just one possible way. :)
[EDIT]
so a class to answer your question below and illustrate using the constructor to initialise:
class ThreeVecs
{
var corner0 : Vector3;
var corner1 : Vector3;
var corner2 : Vector3;
function ThreeVecs(c0 : Vector3, c1 : Vector3, c2 : Vector3)
{
corner0 = c0;
corner1 = c1;
corner2 = c2;
}
}
var multiArr : ThreeVecs[] = new ThreeVecs[10]; // Array of 10 ThreeVecs
multiArr[0] = new ThreeVecs(Vector3(1,2,3), Vector3(4,5,6), Vector3(7,8,9));
Debug.Log(multiArr[0].corner0); //
Sure, you'd probably simply add this static method to the C# script:
public static Vector3[,] Vector3Array (int a, int b) {
return new Vector3[a,b];
}
... so that would be for a 2-dimensional one ... for 3-dimensional, you'd add:
public static Vector3[,,] Vector3Array (int a, int b, int c) {
return new Vector3[a,b,c];
}
Regarding the question of performance: I'm pretty sure there is no performance difference between one-dimensional arrays and multi-dimensional ones. Of course, creating those arrays will likely take a little longer because usually they have more "space" so more memory needs to be allocated and managed. But writing/reading into/from those arrays shouldn't take a significant amount of time.
Of course, it depends on how "much" you do of it. As a rule of thumb: If it's just once per frame: Don't worry. If you're looping or doing nested loops in each frame: Watch out.