使用unity和vuforia的增强现实应用程序 [英] Augmented reality application using unity and vuforia

查看:107
本文介绍了使用unity和vuforia的增强现实应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我目前正在使用unity和vuforia sdk创建我的第一个AR应用程序。我已经知道AR的一些基础知识,您需要一个图像目标才能显示3d对象或与该图像目标相关联的信息。我有这个AR旋转立方体

Good day everyone, I am currently on the process of creating my first AR application using unity and vuforia sdk. I already know some basics of AR that you need an image target to display the 3d object or information associated with the image target. I have this AR spinning cube

此示例应用程序在找到有效的图像目标并且3d多维数据集将显示并开始旋转时起作用。我的问题是,当手机扫描图像目标时,应该显示一条消息正在扫描图像目标,当它被发现时将显示找到图像目标,并且它将显示一个弹出按钮显示,当按下多维数据集时将出现该按钮。

this sample app works when it found a valid image target and a 3d cube will display and start to spin. My question is while the phone scans for image target there should be a message saying "Scanning for image target" and when found it displays "Image target found" and it will display a popup button "Show" that when pressed the cube will appear.

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

public class RotateScript : MonoBehaviour {

public int speed;

//Update is called once per frame
void Update () {
    transform.Rotate (new Vector3 (0, Time.deltaTime * speed, 0));

}

}

更新:

我找到了此脚本并对其进行了一些修改,以在屏幕上方显示UI文本,我打算将文本更改为丢失的图像目标如果未检测到图像目标,则为;如果存在图像目标,则为找到图像目标。我将脚本放在图像目标预制件上,但显示错误 UI文本错误。关于我做错了什么的任何建议。谢谢

I found this script and modified it a bit to display a UI text above the screen, I intend to change the text into "Image Target Lost" when no image target detected and "Image Target Found" when the image target is present. I put the script on image target prefab but it displays an error UI Text Error. Any suggestions on where I did wrong. Thanks

示例代码

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Vuforia;

public class TextScript : MonoBehaviour, ITrackableEventHandler {

private TrackableBehaviour mTrackableBehaviour;
//Declares a UI text
Text uiText;

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

    //uiText = gameObject.GetComponent<Text> ();
}

public void OnTrackableStateChanged(
    TrackableBehaviour.Status previousStatus,
    TrackableBehaviour.Status newStatus)
{
    if (newStatus == TrackableBehaviour.Status.DETECTED ||
        newStatus == TrackableBehaviour.Status.TRACKED)
    {

        uiText.text = "Image Target Found";
    }
    else
    {

        uiText.text = "Image Target Lost";
    }
}
}

更新(10 / 30/2017)

很抱歉,如果我晚更新,我已经尝试了所有您提供的解决方案,但仍然没有工作。该代码的名称为TextScript,我已将其附加到图像目标,但仍未成功。这是示例代码。

I'm sorry if I update late, I've tried all the solution you are all giving but it still don't work. The name of the code is TextScript and I've attached it to image target but still the result is unsuccessful. Here is the sample code.

TextScript

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Vuforia;

public class TextScript : MonoBehaviour, ITrackableEventHandler {

private TrackableBehaviour mTrackableBehaviour;
//Declares a UI text
public Text uiText;

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

    uiText = gameObject.GetComponent<Text> ();
}

public void OnTrackableStateChanged(
    TrackableBehaviour.Status previousStatus,
    TrackableBehaviour.Status newStatus)
{
    if (newStatus == TrackableBehaviour.Status.DETECTED ||
        newStatus == TrackableBehaviour.Status.TRACKED ||
        newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    {
        OnTrackingFound();

        uiText.text = "Image Target Found";
    }
    else
    {
        OnTrackingLost();

        //uiText.text = "Image Target Lost";
    }
}

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

    // Enable rendering:
    foreach (Renderer component in rendererComponents)
    {
        uiText.text = "Image Target Found";
        component.enabled = true;
    }

    // Enable colliders:
    foreach (Collider component in colliderComponents)
    {
        uiText.text = "Image Target Found";
        component.enabled = true;
    }

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


private void OnTrackingLost()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

    // Disable rendering:
    foreach (Renderer component in rendererComponents)
    {
        uiText.text = "Image Target Lost";
        component.enabled = false;
    }

    // Disable colliders:
    foreach (Collider component in colliderComponents)
    {
        uiText.text = "Image Target Lost";
        component.enabled = false;
    }

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

PS

我已经将文本UI命名为Scanning,因此当我按播放时,文本已经显示了该单词。但是,当找到图片目标时,文字不会改变。拜托,我非常需要您的帮助,如果有不需要的代码,请随时对其进行编辑或评论,以便我将其删除。谢谢

I already name the text UI as Scanning so when I press play the text already display the said word. But when it found the image target the text doesn't change. Please guys I badly needed you help, if there are unwanted code please feel free to edit it or comment so that I can delete it. Thanks

推荐答案

在脚本中,您只是声明了uiText,但没有对其进行初始化。

In your script you just declared the uiText but not initialised it. It should be like this

public Text uiText;

,然后将相关的Text游戏对象从项目窗口拖到检查器中的TextScript脚本中或在其中初始化onStart()就像

and then drag your relevant Text gameobject from project window to the TextScript script in the inspector or initialise it in onStart() like

uiText = GameObject.Find("<your_text_object_name>").GetComponent<Text>();

不建议使用第二个选项,因为文本初始化次数等于数据库。

The second option is not recommended as the Text initialises number of times equal to the image targets count in the database.

希望它会有所帮助。

这篇关于使用unity和vuforia的增强现实应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆