Vuforia和Unity3D中的用户定义目标

时间:2017-09-15 15:48:37

标签: c# unity3d vuforia

我正在寻找有关如何在Vuforia中使用UserDefinedTargets的教程。我发现的所有人都展示了Vuforia样品,但样品目前仅在Vuforia 2017.2 beta中提供。我也找不到档案。

为了自己解决问题,我将此代码附加到UserDefinedTargetHandler

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class UDTHandler : MonoBehaviour, IUserDefinedTargetEventHandler
{

    private UserDefinedTargetBuildingAbstractBehaviour mTargetBuildingBehaviour;
    private DataSet mBuildDataSet;
    private bool mUdtInitialized = false;
    private ImageTargetBuilder.FrameQuality mFrameQuality = ImageTargetBuilder.FrameQuality.FRAME_QUALITY_NONE;
    private ImageTracker mImageTracker;

    public ImageTargetBehaviour ImageTargetTemplate;
    // Use this for initialization
    void Start()
    {
        mTargetBuildingBehaviour = GetComponent<UserDefinedTargetBuildingBehaviour>();
        if (mTargetBuildingBehaviour)
        {
            mTargetBuildingBehaviour.RegisterEventHandler(this);
        }
    }

    void OnGUI()
    {
        if (!mUdtInitialized)
            return;
        if (mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_HIGH ||
            mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_MEDIUM)
        {

            if (GUI.Button(new Rect(200, Screen.height - 100, 200, 90), "Build New Target"))
            {

                BuildNewTarget();
            }
        }
    }

    void BuildNewTarget()
    {
        string newTargetName = "myTargetName";
        mTargetBuildingBehaviour.BuildNewTarget(newTargetName, ImageTargetTemplate.GetSize().x);
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnFrameQualityChanged(ImageTargetBuilder.FrameQuality frameQuality)
    {
        mFrameQuality = frameQuality;
    }

    public void OnInitialized()
    {

        mImageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
        if (mImageTracker != null)
        {

            mBuildDataSet = mImageTracker.CreateDataSet();
            mImageTracker.ActivateDataSet(mBuildDataSet);

            mUdtInitialized = true;
        }
    }

    public void OnNewTrackableSource(TrackableSource trackableSource)
    {
        mImageTracker.DeactivateDataSet(mBuildDataSet);

        if (mBuildDataSet.HasReachedTrackableLimit())
        {
            IEnumerable<Trackable> trackables = mBuildDataSet.GetTrackables();
            Trackable oldest = null;

            foreach (Trackable t in trackables)
            {
                if (oldest == null || t.ID < oldest.ID)
                    oldest = t;
            }

            if (oldest != null)
            {
                mBuildDataSet.Destroy(oldest, true);
            }
        }

        ImageTargetBehaviour imageTargetCopy = (ImageTargetBehaviour)Instantiate(ImageTargetTemplate);
        mBuildDataSet.CreateTrackable(trackableSource, imageTargetCopy.gameObject);
    }
}

它遗失了很多。我真的需要一些帮助。我通过youtube,vuforia论坛,google等查看了但是找不到合适的指南

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。简单的解决方案是将您的统一升级到2017.2 beta版。您可以在Unity网站的Beta Program页面下载。它更容易使用,因为他们已经添加了vuforia作为统一外围设备,其中包含您需要的每个工具。此外,转到资产商店并下载vuforia核心样本(免费)以获取预定义模板。