检测到目标时,带有vuforia的Unity3d显示2d图像 [英] Unity3d with vuforia showing 2d image when targed is detected

查看:103
本文介绍了检测到目标时,带有vuforia的Unity3d显示2d图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何在检测到的标记上方显示简单2d图像的方式有疑问.我遵循了一些教程来展示3D模型,并且效果很好. 3d没有问题. 当我想添加普通的2d object-> sprite时,问题就开始了.当我添加简单的精灵时,我无法添加纹理,当我插入UI图像时,它会与画布一起添加,并且当target为 检测到.然后将原始图像放置在编辑器上,以至于很难找到它. 如果有人可以向我强调正确的方向,我将不胜感激.

I have a question about the way how to show simple 2d image on top of detected marker. I have followed some tutorial to show 3d model and it works fine. there is no problem with 3d. The problem starts when I want to add normal 2d object->sprite . When I add simple sprite I can't add texture and when I insert UI image it's added together with canvas and it is not showed when target is detected. The original image on editor is placed then so far that it's difficult to find it. I would be grateful if somebody can highlight me the right direction.

我需要使该图像像按钮一样敏感.单击它必须显示新的场景(我有它,但是在GUI.Button下).最好的方法是替换原始标记,但我也可以将新的精灵变大以将标记隐藏在其下.

I need to make this image touch sensitive like a button. Clicking into it must show new scene ( I have it but under GUI.Button). The best way is to replace original marker but I can also make new sprite bigger to hide marker under it.

推荐答案

为帮助您理解答案,以下简要介绍了Vuforia如何处理标记检测.如果查看连接到 ImageTarget预制 DefaultTrackableEventHandler 脚本,您会发现当跟踪系统发现或丢失某个事件时会触发事件图片.

To help understand the answer, here's a quick rundown on how Vuforia handles marker detection. If you take a look at the DefaultTrackableEventHandler script that's attached to the ImageTarget prefab, you'll see that there are events that fire when the when the tracking system finds or loses an Image.

这些是 OnTrackingFound (第67行)& OnTrackingLost (第88行)在DefaultTrackableEventHandler.cs中

These are OnTrackingFound (line 67) & OnTrackingLost (line 88) in DefaultTrackableEventHandler.cs

如果要在跟踪时显示Sprite,只需将 Image Target 预制件(或任何其他预制件)放入Sprite,然后将Sprite设置为预制件的子代.启用和禁用应该自动进行.

If you want to show a Sprite when tracking, all you need to do is put the Image Target prefab (or any other) and make the Sprite a child of the prefab. The enabling and disabling should happen automatically.

但是,如果您想做更多事情,这里有一些经过编辑的代码.

However, in case you want to do something more, here's some edited code.

DefaultTrackableEventHandler.cs

//Assign this in the inspector. This is the GameObject that 
//has a SpriteRenderer and Collider2D component attached to it
public GameObject spriteGameObject ;

将以下几行添加到 OnTrackingFound

    //Enable both the Sprite Renderer, and the Collider for the sprite
    //upon Tracking Found. Note that you can change the type of 
    //collider to be more specific (such as BoxCollider2D)
    spriteGameObject.GetComponent<SpriteRenderer>().enabled = true;
    spriteGameObject.GetComponent<Collider2D>().enabled = true;

    //EDIT 1
    //Have the sprite inherit the position and rotation of the Image
    spriteGameObject.transform.position = transform.position;
    spriteGameObject.transform.rotation = transform.rotation;

下面是 OnTrackingLost

    //Disable both the Sprite Renderer, and the Collider for the sprite
    //upon Tracking Lost. 
    spriteGameObject.GetComponent<SpriteRenderer>().enabled = false;
    spriteGameObject.GetComponent<Collider2D>().enabled = false;



接下来,关于检测该Sprite的点击的问题. Unity的Monobehaviour会触发许多鼠标事件的事件,例如 OnMouseUp OnMouseDown 等.



Next, your question about detecting clicks on this Sprite. Unity's Monobehaviour fires events for a lot of mouse events, such as OnMouseUp, OnMouseDown etc.

在Unity的API文档上链接到Monobehaviour
您将需要一个名为 OnMouseUpAsButton

Link to Monobehaviour on Unity's API docs
What you will need is an event called OnMouseUpAsButton

创建一个名为 HandleClicks.cs 的新脚本,并将以下代码添加到其中.将此脚本作为组件附加到您为此指定的 spriteGameObject .

Create a new script called HandleClicks.cs and add the below code to it. Attach this script as a component to the spriteGameObject that you assigned for the above.

public class HandleClicks : MonoBehaviour {

    //Event fired when a collider receives a mouse down
    //and mouse up event, like the interaction with a button
    void OnMouseUpAsButton () {
        //Do whatever you want to
        Application.LoadLevel("myOtherLevel");
    }

}

这篇关于检测到目标时,带有vuforia的Unity3d显示2d图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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