/// <summary> /// Loads the bindings from the json file /// </summary> void LoadBindings() { _state = State.RestoreInProgress; foreach (var exampleObject in _exampleObjects) { SetProgress(string.Format(TEXT_RESTORING_OBJECT, exampleObject.GO.name, "started")); if (MLPersistentStore.Contains(exampleObject.GO.name)) { MLContentBinding binding; MLResult result = MLPersistentStore.Load(exampleObject.GO.name, out binding); if (!result.IsOk) { Debug.LogError("Failed to load binding for game object " + exampleObject.GO.name); } else { binding.GameObject = exampleObject.GO; exampleObject.Binding = binding; Debug.LogFormat("Binding loaded from the store: " + "Id: {0} \n" + "PCFID: {1}\n", binding.ObjectId, binding.PCF.CFUID); MLContentBinder.Restore(binding, HandleBindingRestore); } } else { SetProgress(string.Format(TEXT_RESTORING_OBJECT, exampleObject.GO.name, "failed")); } } }
/// <summary> /// Tries to restore the binding from persistent storage and PCF system /// </summary> IEnumerator RestoreBinding(string objId) { _state = State.RestoreBinding; if (MLPersistentStore.Contains(objId)) { MLContentBinding binding; MLResult result = MLPersistentStore.Load(objId, out binding); if (!result.IsOk) { SetError(result); _state = State.BindingComplete; } else { Binding = binding; Debug.Log("binding result : " + Binding.PCF.CurrentResult); Binding.GameObject = this.gameObject; MLContentBinder.Restore(Binding, HandleBindingRestore); } } else { BindToAllPCFs(); } while (_state != State.BindingComplete) { yield return(null); } yield break; }
/// <summary> /// Handler for restoring the bindings. This is called when the content binding /// is restored, and if this is successful, the object is rebound to the original /// location it was bound to when last saved. /// </summary> /// <param name="contentBinding">Content binding.</param> /// <param name="resultCode">Result code.</param> void HandleBindingRestore(MLContentBinding contentBinding, MLResult result) { if (result.IsOk) { SetProgress(string.Format(TEXT_RESTORING_OBJECT, contentBinding.GameObject.name, "succeeded")); contentBinding.GameObject.SetActive(true); Debug.LogFormat("object: {0} - {1} {2} {3} , {4} {5} {6} {7}", contentBinding.GameObject.name, contentBinding.GameObject.transform.position.x, contentBinding.GameObject.transform.position.y, contentBinding.GameObject.transform.position.z, contentBinding.GameObject.transform.rotation.x, contentBinding.GameObject.transform.rotation.y, contentBinding.GameObject.transform.rotation.z, contentBinding.GameObject.transform.rotation.w); } else { SetProgress(string.Format(TEXT_RESTORING_OBJECT, contentBinding.GameObject.name, "failed. Trying again")); //Note: Currently we try endlessly to restore. However, this can lead to a loop //when there's fundamental problem with the underlying system. This logic can be made smarter by //adding retry attempts however for sake of simplicitly we are not showing it here. It is advised //to do it however. MLContentBinder.Restore(contentBinding, HandleBindingRestore); } }
/// <summary> /// Loads the bindings from the json file /// </summary> public void LoadBindings() { _state = State.RestoreInProgress; if (!MLPersistentStore.IsStarted) { return; } if (MLPersistentStore.Contains(referencePointName)) { MLResult result = MLPersistentStore.Load(referencePointName, out alignedBinding); if (!result.IsOk) { Debug.LogError("Failed to load binding for reference point"); } else { AlignedReferencePoint refPoint = AlignedReferencePoint.instance; alignedBinding.GameObject = refPoint.gameObject; Debug.LogFormat("Binding loaded from the store: " + "Id: {0} \n" + "PCFID: {1}\n", alignedBinding.ObjectId, alignedBinding.PCF.CFUID); MLContentBinder.Restore(alignedBinding, HandleBindingRestore); } } else { AlignedReferencePoint.instance.ActionResetXzToTransformPosition(GlobalAppMonitor.mainCamera.transform); //SetProgress(string.Format(TEXT_RESTORING_OBJECT, referencePointName, "failed")); } }
/// <summary> /// Helper function to add new objects and binding them to closest PCFs. /// This function shows how youc an use the underlying systems to accomplish /// game object to a PCF binding /// </summary> public void StoreReferencePointAtPosition(Vector3 position) { if (!MLPersistentCoordinateFrames.IsStarted) { return; } var returnResult = MLPersistentCoordinateFrames.FindClosestPCF(position, (MLResult result, MLPCF pcf) => { if (result.IsOk) { Debug.LogFormat("Closest PCF found. Binding {0} to PCF {1}:", referencePointName, pcf.CFUID); if (alignedBinding == null) { alignedBinding = MLContentBinder.BindToPCF(referencePointName, AlignedReferencePoint.instance.gameObject, pcf); } else { alignedBinding.PCF = pcf; alignedBinding.Update(); } _state = State.SaveRequired; //Debug.LogFormat("object: {0} - {1} {2} {3}, {4} {5} {6} {7}", //newExampleObject.GO.name, //newExampleObject.GO.transform.position.x, //newExampleObject.GO.transform.position.y, //newExampleObject.GO.transform.position.z, //newExampleObject.GO.transform.rotation.x, //newExampleObject.GO.transform.rotation.y, //newExampleObject.GO.transform.rotation.z, //newExampleObject.GO.transform.rotation.w); } else { SetProgress(FAILED_TO_FIND_CLOSEST_PCF + " Reason:" + result); } }); if (!returnResult.IsOk) { SetProgress(FAILED_TO_FIND_CLOSEST_PCF + " Result Code:" + returnResult); } }
/// <summary> /// Helper function to add new objects and binding them to closest PCFs. /// This function shows how youc an use the underlying systems to accomplish /// game object to a PCF binding /// </summary> void CreateObject() { Vector3 position = new Vector3(UnityEngine.Random.Range(-1.5f, 1.5f), UnityEngine.Random.Range(-1.5f, 1.5f), UnityEngine.Random.Range(1.5f, 5.0f)); ExampleObject newExampleObject = new ExampleObject(); newExampleObject.GO = Instantiate(_goPrefab, position, UnityEngine.Random.rotation); newExampleObject.GO.name = Guid.NewGuid().ToString(); _exampleObjects.Add(newExampleObject); var returnResult = MLPersistentCoordinateFrames.FindClosestPCF(position, (MLResult result, MLPCF pcf) => { if (result.IsOk) { Debug.LogFormat("Closest PCF found. Binding {0} to PCF {1}:", newExampleObject.GO.name, pcf.CFUID); newExampleObject.Binding = MLContentBinder.BindToPCF(newExampleObject.GO.name, newExampleObject.GO, pcf); _state = State.SaveRequired; Debug.LogFormat("object: {0} - {1} {2} {3}, {4} {5} {6} {7}", newExampleObject.GO.name, newExampleObject.GO.transform.position.x, newExampleObject.GO.transform.position.y, newExampleObject.GO.transform.position.z, newExampleObject.GO.transform.rotation.x, newExampleObject.GO.transform.rotation.y, newExampleObject.GO.transform.rotation.z, newExampleObject.GO.transform.rotation.w); } else { RemoveObject(newExampleObject); SetProgress(FAILED_TO_FIND_CLOSEST_PCF + " Reason:" + result); } }); if (!returnResult.IsOk) { RemoveObject(newExampleObject); SetProgress(FAILED_TO_FIND_CLOSEST_PCF + " Result Code:" + returnResult); } }
/// <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; }