Skip to main content
Version: 14 Oct 2024

MLConvert

NameSpace: Native

Utility class used for converting vectors and matrices between native and unity format.

Public Methods

object ByteArrayToObject

Convert a byte array to an Object

public static object ByteArrayToObject(
byte [] byteArray
)

Parameters

TypeNameDescription
byte []byteArrayByte array to convert

Returns: Returns the newly converted object


string DecodeAscii

Decodes a buffer of bytes into an ASCII string.

public static string DecodeAscii(
byte [] buffer
)

Parameters

TypeNameDescription
byte []bufferbytes to convert to a string

Returns: A managed string


string DecodeUTF16BE

Converts an unmanaged UTF-16 string into a managed string.

public static string DecodeUTF16BE(
byte [] nativeArray
)

Parameters

TypeNameDescription
byte []nativeArrayNative byte array to convert

Returns: A managed string


string DecodeUTF16LE

Converts an unmanaged UTF-16 string into a managed string.

public static string DecodeUTF16LE(
byte [] nativeArray
)

Parameters

TypeNameDescription
byte []nativeArrayNative byte array to convert

Returns: A managed string


string DecodeUTF8

Decodes a buffer of bytes into a UTF8 string.

public static string DecodeUTF8(
byte [] buffer
)

Parameters

TypeNameDescription
byte []bufferbytes to convert to a UTF8 string

Returns: A managed string


string DecodeUTF8

Converts an unmanaged null terminated UTF-8 string into a managed string.

public static string DecodeUTF8(
IntPtr nativeString,
int maximumSize =-1
)

Parameters

TypeNameDescription
IntPtrnativeStringThe unmanaged string to convert
intmaximumSizemaximum number of characters to convert

Returns: A managed string


IntPtr EncodeToUnmanagedUTF8

Converts a managed string into an unmanaged null terminated UTF-8 string.

public static IntPtr EncodeToUnmanagedUTF8(
string s
)

Parameters

TypeNameDescription
stringsThe managed string to convert

Returns: A pointer to the unmanaged string


byte [] EncodeUTF8

This encodes the string into a UTF-8 byte array.

public static byte [] EncodeUTF8(
string decodedString
)

Parameters

TypeNameDescription
stringdecodedStringstring to encode

Returns: UTF8 string byte array


void FlipTransformMatrixHorizontally

public static void FlipTransformMatrixHorizontally(
float [] frameTransformMatColMajor
)

Parameters

TypeNameDescription
float []frameTransformMatColMajor

void FlipTransformMatrixVertically

public static void FlipTransformMatrixVertically(
float [] frameTransformMatColMajor
)

Parameters

TypeNameDescription
float []frameTransformMatColMajor

MagicLeapNativeBindings.MLTransform FromUnity

Creates native transform from a Unity matrix.

public static MagicLeapNativeBindings.MLTransform FromUnity(
Matrix4x4 mat,
bool transformFromRUF =true
)

Parameters

TypeNameDescription
Matrix4x4matA Unity matrix.
booltransformFromRUF(Optional) If false, prevents conversion to the native SDK coordinate system.
applyScale(Optional) If false, prevents scaling to the native SDK's unit per meter scale.

Returns: A native transform.


void FromUnity

Fills out array with values from 4x4 Unity matrix.

public static void FromUnity(
Matrix4x4 mat,
ref float [] matrixColMajor
)

Parameters

TypeNameDescription
Matrix4x4matAn input native matrix.
ref float []matrixColMajorAn array to populate in Unity format.

MagicLeapNativeBindings.MLVec3f FromUnity

Creates native 3d vector from a Unity vector.

public static MagicLeapNativeBindings.MLVec3f FromUnity(
Vector3 vec,
bool transformFromRUF =true
)

Parameters

TypeNameDescription
Vector3vecA Unity vector.
booltransformFromRUF(Optional) If false, prevents conversion to the native SDK coordinate system.
applyScale(Optional) If false, prevents scaling to the native SDK's unit per meter scale.

Returns: A native vector.


MagicLeapNativeBindings.MLQuaternionf FromUnity

Creates native quaternion from a Unity quaternion.

public static MagicLeapNativeBindings.MLQuaternionf FromUnity(
Quaternion quat,
bool transformFromRUF =true
)

Parameters

TypeNameDescription
QuaternionquatA Unity quaternion.
booltransformFromRUF(Optional) If false, prevents conversion to the native SDK coordinate system.

Returns: A native quaternion.


MagicLeapNativeBindings.MLUUID FromUnity

Creates an MLUUID from a System.Guid

public static MagicLeapNativeBindings.MLUUID FromUnity(
Guid guid
)

Parameters

TypeNameDescription
GuidguidA System.Guid

Returns: A native MLUUID


Vector3 GetPositionFromTransformMatrix

Gets the position vector stored in a transform matrix.

public static Vector3 GetPositionFromTransformMatrix(
Matrix4x4 transformMatrix
)

Parameters

TypeNameDescription
Matrix4x4transformMatrixA Unity matrix treated as a transform matrix.

Returns: A Unity vector representing a position.


Quaternion GetRotationFromTransformMatrix

Gets the rotation quaternion stored in a transform matrix.

public static Quaternion GetRotationFromTransformMatrix(
Matrix4x4 transformMatrix
)

Parameters

TypeNameDescription
Matrix4x4transformMatrixA Unity matrix treated as a transform matrix.

Returns: A Unity quaternion.


T [] MarshalUnmanagedArray< T >

Converts an unmanged array to a managed array of type T.

public static T [] MarshalUnmanagedArray< T >(
IntPtr arrayPtr,
int count
)

Parameters

TypeNameDescription
IntPtrarrayPtr
intcount

byte [] ObjectToByteArray< T >

Convert an object to a byte array. Uses C# Binary formatter to serialize

public static byte [] ObjectToByteArray< T >(
T obj
)

Parameters

TypeNameDescription
TobjObject to convert

Template Parameters:

  • T Data type of object

Returns: Returns a binary array representation of the object


byte [] ToUTF8Snipped

Take a string, snips it to a desired length and converts it to UTF8.

public static byte [] ToUTF8Snipped(
string inString,
int snipLength
)

Parameters

TypeNameDescription
stringinStringString to snip and convert
intsnipLengthlength to snip to

Returns: UTF8 string byte array


Vector3 ToUnity

Converts a Vector3 to Unity coordinate space and scale.

public static Vector3 ToUnity(
Vector3 vec,
bool transformToRUF =true
)

Parameters

TypeNameDescription
Vector3vecVector to convert
booltransformToRUFIf coordinate space should change.
applyScaleIf world scale should be applied.

Returns: Converted Vector


Vector3 ToUnity

Creates a Unity 3D vector from a native vector.

public static Vector3 ToUnity(
MagicLeapNativeBindings.MLVec3f vec,
bool transformToRUF =true
)

Parameters

TypeNameDescription
MagicLeapNativeBindings.MLVec3fvecA native vector.
booltransformToRUF(Optional) If false, prevents conversion to Unity's coordinate system.
applyScale(Optional) If false, prevents scaling to Unity's unit per meter scale.

Returns: A Unity vector.


Vector2 ToUnity

Creates a Unity 2D vector from a native vector.

public static Vector2 ToUnity(
MagicLeapNativeBindings.MLVec2f vec,
bool transformToRUF =true
)

Parameters

TypeNameDescription
MagicLeapNativeBindings.MLVec2fvecA native vector.
booltransformToRUF(Optional) If false, prevents conversion to Unity's coordinate system.
applyScale(Optional) If false, prevents scaling to Unity's unit per meter scale.

Returns: A Unity vector.


Vector3 ToUnity

Creates a Unity 3D vector from a x, y and z parameters.

public static Vector3 ToUnity(
float x,
float y,
float z,
bool transformToRUF =true
)

Parameters

TypeNameDescription
floatxX component
floatyY component
floatzZ component
booltransformToRUF(Optional) If false, prevents conversion to Unity's coordinate system.
applyScale(Optional) If false, prevents scaling to Unity's unit per meter scale.

Returns: A Unity vector.


Quaternion ToUnity

Creates a Unity quaternion from a native quaternion.

public static Quaternion ToUnity(
MagicLeapNativeBindings.MLQuaternionf quat,
bool transformToRUF =true
)

Parameters

TypeNameDescription
MagicLeapNativeBindings.MLQuaternionfquatA native quaternion.
booltransformToRUF(Optional) If false, prevents conversion to Unity's coordinate system.

Returns: A Unity quaternion.


Quaternion ToUnity

Converts a Quaternion to unity space.

public static Quaternion ToUnity(
Quaternion quat,
bool transformToRUF =true
)

Parameters

TypeNameDescription
QuaternionquatQuaternion to convert.
booltransformToRUF(Optional) If false, prevents conversion to Unity's coordinate system.

Returns: A Unity quaternion.


Matrix4x4 ToUnity

Creates Unity 4x4 matrix from native matrix.

public static Matrix4x4 ToUnity(
MagicLeapNativeBindings.MLMat4f mat
)

Parameters

TypeNameDescription
MagicLeapNativeBindings.MLMat4fmatA native matrix.

Returns: A Unity matrix.


Matrix4x4 ToUnity

Creates Unity 4x4 matrix from native transform.

public static Matrix4x4 ToUnity(
MagicLeapNativeBindings.MLTransform transform,
bool transformToRUF =true
)

Parameters

TypeNameDescription
MagicLeapNativeBindings.MLTransformtransformA native transform.
booltransformToRUF(Optional) If false, prevents conversion to Unity's coordinate system.
applyScale(Optional) If false, prevents scaling to Unity's unit per meter scale.

Returns: A Unity matrix.


Guid ToUnity

Creates a System.Guid from an MLUUID

public static Guid ToUnity(
MagicLeapNativeBindings.MLUUID uuid
)

Parameters

TypeNameDescription
MagicLeapNativeBindings.MLUUIDuuidA native UUID

Returns: A System.Guid


Public Attributes

IdentityMatrixColMajor


public static readonly float [] IdentityMatrixColMajor = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };