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

  • 1,传送门https://v.qq.com/x/page/u0378anvm7a.html
  • 2,具体代码 MyTrackable
using UnityEngine;
using Vuforia;

/// 
/// A custom handler that implements the ITrackableEventHandler interface.
/// 
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();
        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;
            }

        }
    }

    /// 
    /// 显示当前模型
    /// 
    public void ShowThisModel()
    {
        foreach (Transform child in this.transform)
        {
            child.gameObject.SetActive(true);
        }
    }
    /// 
    /// 隐藏当前模型
    /// 
    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 allImageTarget = new List();
    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().HiderThisModel();
        }
    }
}

你可能感兴趣的:(Unity(ar) 多图脱卡功能 -- Vuforia)