Skip to content
Cauê Waneck edited this page Jul 13, 2014 · 2 revisions

The following information applies to the following types: Bounds, Color32, Color, Matrix4x4, Quaternion, Rect, Vector2, Vector3, Vector4. If you feel that any type is missing and should be added, please submit a bug report! Avaiable in the development branch


In order to make it easier to work with the core Unity3D structs, some facilities have been added to unihx:

##Operator overloading Operator overloading works the same as in C# or UnityScript - all operator overloading for these types are available:

###Addition / Negation / Multiplication

var vec1 = new Vector4(1,2,3), //you don't have to define all fields
    vec2 = new Vector4(4,5,6);
var vec = vec1 + vec2; //5,7,9,0
vec = vec1 * 10; //10,20,30,0
vec /= 10;//1,2,3,0

###Array access You will need the latest Haxe to be able to use this correctly

var vec1 = new Vector4(1,2,3,4);
trace(vec1[0]); // 1
trace(vec1[1]); // 2
vec1[0] = 10;
trace(vec1); // 10,2,3,4

###Implicit conversion unihx supports the same implicit conversions as C#:

var vec4 = new Vector4(1,2,3,4);
var vec3:Vector3 = vec4; //1,2,3
var vec2:Vector2 = vec4; //1,2
vec3 = vec2; //1,2,0
vec4 = vec2; //1,2,0,0

##Additional facilities Besides from supporting the same features as C# or UnityScript, unihx supports some additional features that make it even easier to work with the core Unity structures: ###.with() All core structures support the with() macro, which creates a shallow copy of the desired object, except for the fields defined in the argument:

var vec = new Vector4(1,2,3,4);
var v2 = vec.with({ y:200 });
//is equivalent to new Vector4(vec.x, 200, vec.z, vec.w);
v2 = vec.with({ x:1000, z:300 });
//equivalent to new Vector4(1000,vec.y,300,vec.w);

Since in many cases when working with the core structures, you need to update a value from another structure, this makes it a lot easier to accomplish that. For example the following code:

this.transform.position = new Vector3(this.transform.position.x, 100, this.transform.position.z);

can be simplified into:

this.tranform.position = this.transform.position.with({ y:100 });
Clone this wiki locally