|
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...
(comments are locked)
|
|
Sure, you'd probably simply add this static method to the C# script:
... so that would be for a 2-dimensional one ... for 3-dimensional, you'd add:
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. As with anything that has to do with performance: "Premature optimization is the root of all evil" (Donald Knuth). See also: Wikipedia: Program Optimization - When to optimize I just tried that and got the following Console error: Assets/Standard Assets/Scripts/MultiDim.cs(31,19): error CS0246: The type or namespace name `Vector3' could not be found. Are you missing a using directive or an assembly reference?
Aug 05 '10 at 01:23 PM
robinking
@Robin King: you need to use the UnityEngine namespace for Unity-specific structs like Vector3.
Aug 05 '10 at 02:44 PM
Eric5h5
Brilliant, that's something else I've learned about today. Thanks!
Aug 05 '10 at 03:41 PM
robinking
(comments are locked)
|
|
You can make a new class containing an array of vector3's e.g.
You can then create an array of ThreeVecs:
Just one possible way. :) [EDIT] so a class to answer your question below and illustrate using the constructor to initialise: I think I understand it! Could I instead give the class three variables corner0, corner1 and corner2 - and then say... var multiArr : ThreeVecs[] = new ThreeVecs[10]; multiArr[0].corner0 = Vector3(1,2,3); ? I tried this and it doesn't work. Is it possible and I'm just getting the syntax wrong, or am I barking up the wrong SpeedTree?
Aug 05 '10 at 01:38 PM
robinking
You can put whatever variables you like in the class. something like That should work - what errors are you getting?
Aug 05 '10 at 02:57 PM
cncguy
oh I see - don't forget to do this: multiArr[0] = new ThreeVecs();
Aug 05 '10 at 02:59 PM
cncguy
See you create an array of ThreeVecs but then you have to assign each ThreeVec object in the array with an instance of ThreeVec. Then you have to assign the class variables of each ThreeVec instance;
Aug 05 '10 at 03:01 PM
cncguy
Of course you can use the class constructor to assign the variables when you create each instance
Aug 05 '10 at 03:02 PM
cncguy
(comments are locked)
|
|
Multi-dimensional arrays are slightly slower than 1-dimensional arrays. It's unlikely to make any difference in practice though.
(comments are locked)
|
