/// <summary> /// Converts playlist URLs to UTF8 and sets it to use for the playlist in the MusicService. /// </summary> /// <param name="playlist">The array of URLs</param> /// <param name="size">The size of the playList array</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successful. /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to invalid input parameter. /// MLResult.Result will be <c>MLResult.Code.PrivilegeDenied</c> if necessary privilege is missing. /// MLResult.Result will be <c>MLResult.Code.MediaGenericNoInit</c> if not connected. /// </returns> public static MLResult.Code SetPlayListHelper(string[] playlist, ulong size) { try { IntPtr[] intPtrList = new IntPtr[playlist.Length]; for (int i = 0; i < playlist.Length; ++i) { intPtrList[i] = MLConvert.EncodeToUnmanagedUTF8(playlist[i]); } MLResult.Code resultCode = MLMusicServiceNativeBindings.MLMusicServiceSetPlayList(intPtrList, size); for (int i = 0; i < intPtrList.Length; ++i) { Marshal.FreeHGlobal(intPtrList[i]); } return(resultCode); } catch (System.EntryPointNotFoundException) { MLPluginLog.Error("MLMusicServiceNativeBindings.SetPlayListHelper failed. Reason: API symbols not found"); return(MLResult.Code.UnspecifiedFailure); } }
/// <summary> /// Creates native 3d vector from a Unity vector. /// </summary> /// <param name="vec">A Unity vector.</param> /// <param name="transformFromRUF">(Optional) If false, prevents conversion to the native SDK coordinate system.</param> /// <param name="applyScale">(Optional) If false, prevents scaling to the native SDK's unit per meter scale.</param> /// <returns>A native vector.</returns> public static MagicLeapNativeBindings.MLVec3f FromUnity(Vector3 vec, bool transformFromRUF = true, bool applyScale = true) { if (transformFromRUF) { vec.z = -vec.z; } if (applyScale) { if (MLDevice.WorldScale == 0.0f) { MLPluginLog.Error("Divide by zero, unit scale vector contains 0"); } else { vec = vec / MLDevice.WorldScale; } } MagicLeapNativeBindings.MLVec3f outVec = new MagicLeapNativeBindings.MLVec3f(); outVec.X = vec.x; outVec.Y = vec.y; outVec.Z = vec.z; return(outVec); }
/// <summary> /// Reads the JSON file and stores the results into the list of serialized bindings. /// </summary> /// <returns>The list of bindings found in the JSON file.</returns> public List <T> LoadFromFile() { if (Bindings.Count != 0) { return(Bindings); } string fullPath = Path.Combine(Application.persistentDataPath, this.fileName); if (fullPath != null && File.Exists(fullPath)) { StreamReader reader = new StreamReader(fullPath); if (reader != null) { string jsonString = reader.ReadToEnd(); this.serializedData = JsonUtility.FromJson <SerializeBindings>(jsonString); reader.Dispose(); } else { MLPluginLog.Error("BindingsLocalStorage.LoadFromFile failed to create StreamReader."); return(new List <T>()); } } else { MLPluginLog.WarningFormat("File was not found: {0}", fullPath); return(new List <T>()); } return(Bindings); }
/// <summary> /// Convert Unity Texture2D to a byte array. /// Texture2D should be in one of the following formats: DXT1, RGBA32, RGB24. /// For DXT1 and RGBA32, resulting byte array is in RGBA format. /// For RGB24, resulting byte array is in RGB format. /// The origin of the byte array will be at the top left corner. /// Returns null on unsupported formats. /// </summary> /// <param name="texture">Texture to extract byte array from</param> /// <param name="numChannels">Out parameter to determine how many bytes per pixel</param> /// <returns>Returns an array of bytes that holds the converted texture</returns> public static byte[] ConvertToByteArray(Texture2D texture, out int numChannels) { byte[] encodedImage = null; numChannels = 0; // [1] Convert the unity texture to RGBA pixel format (if needed). // For a pixel at (x,y) the channel c data of that pixels is at the position // given by [(width * y + x)*channels + c]. if (TextureFormat.DXT1 == texture.format) { Color32[] colorArray = texture.GetPixels32(); numChannels = Marshal.SizeOf(typeof(Color32)); int totalBytes = numChannels * colorArray.Length; encodedImage = new byte[totalBytes]; GCHandle handle = default(GCHandle); try { handle = GCHandle.Alloc(colorArray, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); Marshal.Copy(ptr, encodedImage, 0, totalBytes); } finally { if (handle != default(GCHandle)) { handle.Free(); } } } else if (TextureFormat.RGBA32 == texture.format) { numChannels = 4; encodedImage = texture.GetRawTextureData(); } else if (TextureFormat.RGB24 == texture.format) { numChannels = 3; encodedImage = texture.GetRawTextureData(); } else { MLPluginLog.Error("MLTextureUtils.ConvertToByteArray failed to convert to byte array. Reason: unsupported format. Use RGBA32, RGB24 or DXT1 format when importing textures."); return(encodedImage); } // [2] Convert to coorindate frame used by Lumin OS // Unity texture origin in bottom left, but Lumin OS is expecting top left to be the origin. int rowLength = texture.width * numChannels; var rowTemp = new byte[rowLength]; for (var i = 0; i < texture.height / 2; i++) { Buffer.BlockCopy(encodedImage, i * rowLength, rowTemp, 0, rowLength); Buffer.BlockCopy(encodedImage, (texture.height - i - 1) * rowLength, encodedImage, i * rowLength, rowLength); Buffer.BlockCopy(rowTemp, 0, encodedImage, (texture.height - i - 1) * rowLength, rowLength); } return(encodedImage); }
/// <summary> /// Converts URL string to UTF8 and sets it for the MusicService. /// </summary> /// <param name="url">The URL to play</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successful. /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to invalid input parameter. /// MLResult.Result will be <c>MLResult.Code.PrivilegeDenied</c> if necessary privilege is missing. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error. /// MLResult.Result will be <c>MLResult.Code.MediaGenericNoInit</c> if not connected. /// </returns> public static MLResult.Code SetURLHelper(string url) { try { IntPtr urlPtr = MLConvert.EncodeToUnmanagedUTF8(url); MLResult.Code resultCode = MLMusicServiceNativeBindings.MLMusicServiceSetURL(urlPtr); Marshal.FreeHGlobal(urlPtr); return(resultCode); } catch (System.EntryPointNotFoundException) { MLPluginLog.Error("MLMusicServiceNativeBindings.SetURLHelper failed. Reason: API symbols not found"); return(MLResult.Code.UnspecifiedFailure); } }
private bool DestroyPurchaseHistoryQuery(ulong handle) { if (MagicLeapNativeBindings.MLHandleIsValid(handle)) { MLResult.Code result = MLPurchaseNativeBindings.MLPurchaseHistoryQueryDestroy(handle); if (result == MLResult.Code.Ok) { return(true); } MLPluginLog.ErrorFormat("MLPurchase.DestroyPurchaseHistoryQuery failed to free purchase history query handle. Reason: {0}", GetResultString(result)); return(false); } MLPluginLog.Error("MLPurchase.DestroyPurchaseHistoryQuery failed, handle is invalid."); return(false); }
/// <summary> /// Request for the details for the given IAP ids /// </summary> /// <param name="itemIds">Array of IAP ids</param> /// <param name="callback">Method to be called when the query is completed</param> /// <returns> /// MLResult.Result will be MLResult.Code.Ok if successful. /// /// MLResult.Result will be MLResult.Code.AllocFailed if the handle cannot be allocated. /// /// MLResult.Result will be MLResult.Code.UnspecifiedFailure if there is an unexpected failure. /// /// MLResult.Result will be MLResult.Code.InvalidParam if any input parameters are invalid. /// /// MLResult.Result will be MLResult.Code.PrivilegeDenied if there is an privilege error with the purchase details system call. /// /// MLResult.Result will be MLResult.Code.CloudSystemError if there is an issue with the cloud service, e.g. service is not available for any reason. /// </returns> public static MLResult GetItemDetails(string[] itemIds, Action <MLPurchaseItemDetail> callback) { if (!IsStarted) { MLResult result = MLResult.Create(MLResult.Code.UnspecifiedFailure, "Please call MLPurchase.Start() first."); MLPluginLog.ErrorFormat("MLPurchase.GetItemDetails failed to get item details. Reason: {0}", result); return(result); } if (itemIds == null || itemIds.Length == 0 || callback == null) { MLResult result = MLResult.Create(MLResult.Code.InvalidParam, "Invalid GetItemDetails params."); MLPluginLog.Error(result); return(result); } return(Instance.GetItemDetailsInternal(itemIds, callback)); }
/// <summary> /// Create an initialized version of this struct. /// </summary> /// <returns>An initialized version of this struct.</returns> public static SettingsNative Create() { if (!defaultSettings.HasValue) { SettingsNative settings = new SettingsNative() { Version = 1u, SwayHistorySize = 0u, MaxDeltaAngle = 0.0f, ControlDampeningFactor = 0.0f, MaxSwayAngle = 0.0f, MaximumHeadposeRotationSpeed = 0.0f, MaximumHeadposeMovementSpeed = 0.0f, MaximumDepthDeltaForSway = 0.0f, MinimumDistance = 0.0f, MaximumDistance = 0.0f, MaximumSwayTimeSeconds = 0.0f, EndResolveTimeoutSeconds = 0.0f, }; try { MLResult.Code result = NativeBindings.MLMovementGetDefaultSettings(out settings); if (result != MLResult.Code.Ok) { MLPluginLog.ErrorFormat("MLMovement.NativeBindings.SettingsNative.Create failed to get default movement settings. Reason: {0}", result); } } catch (System.DllNotFoundException) { MLPluginLog.Error("MLMovement API is currently available only on device."); } catch (System.EntryPointNotFoundException) { MLPluginLog.Error("MLMovement API symbols not found"); } defaultSettings = settings; } return(defaultSettings.Value); }
/// <summary> /// Allocates the static pointer to the CallbacksNative passed in. This is used to pass it into the CAPI. /// </summary> /// <returns>The pointer to the native callbacks.</returns> public static IntPtr AllocateSystemCallbacks() { if (SystemCallbacks != IntPtr.Zero) { MLPluginLog.Error("MLMusicServiceNativeBindings.CallbacksNative.AllocateCallbacksPointer failed to allocate new callbacks pointer. Reason: Static callback pointer was already allocated, please call MLMusicServiceNativeBindings.CallbacksNative.DeallocateCallbacksPointer and try again"); return(IntPtr.Zero); } SystemCallbacks = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CallbacksNative))); CallbacksNative callbacks = CallbacksNative.Create(); callbacks.OnPlaybackStateChange = HandlePlaybackStateChange; callbacks.OnRepeatStateChange = HandleRepeatStateChange; callbacks.OnShuffleStateChange = HandleShuffleStateChange; callbacks.OnMetadataChange = HandleMetadataChange; callbacks.OnPositionChange = HandlePositionChange; callbacks.OnError = HandleError; callbacks.OnStatusChange = HandleStatusChange; callbacks.OnVolumeChange = HandleVolumeChange; Marshal.StructureToPtr(callbacks, SystemCallbacks, false); return(SystemCallbacks); }