BepInEx / Il2CppInterop

A tool interoperate between CoreCLR and Il2Cpp at runtime
GNU Lesser General Public License v3.0
206 stars 66 forks source link

Implicit conversions for Unity vectors #75

Open ds5678 opened 1 year ago

ds5678 commented 1 year ago

It would be convenient to have bidirectional implicit conversions for common mathematical value types.

Some conversions can be done purely with pointers since they contain the same memory layout:

Other conversions may require some small processing:

Other conversions may require some medium processing:

Matrix4x4

Matrix4x4 might need to be transposed because M14 maps to m30 in the field layouts. The possible necessity of transposition can be verified with vector math.

Range

Both are the same size, but Unity stores "start" and "length" whereas CoreCLR stores "start" and "end."

Color

The CoreCLR color has a completely different memory layout. Properties and methods must be used to convert.

public static implicit operator UnityEngine.Color32(System.Drawing.Color color)
{
    return new UnityEngine.Color32(color.R, color.G, color.B, color.A);
}
public static implicit operator System.Drawing.Color(UnityEngine.Color32 color)
{
    return System.Drawing.Color.FromArgb(color.a, color.r, color.g, color.b);
}