Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit conversions for Unity vectors #75

Open
ds5678 opened this issue Feb 13, 2023 · 0 comments
Open

Implicit conversions for Unity vectors #75

ds5678 opened this issue Feb 13, 2023 · 0 comments
Labels
enhancement New feature or request generation Related to assembly generation

Comments

@ds5678
Copy link
Collaborator

ds5678 commented Feb 13, 2023

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:

  • UnityEngine.Vector2 <-> System.Numerics.Vector2
  • UnityEngine.Vector3 <-> System.Numerics.Vector3
  • UnityEngine.Vector4 <-> System.Numerics.Vector4
  • UnityEngine.Quaternion <-> System.Numerics.Quaternion
  • UnityEngine.Plane <-> System.Numerics.Plane
  • UnityEngine.Vector2 <-> System.Drawing.PointF
  • UnityEngine.Vector2Int <-> System.Drawing.Point
  • UnityEngine.Vector2 <-> System.Drawing.SizeF
  • UnityEngine.Vector2Int <-> System.Drawing.Size
  • UnityEngine.Rect <-> System.Drawing.RectangleF
  • UnityEngine.RectInt <-> System.Drawing.Rectangle

Other conversions may require some small processing:

  • UnityEngine.Matrix4x4 <-> System.Numerics.Matrix4x4
  • UnityEngine.RangeInt <-> System.Range

Other conversions may require some medium processing:

  • UnityEngine.Color32 <-> System.Drawing.Color

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);
}
@ds5678 ds5678 added enhancement New feature or request generation Related to assembly generation labels Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request generation Related to assembly generation
Projects
None yet
Development

No branches or pull requests

1 participant