/// <summary>
        /// Tries to restore the given binding (It tries once and returns true if succeeded). If no binding is provided it will try to retrieve it from the current binding library.
        /// The binding may be a valid binding but fail to restore the transform if the current landscape is not recognized, in that case false will be returned.
        /// </summary>
        public bool TryRestoreOnce(MLULandscapeBinding?tryBinding = null)
        {
            MLULandscapeBinding binding = tryBinding ?? default(MLULandscapeBinding);

            // either use the given binding if its valid or try to retrieve it from the binding library
            if (binding.IsValid || (BindingLibrary.TryGetLandscapeBinding(UniqueID, out binding) && binding.IsValid))
            {
                // if a saved binding was found try to apply it on the object transform
                if (TryApplyBinding(binding))
                {
                    BindingInfo bindingInfo = CurrentBindingInfo ?? default(BindingInfo);
                    Debug.Log(LogHeader + "Binding restored succesfully! Binded to PCF: " + bindingInfo.trackedPCF.PCF.CFUID);
                    if (OnRestored != null)
                    {
                        OnRestored(this);
                    }
                    return(true);
                }
                else
                {
                    Debug.Log(LogHeader + "Restore try failed! No binded PCFs were found on current landscape.");
                }
            }
            else
            {
                Debug.Log(LogHeader + "Restore try failed! binding data is not valid.");
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// Tries to apply the given binding to the transform (parenting it with the corresponding Tracked PCF if specified and setting its local position, rotation and scale).
        /// Returns true if applied succesfully.
        /// </summary>
        private bool TryApplyBinding(MLULandscapeBinding binding)
        {
            if (!binding.IsValid)
            {
                return(false);
            }

            BindingInfo bindingInfo;

            // try to retrieve binding information (at least one current PCF must be binded to successfully restore the object binding)
            if (binding.TryGetBindingInfo(out bindingInfo))
            {
                // only apply binding if its actually different from current one
                if (bindingInfo != CurrentBindingInfo)
                {
                    Transform currentParent = transform.parent;
                    // apply binding transformation
                    transform.parent        = bindingInfo.trackedPCF.transform;
                    transform.localPosition = bindingInfo.binding.Position;
                    transform.localRotation = bindingInfo.binding.Rotation;
                    transform.localScale    = bindingInfo.binding.Scale;
                    // restore previus parent if not parenting with tracked PCF is desired
                    if (!parentToBindedPCF)
                    {
                        transform.parent = currentParent;
                    }

                    CurrentBindingInfo = bindingInfo;        // only succesfully applied bindings are saved
                    transformStatus.RegisterState();
                    isRestoring = false;                     // we are for sure not restoring any more if we succesfully apply a binding
                    if (OnBindingStateChanged != null)
                    {
                        OnBindingStateChanged(this);
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// It will start a coroutine that tries to restore the given binding or look for a saved one in the current binding library (if a custom binding is given
        /// but it's not valid it will look on the library). AutoBindAndSave functionallity will be paused during restoration process. If GameObject transform is changed or
        /// TryBind() is called the restoration coroutine will stop.
        /// </summary>
        public void TryRestore(MLULandscapeBinding?tryBinding = null)
        {
            MLULandscapeBinding binding = tryBinding ?? default(MLULandscapeBinding);

            if (restoringCoroutine != null)
            {
                StopCoroutine(restoringCoroutine);
            }
            isRestoring = false;             // just in case it was true because of the stopped coroutine

            if (binding.IsValid || BindingLibrary.TryGetLandscapeBinding(UniqueID, out binding))
            {
                restoringCoroutine = StartCoroutine(C_TryRestore(binding));
            }
            else
            {
                Debug.Log(LogHeader + "Restore coroutine did not start. Couldn't find valid binding data for this object.");
                restoringCoroutine = null;
            }
        }
Пример #4
0
        /// <summary>
        /// TryRestore coroutine that tries to restore the object with the given binding (if it's valid) until it succeeds or the object is moved.
        /// </summary>
        private IEnumerator C_TryRestore(MLULandscapeBinding binding)
        {
            bool lastRestorationFailed = true;

            if (binding.IsValid)
            {
                restoringBinding = binding;
                transformStatus.RegisterState();

                while (isRestoring = (!transformStatus.HasChanged && (lastRestorationFailed = !TryRestoreOnce(binding))))
                {
                    float time = 0.0F;

                    while (time < tryRestoreInterval)
                    {
                        yield return(null);

                        time += Time.deltaTime;
                        if (transformStatus.HasChanged)
                        {
                            break;
                        }
                    }
                }
            }

            if (lastRestorationFailed)
            {
                Debug.Log(LogHeader + "Restoration coroutine terminated. No binding data was restored!");
            }
            else
            {
                Debug.Log(LogHeader + "Restoration coroutine terminated. Binding restored succesfully!");
            }

            restoringCoroutine = null;
        }
 public BindingInfo(MLULandscapeBinding landscapeBinding, MLUPCFBinding binding, MLUTrackedPCF trackedPCF)
 {
     this.landscapeBinding = landscapeBinding;
     this.binding          = binding;
     this.trackedPCF       = trackedPCF;
 }
Пример #6
0
        /// <summary>
        /// Tries to create a new binding with the current landscape and apply it to the object. Returns true if succeded.
        /// </summary>
        private bool TryCreateAndApplyBinding()
        {
            MLULandscapeBinding binding = new MLULandscapeBinding(transform, bindingRadius);

            return(TryApplyBinding(binding));
        }