/// <summary> /// Creates a binding to the closest PCF /// </summary> /// <returns>Must be executed as a Coroutine</returns> IEnumerator TryBindingToClosestPCF() { _done = false; MLResult returnResult = MLPersistentCoordinateFrames.FindClosestPCF(gameObject.transform.position, (pcfPositionResult, pcfWithPosition) => { if (pcfPositionResult.IsOk && pcfWithPosition != null && pcfWithPosition.CurrentResult == MLResultCode.Ok) { Debug.Log("Binding to closest found PCF: " + pcfWithPosition.CFUID); Binding = MLContentBinder.BindToPCF(UniqueId, gameObject, pcfWithPosition); MLPersistentStore.Save(Binding); NotifyChangeOfStatus(Status.BINDING_CREATED, MLResult.ResultOk); RegisterPCFEventHandlers(); _done = true; } else { Debug.LogErrorFormat("Error: MLPersistentBehavior failed to get PCF position. Reason: {0}", pcfPositionResult); NotifyChangeOfStatus(Status.BINDING_CREATE_FAILED, pcfPositionResult); _done = true; } }); if (!returnResult.IsOk) { Debug.LogErrorFormat("Error: MLPersistentBehavior failed to attempt to find closest PCF. Reason: {0}", returnResult); NotifyChangeOfStatus(Status.BINDING_CREATE_FAILED, returnResult); _done = true; } while (!_done) { yield return(null); } }
/// <summary> /// Creates a binding to the closest PCF /// </summary> /// <returns>Must be executed as a Coroutine</returns> IEnumerator TryBindingToClosestPCF() { _done = false; MLResult returnResult = MLPersistentCoordinateFrames.FindClosestPCF(gameObject.transform.position, (result, returnPCF) => { if (result.IsOk && returnPCF.CurrentResult == MLResultCode.Ok) { Debug.Log("Binding to closest found PCF: " + returnPCF.CFUID); Binding = MLContentBinder.BindToPCF(gameObject.name, gameObject, returnPCF); MLPersistentStore.Save(Binding); SetComplete(true); _done = true; } else { Debug.LogErrorFormat("Error: MLPersistentPoint failed to find closest PCF. Reason: {0}", result); SetComplete(false); _done = true; } }); if (!returnResult.IsOk) { // Technically, if we reach this point, the system had a problem Debug.LogErrorFormat("Error: MLPersistentPoint failed to attempt to find closest PCF. Reason: {0}", returnResult); SetComplete(false); _done = true; } while (!_done) { yield return(null); } }
/// <summary> /// Bind this gameObject to a pcf /// </summary> /// <param name="pcf">The pcf to bind to</param> void BindToPCF(MLPCF pcf) { UnregisterPCFEventHandlers(); DestroyBinding(); Binding = MLContentBinder.BindToPCF(UniqueId, gameObject, pcf); MLPersistentStore.Save(Binding); NotifyChangeOfStatus(Status.BINDING_CREATED, MLResult.ResultOk); RegisterPCFEventHandlers(); _pcfLost = false; }
/// <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; }