You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Most of the methods in Mathematics have many overloads for different combinations of types. For example, math.abs() takes vector arguments, not just scalars, e.g. math.abs(new int3(5, -7, -1)) returns new int3(5, 7, 1). This cheat sheet does not exhaustively demonstrate all of the overloads. Consult the API reference for the full list.
int4i4=newint4(1,2,3,4);// x, y, z, wint2i2=int2.zero;// new int2(0, 0);// Index the components like an array.inti=i4[2];// int i = i4.zi4[0]=9;// i4.x = 9// Creating a vector by copying combinations// of values from another vector (swizzling).i4=i4.xwyy;// new int4(i4.x, i4.w, i4.y, i4.y);i2=i4.yz;// new int2(i4.y, i4.z);i4=i2.yxyx;// new int4(i2.y, i2.x, i2.y, i2.x);// Creating a vector from combinations of// lower-dimension vectors and scalars.i4=newint4(1,i2,3);// new int4(1, i2.x, i2.y, 3);i4=newint4(i2,i2);// new int4(i2.x, i2.y, i2.x, i2.y);i4=newint4(7);// new int4(7, 7, 7, 7);i2=newint2(7.5f);// new int2((int) 7.5f, (int) 7.5f);// Creating a vector by casting.i4=(int4)7;// new int4(7, 7, 7, 7);i2=(int2)7.5f;// new int2((int) 7.5f, (int) 7.5f);
Matrix creation and copying
// Values in row-major order.int2x3m=newint2x3(1,2,3,4,5,6);// first row: 1, 2, 3// second row: 4, 5, 6// First column: new int2(1, 4)int2i2=m.c0;// Third column: new int2(3, 6)i2=m.c2;// new int2x3(100, 100, 100, 100, 100, 100)m=newint2x3(100);m=newint2x3(newint2(1,2),// column 0newint2(3,4),// column 1newint2(5,6));// column 2// Converts each int component to a float.float2x3m2=newfloat2x3(m);
Vector and matrix operators
The vector and matrix operators (+, -, *, /, %, --, ++, ==, !=, <, >, <=, >=) operate upon corresponding component pairs:
int2a=newint2(1,2);int2b=newint2(3,4);// Addition.int2c=a+b;// new int2(a.x + b.x, a.y + b.y)// Negation.c=-a;// new int2(-a.x, -a.y)// Equality.boolmyBool=a.Equals(b);// a.x == b.x && a.y == b.ybool2myBool2=a==b;// new int2(a.x == b.x, a.y == b.y)// Greater than.myBool2=a>b;// new bool2(a.x > b.x, a.y > b.y)
The integer vector and matrix types also have bitwise operators: &, |, ~, <<, >>.
When hashing multiple values together, it's often more efficient to separately pass them to hashwide(), combine the results, then hash() the combination.
RigidTransform, float3x3, and float4x4 also have most of the same rotation methods as quaternion.
Generating random numbers
Randomrand=newRandom(123);// seed of 123// [-2147483647, 2147483647]intinteger=rand.NextInt();// [25, 100)integer=rand.NextInt(25,100);// x is [0, 1), y is [0, 1)float2f2=rand.NextFloat2();// x is [0, 7.5), y is [0, 11)f2=rand.NextFloat2(newfloat2(7.5f,11f));// x is [2, 7.5), y is [-4.6, 11)f2=rand.NextFloat2(newfloat2(2f,-4.6f),newfloat2(7.5f,11f));// Uniformly random unit-length direction vector.double3d3=rand.NextDouble3Direction();// Uniformly random unit-length quaternion.quaternionq=rand.NextQuaternionRotation();// Create multiple Random number generators using an incremented seed.NativeArray<Random>rngs=newNativeArray<Random>(10,Allocator.Temp);for(inti=0;i<10;i++){// Unlike the Random constructor, CreateFromIndex hashes the seed.// If we were to pass incremented seeds to the constructor,// each RNG would produce a similar stream of random numbers// as each other. Because we instead here use CreateFromIndex,// the RNG's will each produce streams of random numbers that// are properly distinct and unrelated to the others.rngs[i]=Random.CreateFromIndex((uint)(i+123));}