高通AR-关于切换相机跟踪图片的控制
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">        当跟踪到图片,并且显示模型的时候,调用SetTrackerSwitch(false)函数,模型会保持显示,移开图片,不会对模型产生影响.此时如果调用SetTrackerSwitch(true),就会回到正常状态。</span>

    

using UnityEngine;
using System.Collections;
using Vuforia;
public class MyTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{

    #region PRIVATE_MEMBER_VARIABLES

    private TrackableBehaviour mTrackableBehaviour;
    bool _trackerSwitch = true; //作为可否跟踪的开关.

    //private ImageTargetAbstractBehaviour mImageTarget;
    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNTIY_MONOBEHAVIOUR_METHODS



    void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
        //mImageTarget = GetComponent<ImageTargetAbstractBehaviour>();
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS



    #region PUBLIC_METHODS
    /// <summary>
    /// 设置跟踪开关的状态
    /// </summary>
    /// <param name="sitchValue"></param>
    public void SetTrackerSwitch(bool sitchValue)
    {
        _trackerSwitch = sitchValue;
        Fix();
    }

    /// <summary>
    /// 改变跟踪开关为相反值
    /// </summary>
    public void ChangeTrackerSwitch()
    {
        _trackerSwitch = !_trackerSwitch;
        Fix();
    }

    /// <summary>
    /// 跟踪开关改变后进行的调整.
    /// </summary>
    void Fix()
    {
        if(_trackerSwitch == true)
        {
            mTrackableBehaviour.enabled = true;
            OnTrackingLost();
        }
        else
        {
            mTrackableBehaviour.enabled = false; 
        }
    }
    /// <summary>
    /// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS


    private void OnTrackingFound()
    {
        if (_trackerSwitch == false) return;
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

        // Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }

        // Enable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = true;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    }


    private void OnTrackingLost()
    {
        if (_trackerSwitch == false) return;

        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

        // Disable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = false;
        }

        // Disable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = false;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    }

    #endregion // PRIVATE_METHODS
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dgtgtjs2/article/details/49947799
文章标签: Unity 高通ar
个人分类: Unity3d
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

高通AR-关于切换相机跟踪图片的控制

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭