/// <summary> /// Start up /// Note: This requires the privilege to be granted prior to Start() /// </summary> void Start() { MLResult result = MLPersistentStore.Start(); if (!result.IsOk) { Debug.LogErrorFormat("MLPersistentPoint failed starting MLPersistentStore, disabling script. Reason: {0}", result); enabled = false; return; } result = MLPersistentCoordinateFrames.Start(); if (!result.IsOk) { MLPersistentStore.Stop(); Debug.LogErrorFormat("MLPersistentPoint failed starting MLPersistentCoordinateFrames, disabling script. Reason: {0}", result); enabled = false; return; } if (string.IsNullOrEmpty(UniqueId)) { Debug.LogWarning("Unique Id is empty will try to use game object's name. It's good to provide a unique id for virtual objects to avoid weird behavior."); if (string.IsNullOrEmpty(gameObject.name)) { SetError(new MLResult(MLResultCode.UnspecifiedFailure, "Either UniqueId or name should be non empty. Disabling component")); enabled = false; return; } UniqueId = gameObject.name; } else { gameObject.name = UniqueId; } if (MLPersistentCoordinateFrames.IsReady) { RestoreBinding(gameObject.name); } else { MLPersistentCoordinateFrames.OnReady += HandleReady; } }
/// <summary> /// Attempts to start the MLPersistentStore and MLPersistentCoordinateFrames APIs /// </summary> void StartAPIs() { MLResult result = MLPersistentStore.Start(); if (!result.IsOk) { Debug.LogErrorFormat("MLPersistentBehavior failed starting MLPersistentStore, disabling script. Reason: {0}", result); enabled = false; return; } result = MLPersistentCoordinateFrames.Start(); if (!result.IsOk) { MLPersistentStore.Stop(); Debug.LogErrorFormat("MLPersistentBehavior failed starting MLPersistentCoordinateFrames, disabling script. Reason: {0}", result); enabled = false; return; } if (string.IsNullOrEmpty(UniqueId)) { Debug.LogWarning("Unique Id is empty will try to use game object's name. It's good to provide a unique id for virtual objects to avoid weird behavior."); if (string.IsNullOrEmpty(gameObject.name)) { Debug.LogError("Either UniqueId or name should be non empty. Disabling component"); enabled = false; return; } UniqueId = gameObject.name; } if (MLPersistentCoordinateFrames.IsReady) { CreateOrRestoreBinding(); } else { MLPersistentCoordinateFrames.OnInitialized += HandleInitialized; } }
/// <summary> /// Start this instance. /// </summary> void Start() { MLResult result = MLPersistentStore.Start(); if (!result.IsOk) { SetError("Failed to start persistent store. Disabling component"); enabled = false; return; } result = MLPersistentCoordinateFrames.Start(); if (!result.IsOk) { MLPersistentStore.Stop(); SetError("Failed to start coordinate frames system. disabling component"); enabled = false; return; } if (_representativePrefab == null) { SetError("Error: _representativePrefab must be set"); enabled = false; return; } List <MLPCF> pcfList; result = MLPersistentCoordinateFrames.GetAllPCFs(out pcfList, int.MaxValue); if (!result.IsOk) { MLPersistentStore.Stop(); MLPersistentCoordinateFrames.Stop(); SetError(result.ToString()); enabled = false; return; } TryShowingAllPCFs(pcfList); }
/// <summary> /// Finds the closest pcf for this persistent point. /// </summary> void BindToAllPCFs() { _state = State.BindToAllPCFs; string suffix = ""; int count = 0; // In the loop below we try to associate the persitent point with not only // the closest but all pcfs in the surrounding. This will increase the probablilty // of restoration on reboots. It's costly in terms of disk space so we will limit it to // a max foreach (MLPCF pcf in _allPCFs) { string objectName = gameObject.name + suffix; var returnResult = MLPersistentCoordinateFrames.GetPCFPosition(pcf, (result, returnPCF) => { if (result.IsOk && pcf.CurrentResult == MLResultCode.Ok) { Debug.Log("binding to PCF: " + pcf.CFUID); Binding = MLContentBinder.BindToPCF(objectName, gameObject, pcf); MLPersistentStore.Save(Binding); } else { Debug.LogWarningFormat("Failed to find the position for PCF {0}", returnPCF.CFUID); } }); if (!returnResult.IsOk) { Debug.LogError("Failed to GetPCF"); break; } suffix = "-" + count; count++; } _state = State.BindingComplete; }
/// <summary> /// Tries the showing all PCF. /// </summary> /// <param name="pcfList">Pcf list.</param> void TryShowingAllPCFs(List <MLPCF> pcfList) { foreach (MLPCF pcf in pcfList) { if (pcf.CurrentResult == MLResultCode.Pending) { MLPersistentCoordinateFrames.GetPCFPosition(pcf, (r, p) => { if (r.IsOk) { AddPCFObject(p); } else { SetError("failed to get position for pcf : " + p); } }); } else { AddPCFObject(pcf); } } }
/// <summary> /// Provides the string value for any MLResult.Code. /// </summary> /// <param name="resultCode">The code to convert into a string value.</param> /// <returns>The string value of the given MLResult.Code.</returns> public static string CodeToString(MLResult.Code resultCode) { string codeString = string.Empty; switch ((CodePrefix)((int)resultCode >> 16)) { case CodePrefix.MLResultGlobal: case CodePrefix.MLSnapshotResult: codeString = Marshal.PtrToStringAnsi(MagicLeapNativeBindings.MLSnapshotGetResultString(resultCode)); break; case CodePrefix.MLAudioResult: codeString = Marshal.PtrToStringAnsi(MLAudio.GetResultString(resultCode)); break; case CodePrefix.MLMediaDRMResult: case CodePrefix.MLMediaGenericResult: case CodePrefix.MLMediaPlayerResult: case CodePrefix.MLMediaResult: codeString = Marshal.PtrToStringAnsi(MLMediaPlayer.GetResultString(resultCode)); break; case CodePrefix.MLDispatchResult: codeString = Marshal.PtrToStringAnsi(MLDispatch.GetResultString(resultCode)); break; case CodePrefix.MLIdentityResult: codeString = Marshal.PtrToStringAnsi(MLIdentity.GetResultString(resultCode)); break; case CodePrefix.MLPassableWorldResult: codeString = Marshal.PtrToStringAnsi(MLPersistentCoordinateFrames.GetResultString(resultCode)); break; case CodePrefix.MLTokenAgentResult: codeString = Marshal.PtrToStringAnsi(MLTokenAgent.GetResultString(resultCode)); break; case CodePrefix.MLPrivilegesResult: codeString = Marshal.PtrToStringAnsi(MLPrivileges.GetResultString(resultCode)); break; case CodePrefix.MLContactsResult: codeString = Marshal.PtrToStringAnsi(MLContacts.GetResultString(resultCode)); break; case CodePrefix.MLLocationResult: codeString = Marshal.PtrToStringAnsi(MLLocation.GetResultString(resultCode)); break; case CodePrefix.MLNetworkingResult: codeString = Marshal.PtrToStringAnsi(MLNetworkingNativeBindings.MLNetworkingGetResultString(resultCode)); break; case CodePrefix.MLMovementResult: codeString = Marshal.PtrToStringAnsi(MLMovement.GetResultString(resultCode)); break; case CodePrefix.MLConnectionsResult: codeString = Marshal.PtrToStringAnsi(MLConnections.GetResultString(resultCode)); break; case CodePrefix.MLSecureStorageResult: codeString = Marshal.PtrToStringAnsi(MLSecureStorageNativeBindings.MLSecureStorageGetResultString(resultCode)); break; case CodePrefix.MLAppConnect: codeString = Marshal.PtrToStringAnsi(MLAppConnectNativeBindings.MLAppConnectGetResultString(resultCode)); break; case CodePrefix.MLWebRTC: codeString = Marshal.PtrToStringAnsi(MLWebRTC.NativeBindings.MLWebRTCGetResultString(resultCode)); break; case CodePrefix.MLBluetoothGattResult: codeString = Marshal.PtrToStringAnsi(MLBluetoothLE.NativeBindings.MLBluetoothGattGetResultString(resultCode)); break; default: // This will catch any unknown/invalid return values. codeString = MagicLeapNativeBindings.MLGetResultString(resultCode); break; } return(codeString); }