Jan*_*Jan 0 unity-game-engine augmented-reality vuforia unity3d-gui
我有一个关于如何在检测到的标记上显示简单二维图像的问题。我已经按照一些教程来显示 3d 模型,它工作正常。3d没有问题。当我想添加普通的 2d object->sprite 时,问题就开始了。当我添加简单的精灵时,我无法添加纹理,当我插入 UI 图像时,它与画布一起添加,并且在检测到目标时不显示。编辑器上的原始图像放置到此为止,很难找到它。如果有人能向我强调正确的方向,我将不胜感激。
我需要让这个图像像按钮一样触感。单击它必须显示新场景(我有它,但在 GUI.Button 下)。最好的方法是替换原始标记,但我也可以使新精灵变大以将标记隐藏在其下。
为了帮助理解答案,这里简要介绍了 Vuforia 如何处理标记检测。如果您查看附加到ImageTarget 预制件的DefaultTrackableEventHandler脚本,您会看到当跟踪系统找到或丢失图像时会触发一些事件。
这些是DefaultTrackableEventHandler.cs 中的 OnTrackingFound(第 67 行)和OnTrackingLost(第 88 行)
如果您想在跟踪时显示 Sprite,您需要做的就是放置Image Target预制件(或任何其他)并使 Sprite 成为预制件的子项。启用和禁用应该自动发生。
但是,如果您想做更多事情,这里有一些编辑过的代码。
DefaultTrackableEventHandler.cs
//Assign this in the inspector. This is the GameObject that
//has a SpriteRenderer and Collider2D component attached to it
public GameObject spriteGameObject ;
Run Code Online (Sandbox Code Playgroud)
将以下行添加到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;
Run Code Online (Sandbox Code Playgroud)
下面是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;
Run Code Online (Sandbox Code Playgroud)
接下来,关于检测此 Sprite 上的点击的问题。Unity 的 Monobehaviour 为许多鼠标事件触发事件,例如OnMouseUp、OnMouseDown等。
链接到Monobehaviour上统一的API文档,
你所需要的是称为事件OnMouseUpAsButton
创建一个名为HandleClicks.cs的新脚本并将以下代码添加到其中。将此脚本作为组件附加到您为上面分配的spriteGameObject。
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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4733 次 |
| 最近记录: |