【发布时间】:2016-05-06 11:18:28
【问题描述】:
目前,我正在尝试在 android 中使用增强现实。对于这个任务,我使用Unity + Vuforia。
所以,我制作了一个正在运行的场景,当我从相机中寻找特定对象时,它会向我展示我的模型(基本上是带有动画的 3d 猫模型)。我已经按照这样的教程完成了这个: text format tutorial 和 youtube 上这样的视频:video tutroial。
在此之后,我基于 scene 制作了 android 应用程序,如下所示:
结果是Android项目,它基本上有一个Activity和一个assets和libs的banch。到目前为止我看到的与Unity 的唯一联系是UnityPlayer 类,但它只是一个ViewGroup,扩展自FrameLayout
public class UnityPlayer extends FrameLayout implements com.unity3d.player.a.a
我的目标:我需要在Unity 的视图上覆盖onClick,这是我创建的(我的3d 猫),就像你在手机上点击猫时一样,它会发出一些声音,并在点击后为其设置一些动画。我在scene 上有一个模型,从逻辑上讲,它已转换为Android 内部的View 类,我认为它只是UnityPlayer 的子对象,但代码如下:
mUnityPlayer.getChildAt(0).setOnClickListener
没有效果。
我想要一个包含所有动画和其他统一模型的属性的对象,或者如果不可能,学习如何在 Unity 本身中设置 onClick 侦听器
我意识到这个问题可能不清楚,我想为那些愿意提供帮助的人更详细地解释它。
如果您需要更多信息,请在 cmets 中索取。谢谢
编辑:正如答案所暗示的,我可以简单地为此编写一个脚本,我这样做了,使用VirtualButton,它看起来像这样:
using UnityEngine;
using System.Collections.Generic;
using Vuforia;
public class VirtualButtonEventHandler : MonoBehaviour, IVirtualButtonEventHandler {
// Private fields to store the models
private GameObject kitten;
private GameObject btn;
/// Called when the scene is loaded
void Start() {
// Search for all Children from this ImageTarget with type VirtualButtonBehaviour
VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
for (int i = 0; i < vbs.Length; ++i) {
// Register with the virtual buttons TrackableBehaviour
vbs[i].RegisterEventHandler(this);
}
// Find the models based on the names in the Hierarchy
kitten = transform.FindChild("kitten").gameObject;
btn = transform.FindChild("btn").gameObject;
kitten.SetActive(false);
btn.SetActive(true);
}
/// <summary>
/// Called when the virtual button has just been pressed:
/// </summary>
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) {
//Debug.Log(vb.VirtualButtonName);
//GUI.Label(new Rect(0, 0, 10, 5), "Hello World!");
}
/// Called when the virtual button has just been released:
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb) {
}
}
如您所见,在Start() 方法中,我想查找并隐藏模型,该模型称为kitten,但它没有隐藏
我已将此脚本附加到虚拟按钮对象,我将提供一个屏幕:
编辑:实际上我的错误,出于某种原因,我不得不将VirtualButtonBehaviorHandler 脚本附加到ImageTarget,这对我来说并不是那么容易理解,但我认为我看到了一些逻辑现在在它后面。
但是,由于某些未知的原因,如果我要添加此代码:
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) {
//Debug.Log(vb.VirtualButtonName);
switch(vb.VirtualButtonName) {
case "btn":
kitten.setActive(true);
break;
}
}
即使不触摸按钮,它也能立即工作
最终编辑:这是发生的,因为我已将我的按钮添加到数据库 .xml 中,当我从中删除按钮时 - 一切正常,我正在标记唯一的答案正确,因为它帮助了我
【问题讨论】:
标签: android unity3d augmented-reality