/// <summary>
        /// 选取一个Anchor的标志物
        /// </summary>
        /// <param name="mark"></param>
        private void SelectMark(AnchorMark mark)
        {
            if (currentAnchorInfo != null)
            {
                ChangeAdjustType(SpatialAdjustType.None);
                //currentAnchorInfo.mark.SetAdjustType(AnchorAdjestType.None);
                currentAnchorInfo.mark.SetMarkSelected(false);
                StopAdjust(currentAnchorInfo);
            }

            if (mark == null)
            {
                currentAnchorInfo = null;
                return;
            }

            for (int i = 0; i < anchorObjectList.Count; i++)
            {
                AnchorObjectInfo info = anchorObjectList[i];
                if (info.anchorName == mark.anchorName)
                {
                    currentAnchorInfo = info;

                    currentAnchorInfo.mark.SetMarkSelected(true);

                    StartAdjust(currentAnchorInfo);
                    break;
                }
            }
        }
        /// <summary>
        /// 检查一个anchor是否需要存储,如果需要则存储
        /// </summary>
        /// <param name="info"></param>
        private void TrySaveAnchor(AnchorObjectInfo info)
        {
            if (!info.needSave)
            {
                return;
            }

            if (anchorStore == null)
            {
                needSaveLater = true;
                return;
            }

            string saveName = GetSaveAnchorName(info.anchorName);

            // 需要先删除一下,否则同名无法存储
            anchorStore.Delete(saveName);

            if (anchorStore.Save(saveName, info.anchor))
            {
                info.saveSucc = true;
                Debug.Log("Anchor " + saveName + " Saved!");
            }
            else
            {
                info.saveSucc = false;
                Debug.LogError("Anchor " + saveName + " save error!");
            }


            info.needSave = false;
        }
        /// <summary>
        /// 下载一个anchor
        /// 回调中两个参数
        /// bool 表示是否成功
        /// string 表示失败原因
        /// </summary>
        /// <param name="cbFinish"></param>
        public void DownloadAnchor(System.Action <bool, string> cbFinish)
        {
#if UNITY_METRO && !UNITY_EDITOR
            anchorShared.appId      = appId;
            anchorShared.roomId     = roomId;
            anchorShared.serverHost = serverHost;
            anchorShared.serverPort = serverPort;

            anchorShared.ImportRootGameObject((bool succ, string error, WorldAnchorTransferBatch batch) =>
            {
                if (!succ)
                {
                    cbFinish(succ, error);
                }
                else
                {
                    bool saveSucc = true;
                    for (int i = 0; i < anchorObjectList.Count; i++)
                    {
                        AnchorObjectInfo info = anchorObjectList[i];
                        // 先删除anchor
                        RemoveAnchor(info);

                        WorldAnchor anchor = batch.LockObject(info.anchorName, info.rootObject);
                        if (anchor == null)
                        {
                            Debug.LogWarning("Anchor [" + info.anchorName + "] can not be found in batch!");

                            saveSucc = false;

                            // 如果没读出来,则需要再次创建本地anchor
                            CreateAnchor(info);
                        }
                        else
                        {
                            Debug.Log("Anchor [" + info.anchorName + "] has been load!");
                            Debug.Log("Andhor in Object? " + info.rootObject.GetComponent <WorldAnchor>());
                            info.anchor = anchor;
                            //info.FollowRootObject();
                        }
                    }
                    // 存储
                    SaveAllSceneRootAnchor();

                    if (!saveSucc)
                    {
                        cbFinish(false, "Lock anchor failed!");
                    }
                    else
                    {
                        cbFinish(true, null);
                    }
                }
            });
#else
            // 非windows平台,直接调用成功的结果,跳过执行过程
            cbFinish(true, null);
#endif
        }
 /// <summary>
 /// 检查所有anchor是否需要存储,如果有需要存储的则存储
 /// </summary>
 private void TrySaveAllSceneRootAnchor()
 {
     for (int i = 0; i < anchorObjectList.Count; i++)
     {
         AnchorObjectInfo info = anchorObjectList[i];
         TrySaveAnchor(info);
     }
 }
 /// <summary>
 /// 强制存储所有anchor到本地
 /// </summary>
 public void SaveAllSceneRootAnchor()
 {
     for (int i = 0; i < anchorObjectList.Count; i++)
     {
         AnchorObjectInfo info = anchorObjectList[i];
         SaveAnchor(info);
     }
 }
        private void FinishAdjust()
        {
            currentAnchorInfo = null;

            // 结束操作
            if (cbAnchorControlFinish != null)
            {
                cbAnchorControlFinish();
            }
        }
        /// <summary>
        /// 清除Anchor。注意,这里仅清除WorldAnchor,并不会清除info数据,也不会从存盘中删除Anchor
        /// </summary>
        /// <param name="info"></param>
        public void RemoveAnchor(AnchorObjectInfo info)
        {
            WorldAnchor an = info.rootObject.GetComponent <WorldAnchor>();

            while (an != null)
            {
                DestroyImmediate(an);
                an = info.rootObject.GetComponent <WorldAnchor>();
            }

            info.anchor = null;
        }
 public AnchorObjectInfo GetAnchorInfo(string name)
 {
     for (int i = 0; i < anchorObjectList.Count; i++)
     {
         AnchorObjectInfo info = anchorObjectList[i];
         if (info.anchorName == name)
         {
             return(info);
         }
     }
     return(null);
 }
        private void StartAdjust(AnchorObjectInfo info)
        {
            RemoveAnchor(info);

            info.mark.StartAdjust();

            info.definition.startLock = true;

            //info.mark.followRoot = false;

            info.mark.ShowTips();
        }
Exemple #10
0
        private void FitArrow()
        {
            for (int i = 0; i < anchorObjectList.Count; i++)
            {
                AnchorObjectInfo info = anchorObjectList[i];

                // 把位置转换到Camera坐标系
                Vector3 anchorPos = info.rootTrans.position;
                Vector3 posInView = mainCameraTrans.InverseTransformPoint(anchorPos);

                if (CheckAnchorPositionInCameraView(posInView))
                {
                    info.arrowTrans.gameObject.SetActive(false);
                }
                else
                {
                    info.arrowTrans.gameObject.SetActive(true);
                    info.arrowTrans.LookAt(anchorPos);

                    float x, y;
                    if (posInView.x != 0)
                    {
                        float tan = Mathf.Abs(posInView.y) / Mathf.Abs(posInView.x);

                        if (Mathf.Abs(tan) > (tanView))
                        {
                            y = Mathf.Sign(posInView.y) * fovVerticalWidthHalf;
                            x = Mathf.Sign(posInView.x) * fovVerticalWidthHalf / tan;
                        }
                        else
                        {
                            y = Mathf.Sign(posInView.y) * fovHorizonWidthHalf * tan;
                            x = Mathf.Sign(posInView.x) * fovHorizonWidthHalf;
                        }
                    }
                    else
                    {
                        x = 0;
                        if (posInView.y > 0)
                        {
                            y = fovVerticalTan;
                        }
                        else
                        {
                            y = -fovVerticalTan;
                        }
                    }

                    info.arrowTrans.position = mainCameraTrans.TransformPoint(new Vector3(x, y, arrowDistance + 0.2f));
                }
            }
        }
Exemple #11
0
        private void StartAdjust(AnchorObjectInfo info)
        {
            RemoveAnchor(info);

            info.mark.StartAdjust();

            info.definition.startLock = true;

            //info.mark.followRoot = false;
#if !ME_LIVE_ACTIVE
            info.mark.ShowTips();
#endif
            VuforiaManager.Instance.TurnOn();
        }
Exemple #12
0
        ////////////////////////////////////////////
        #region 创建、删除、本地存储Anchor相关内容
        ////////////////////////////////////////////

        /// <summary>
        /// 将指定的物体设置为Anchor
        /// </summary>
        /// <param name="name"></param>
        /// <param name="obj"></param>
        public AnchorObjectInfo AddAnchorObject(string name, GameObject obj)
        {
            AnchorObjectInfo info = new AnchorObjectInfo();

            info.anchorName = name;
            info.rootTrans  = obj.transform;

            AnchorDefinition define = obj.GetComponent <AnchorDefinition>();

            if (define == null)
            {
                define            = obj.AddComponent <AnchorDefinition>();
                define.anchorName = name;
            }
            info.definition = define;

            // 创建mark物体
            GameObject markObj = PrefabUtils.CreateGameObjectToParent(anchorMarkRoot, markPrefab);
            AnchorMark mark    = markObj.GetComponent <AnchorMark>();

            mark.Init(name, info);
            mark.rootObjectTransform = obj.transform;

            // 创建指引箭头
            GameObject arrowObj = PrefabUtils.CreateGameObjectToParent(gameObject, arrowPrefab);

            info.arrowTrans = arrowObj.transform;
            arrowObj.SetActive(false);

            // 设置位置
            Vector3 originPos = obj.transform.position;

            markObj.transform.position = originPos;

            info.mark = mark;

            AddAnchor(info);

            //info.mark.followRoot = true;
            //info.FollowRootObject();

            //mark.gameObject.SetActive(false);

            anchorObjectList.Add(info);

            Debug.Log("Add anchor [" + name + "] to obj [" + obj.name + "]");

            return(info);
        }
        /// <summary>
        /// 为一个已有的对象,添加anchor组件,并不做存储和读取的动作
        /// </summary>
        /// <param name="info"></param>
        public void CreateAnchor(AnchorObjectInfo info)
        {
            if (info.anchor != null)
            {
                return;
            }

            Debug.Log("Create Anchor for [" + info.anchorName + "]");

            WorldAnchor anchor = info.rootObject.AddComponent <WorldAnchor>();

            info.anchor = anchor;

            info.status = AnchorObjectStatus.Saved;
        }
        private void StopAdjust(AnchorObjectInfo info)
        {
            // 这里只能Create,不能Add,因为Add会先去store里加载一次
            CreateAnchor(info);

            // 存储一下
            SaveAnchor(info);

            info.definition.startLock = false;

            ChangeAdjustType(SpatialAdjustType.None);
            info.mark.HideTips();

            //info.mark.followRoot = true;
        }
 public void ShowAllMark(bool b)
 {
     for (int i = 0; i < anchorObjectList.Count; i++)
     {
         AnchorObjectInfo info = anchorObjectList[i];
         if (b)
         {
             info.mark.ShowTips();
         }
         else
         {
             info.mark.HideTips();
         }
     }
 }
        /// <summary>
        /// 放弃所有Anchor信息,会删除所有绑定物体上的WorldAnchor组件,并清空info信息
        /// 但是不会删除存盘信息
        /// </summary>
        public void ClearAllAnchorInfo(bool removeRootObject = false)
        {
            for (int i = 0; i < anchorObjectList.Count; i++)
            {
                AnchorObjectInfo info = anchorObjectList[i];
                RemoveAnchor(info);

                info.Clear();

                if (removeRootObject)
                {
                    GameObject.DestroyImmediate(info.rootObject);
                }
            }
            anchorObjectList.Clear();
        }
        /// <summary>
        /// 强制存储一个Anchor
        /// </summary>
        /// <param name="info"></param>
        private void SaveAnchor(AnchorObjectInfo info)
        {
            if (info.anchor == null)
            {
                return;
            }

            Debug.Log("Save anchor [" + info.anchorName + "] ....");
            info.needSave = true;
            if (info.anchor.isLocated)
            {
                TrySaveAnchor(info);
            }
            else
            {
                info.anchor.OnTrackingChanged += Anchor_OnTrackingChanged;
            }
        }
        /// <summary>
        /// 为一个物体添加anchor信息并存储。这里会先尝试读取Anchor,如果读取不到则创建并存储
        /// </summary>
        /// <param name="info"></param>
        private void AddAnchor(AnchorObjectInfo info)
        {
            //Debug.Log("Add Anchor [" + info.anchorName + "]");

            //Debug.Log("  ---anchor=" + info.anchor + " store=" + anchorStore);
            if (anchorStore == null)
            {
                info.status   = AnchorObjectStatus.StoreNotReady;
                needSaveLater = true;
                anchorToAdd.Add(info);
                return;
            }
            if (info.anchor == null)
            {
                string      saveName    = GetSaveAnchorName(info.anchorName);
                WorldAnchor savedAnchor = anchorStore.Load(saveName, info.rootObject);

                if (savedAnchor != null)
                {
                    Debug.Log("Load anchor [" + saveName + "] success!");
                    info.anchor = savedAnchor;
                    //info.mark.followRoot = true;
                    //info.FollowRootObject();

                    info.status = AnchorObjectStatus.Saved;
                }
                else
                {
                    Debug.Log("Can not load anchor [" + info.anchorName + "]");
                    info.status = AnchorObjectStatus.HasNotSet;
                }

                /**
                 * 这里先不要添加anchor组件
                 * if (info.anchor == null)
                 * {
                 *  CreateAnchor(info);
                 *  SaveAnchor(info);
                 * }
                 */
            }
        }
Exemple #19
0
        public void Init(string name, AnchorObjectInfo info)
        {
            anchorName = name;
            this.info  = info;

            anchorNameText.text = anchorName;

            controller = SceneAnchorController.Instance;
            buttonMove.Init(this);
            buttonRotate.Init(this);
            buttonFree.Init(this);

            buttonMove.SetActive(false);
            buttonRotate.SetActive(false);
            buttonFree.SetActive(false);

            // 创建外框
            GameObject obj = GameObject.Instantiate <GameObject>(this.markCubePrefab);

            obj.transform.SetParent(transform);
            obj.transform.localPosition = new Vector3(
                info.definition.xMin + (info.definition.xMax - info.definition.xMin) / 2,
                info.definition.yMin + (info.definition.yMax - info.definition.yMin) / 2,
                info.definition.zMin + (info.definition.zMax - info.definition.zMin) / 2
                );
            obj.transform.localRotation = Quaternion.identity;
            obj.transform.localScale    = new Vector3(
                info.definition.xMax - info.definition.xMin,
                info.definition.yMax - info.definition.yMin,
                info.definition.zMax - info.definition.zMin
                );

            obj.layer = controller.AnchorMarkLayer;
            border    = obj;

            render = border.GetComponent <Renderer>();

            SetMarkSelected(false);
        }
Exemple #20
0
        /// <summary>
        /// 导出anchor资料并上传
        /// 注意,上传期间应当阻止应用活动,以避免上传完成之前再次调用
        /// </summary>
        /// <param name="anchors"></param>
        /// <param name="cbFinish"></param>
        public void ExportGameRootAnchor(List <AnchorObjectInfo> anchors, System.Action <bool, string> cbFinish)
        {
            cbExportFinish    = cbFinish;
            uploadFinish      = false;
            uploadErrorString = null;
            uploadApi         = null;

            WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();

            for (int i = 0; i < anchors.Count; i++)
            {
                AnchorObjectInfo info = anchors[i];
                if (info.anchor != null)
                {
                    bool flag = transferBatch.AddWorldAnchor(info.anchorName, info.anchor);
                    Debug.Log(flag);
                }
            }

            Buffer = new byte[0];
            WorldAnchorTransferBatch.ExportAsync(transferBatch, OnExportDataAvailable, OnExportComplete);
        }