使用unity和vuforia的增强现实应用程序

时间:2017-10-11 04:04:11

标签: android unity3d unityscript vuforia android-augmented-reality

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

enter image description here

此示例应用程序在找到有效的图像目标时会起作用,并且会显示一个3d立方体并开始旋转。我的问题是,当手机扫描图像目标时,应该有一条消息说"扫描图像目标"当找到它时显示"找到图像目标"它会显示一个弹出按钮" Show"当按下时,立方体就会出现。

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文本,我打算将文本更改为" Image Target Lost"当没有检测到图像目标时,"图像目标找到"当图像目标存在时。我将脚本放在图像目标预制件上,但它显示错误UI Text Error。关于我做错了什么的任何建议。感谢

示例代码

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,我已将其附加到图像目标,但结果仍然不成功。这是示例代码。

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");
}
}

P.S

我已经将文本UI命名为Scanning,所以当我按下播放时,文本已经显示了所说的单词。但是当它找到图像目标时,文本不会改变。请大家,我非常需要你的帮助,如果有不需要的代码,请随时编辑或评论,以便我可以删除它。谢谢

3 个答案:

答案 0 :(得分:0)

也许您应该在 vuforia / scripts / defaulttrackable.cs

中查看此方法 OnTrackingFound()
       // Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }

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

您可以使用布尔值来管理何时渲染多维数据集。

答案 1 :(得分:0)

在给定链接中查看 TrackableBehaviour 的属性: https://library.vuforia.com/content/vuforia-library/en/reference/unity/classVuforia_1_1TrackableBehaviour.html

当图像目标检测到时将TEXT设置为“正在扫描”, On OnTrackingFound()方法将TEXT设置为“Found”并将Set Button Object设置为true,Onbuttonclik事件实例化Cube对象

On OntrackingLost()方法设置Button对象可见false

希望这有帮助。

答案 2 :(得分:0)

在你的脚本中你刚刚声明了uiText但没有初始化它。它应该是这样的

public Text uiText;

然后将相关的文本游戏对象从项目窗口拖动到检查器中的TextScript脚本,或者在onStart()中初始化它,如

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

不建议使用第二个选项,因为Text初始化的次数等于数据库中图像目标的数量。

希望它有所帮助。

相关问题