Is there a way to make a structure in JavaScript? I mean not class but structure as Vector3 for example. For creating big fast arrays. Something like this:
struct SomeStruct{
var i : int;
var f : float;
}
var arr = new SomeStruct[1000];
arr[500].a = 3;
In a case of class I need to initialize every member of the array...
Building on the example given by Senhor de todo o Mal.
Save the following script in your Standard Assets folder:
class SomeStruct extends System.ValueType{
//fields
var i : int;
var f : float;
//Constructor
public function SomeStruct(i:int,f:float){
this.i = i;
this.f = f;
}
//Method
public function PowerOfI(a:int)
{
for (var index:int = 1; index<a; ++index)
{
this.i = this.i * this.i;
}
}
}
Now create a new js script as follows:
function Start () {
//Make a new SomeStruct with uninitialized fields
var myStruct:SomeStruct = new SomeStruct();
//Set field i
myStruct.i =5;
//Test it
Debug.Log(myStruct.i);
//Make a new SomeStruct with initialized fields
var myStruct2:SomeStruct = new SomeStruct(5, 2.3);
//Test it
Debug.Log(myStruct2.f);
//Use a struct method
myStruct2.PowerOfI(2);
//Test it
Debug.Log(myStruct2.i);
}
So yes, you can create structures in Unity JavaScript.
This has annoyed me for days. Doing it the way Senhor de todo o Mal did it does not make the struct show up in the editor. Then I found this in another answer:
class Test extends System.Object {
var p = 5;
var c = Color.white;
}
var test = Test ();
And now it shows up in the editor as well. I hope it saves others some time.