美文网首页Unity (AR)
Unity(ar) 多图脱卡功能 -- Vuforia

Unity(ar) 多图脱卡功能 -- Vuforia

作者: U3D界金城武 | 来源:发表于2017-04-25 17:06 被阅读207次
    using UnityEngine;
    using Vuforia;
    
    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class MyTrackable : MonoBehaviour,
                                                ITrackableEventHandler
    {
        private TrackableBehaviour mTrackableBehaviour;
    
    
        private bool isFirstfound = false;
        private void Awake()
        {
    
        }
        void Start()
        {
            Manager.Instance.allImageTarget.Add(this.transform);
            HiderThisModel();
            mTrackableBehaviour = GetComponent<TrackableBehaviour>();
            if (mTrackableBehaviour)
            {
                mTrackableBehaviour.RegisterTrackableEventHandler(this);
            }
        }
    
        public void OnTrackableStateChanged(
                                        TrackableBehaviour.Status previousStatus,
                                        TrackableBehaviour.Status newStatus)
        {
            if (newStatus == TrackableBehaviour.Status.DETECTED ||
                newStatus == TrackableBehaviour.Status.TRACKED ||
                newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
            {
                isFirstfound = true;
                Manager.Instance.lost = false;
                if (Manager.Instance.lastTranckName == mTrackableBehaviour.TrackableName)
                {
                    return;
                }
                else
                {
                    Manager.Instance.HideallModel();
                    ShowThisModel();
                }
            }
            else
            {
                if (isFirstfound)
                {
    
                    Manager.Instance.lastTranckName = mTrackableBehaviour.TrackableName;
                    Manager.Instance.lost = true;
                }
    
            }
        }
    
        /// <summary>
        /// 显示当前模型
        /// </summary>
        public void ShowThisModel()
        {
            foreach (Transform child in this.transform)
            {
                child.gameObject.SetActive(true);
            }
        }
        /// <summary>
        /// 隐藏当前模型
        /// </summary>
        public void HiderThisModel()
        {
            foreach (Transform child in this.transform)
            {
                child.gameObject.SetActive(false);
            }
        }
    }
    
    • 2,Manager管理类
    using System.Collections.Generic;
    using UnityEngine;
    using DG.Tweening;
    
    public class Manager : MonoBehaviour {
        public static Manager Instance;
        public List<Transform> allImageTarget = new List<Transform>();
        public string lastTranckName = "";
        public bool lost = false;
        private Transform ARCamera;
        private Vector3 lostCamPos = new Vector3(0, 4.9f, -3.5f);
        public Transform arcam;
    
        private void Awake()
        {
            ARCamera = GameObject.Find("ARCamera").transform;
            Instance = this;
        }
    
        void Update () {
            if (lost)
            {
                //ARCamera.localPosition = lostCamPos;
                ARCamera.DOLocalMoveX(lostCamPos.x, 0.2f);
                ARCamera.DOLocalMoveY(lostCamPos.y,0.2f);
                ARCamera.DOLocalMoveZ(lostCamPos.z, 0.2f);
                ARCamera.DOLocalRotate(new Vector3(55, 0, 0), 0.2f);
               // ARCamera.localRotation = Quaternion.Euler(55, 0, 0);
                //ARCamera.localRotation = Quaternion.identity;
    
            }
        }
        public void HideallModel()
        {
            foreach (Transform imageTarget in allImageTarget)
            {
                imageTarget.GetComponent<MyTrackable>().HiderThisModel();
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题: Unity(ar) 多图脱卡功能 -- Vuforia

        本文链接:https://www.haomeiwen.com/subject/pngvzttx.html