/// <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; }