#if UNITY_ANDROID && !UNITY_EDITOR #define XREALPLUGIN_SUPPORTS_TARGET_PLATFORM #endif using System; using System.Runtime.InteropServices; using UnityEngine; namespace Unity.XR.XREAL { /// /// Delegate for handling RGB camera data callbacks. /// /// The data frame from the RGB camera. /// A user-defined pointer for additional data. public delegate void RGBCameraDataCallback(RGBCameraDataFrame rgbCameraData, IntPtr userData); /// /// Represents a single frame of RGB camera data. /// The timestamp of frame data. /// The resolution(width and height) of frame data. /// The raw data's size of frame data. /// The raw data of frame data. /// public struct RGBCameraDataFrame { public ulong timeStamp; public Vector2Int resolution; public ulong rawDataSize; public IntPtr rawData; } /// /// Provides a set of utility functions and events for interacting with the XREAL XR Plugin. /// public static partial class XREALPlugin { /// /// Starts capturing data from the RGB camera. /// /// The capture session ID. public static ulong StartRGBCameraDataCapture() { #if XREALPLUGIN_SUPPORTS_TARGET_PLATFORM return Internal.StartRGBCameraDataCapture(null, IntPtr.Zero); #else return 0; #endif } /// /// Stops the currently active RGB camera data capture. /// /// True if the capture was successfully stopped; otherwise, false. public static bool StopRGBCameraDataCapture() { #if XREALPLUGIN_SUPPORTS_TARGET_PLATFORM return Internal.StopRGBCameraDataCapture(0); #else return false; #endif } /// /// Attempts to get the timestamp of the latest RGB camera frame. /// /// The timestamp of the frame. /// True if the timestamp was successfully retrieved; otherwise, false. public static bool TryGetRGBCameraFrame(ref ulong timeStamp) { #if XREALPLUGIN_SUPPORTS_TARGET_PLATFORM return Internal.TryGetRGBCameraFrame(ref timeStamp); #else return false; #endif } /// /// Attempts to acquire the latest image from the RGB camera. /// /// The handle for the acquired frame. /// The resolution of the acquired frame. /// The timestamp of the acquired frame. /// True if the image was successfully acquired; otherwise, false. public static bool TryAcquireLatestImage(ref int frameHandle, ref Vector2Int resolution, ref ulong timeStamp) { #if XREALPLUGIN_SUPPORTS_TARGET_PLATFORM return Internal.TryAcquireLatestImage(ref frameHandle, ref resolution, ref timeStamp); #else return false; #endif } /// /// Attempts to get the data plane of a specific frame from the RGB camera. /// /// The handle for the frame. /// The index of the data plane. /// A pointer to the data plane. /// The size of the data plane. /// True if the data plane was successfully retrieved; otherwise, false. public static bool TryGetRGBCameraDataPlane(int frameHandle, int planeIndex, out IntPtr dataPtr, out Vector2Int size) { #if XREALPLUGIN_SUPPORTS_TARGET_PLATFORM return Internal.TryGetRGBCameraDataPlane(frameHandle, planeIndex, out dataPtr, out size); #else dataPtr = IntPtr.Zero; size = default; return false; #endif } /// /// Checks whether a given RGB camera data handle is valid. /// /// The handle to validate. /// True if the handle is valid; otherwise, false. public static bool IsRGBCameraDataHandleValid(int frameHandle) { #if XREALPLUGIN_SUPPORTS_TARGET_PLATFORM return Internal.IsRGBCameraDataHandleValid(frameHandle); #else return false; #endif } /// /// Releases resources associated with a specific RGB camera data handle. /// /// The handle to dispose. public static void DisposeRGBCameraDataHandle(int frameHandle) { #if XREALPLUGIN_SUPPORTS_TARGET_PLATFORM Internal.DisposeRGBCameraDataHandle(frameHandle); #endif } private static partial class Internal { [DllImport(LibName)] internal static extern ulong StartRGBCameraDataCapture(RGBCameraDataCallback callback, IntPtr userData); [DllImport(LibName)] internal static extern bool StopRGBCameraDataCapture(ulong callbackHandle); [DllImport(LibName)] internal static extern bool TryGetRGBCameraFrame(ref ulong timeStamp); [DllImport(LibName)] internal static extern bool TryAcquireLatestImage(ref int frameHandle, ref Vector2Int resolution, ref ulong timeStamp); [DllImport(LibName)] public static extern bool TryGetRGBCameraDataPlane(int frameHandle, int planeIndex, out IntPtr dataPtr, out Vector2Int size); [DllImport(LibName)] internal static extern bool IsRGBCameraDataHandleValid(int frameHandle); [DllImport(LibName)] internal static extern void DisposeRGBCameraDataHandle(int frameHandle); } } }