从 ImageTarget 获取模型对象 Android

Get model object from ImageTarget Android

目前,我正尝试在 android 中使用增强现实技术。对于这个任务,我使用 Unity + Vuforia.

因此,我制作了一个场景,该场景正在运行,当我从我的相机中寻找特定对象时,它会显示我的模型(基本上是带动画的 3d 猫模型)。我已经根据这样的教程完成了此操作: text format tutorial and videos on youtube like this : video tutroial.

在此之后,我基于此 scene 制作了 android 应用程序,如下所示:

结果是Android项目,基本上有一个Activityassetslibs一批。到目前为止,我看到的与 Unity 的唯一联系是 UnityPlayer class,但它只是一个 ViewGroup,从 FrameLayout

扩展而来
public class UnityPlayer extends FrameLayout implements com.unity3d.player.a.a 

我的目标: 我需要在 Unity 的视图上覆盖 onClick,这是我创建的(我的 3d 猫),类似当您在 phone 上点击猫时,它会发出一些声音,并在点击后为其设置一些动画。我在 scene 上有一个模型,逻辑上它已在 Android 内转换为 View class,我认为它只是 [=22= 的子级],但代码是这样的:

mUnityPlayer.getChildAt(0).setOnClickListener

没有效果。

我想要一些对象来包含 unity 中的模型所具有的所有动画和其他属性,或者如果不可能,学习如何在 Unity 本身中设置 onClick 侦听器

我意识到这个问题可能不清楚,我想详细解释一下,以帮助那些试图提供帮助的人。

如果您需要更多信息,请在评论中询问。谢谢

编辑: 正如答案所暗示的那样,我可以简单地为此编写一个脚本,我确实这样做了,用于使用 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 中添加了我的按钮,当我从中删除按钮时 - 一切正常,我'将唯一一个答案标记为正确,因为它对我有帮助

兄弟,只要我们去做,一切皆有可能。根据我的理解,你想要做的是:

首先:您需要通过阅读博客和教程来清除一些基本概念:

  1. 正如您提到的对象,您的白色细胞 :) 猫渲染是 "Marker"

  2. 在 Unity 中,一切都是游戏对象,您可以编写脚本来使用脚本来操作该游戏对象 (CAT)。它将在 C#(Mono) 或 JavaScript 中用于此工作,您可以使用 Visual Studio 或 Unity 的 MonoDevelop 但在此之前,请在 google

    上搜索关键字

    a) Touchevent, RayCastMenu Controle 统一:处理 Touch

    b) MonoBehaviour class, Start(), Update(), Unity中的OnGUI()方法

  3. 您可以使用其 名称或标签 来识别任何游戏对象,您可以在 Inspector window[ 中查看或更改它们=121=]

这些是一些基本的东西。请关注 vuforia 开发者门户以了解更多信息: https://developer.vuforia.com/library/


现在:回答您的问题: 根据我的说法,您想在 Click of your sweet cat 上做一些事情。 很简单,如果你只是想在点击 cat 时启动 android activity 那么有两种可能的方法:

  1. 创建 android 项目并将其作为库项目统一导入 Unity。

  2. 借助 C# 脚本从 unity 项目创建 android activity。将此脚本附加到场景中的任何游戏对象。

这里我提供第二个例子:点击按钮它会启动AndroidActivity。 你要做的是:

通过 CAT GameObjectExport 项目将 Button GameObject 替换为 Android 并使用与 C# 代码中提到的相同的名称和包编写 activity 以执行任何您想做的事情

这里在我的例子中我已经解释过了:

  1. 使用Unity+ Vuforia检测到Marker时如何弹出GUI

  2. 如何在特定事件上从 Unity 代码启动 androidActivity

  3. 如何在Unity中处理事件

  4. 如何在多个分辨率下保持 GUI 相同


请仔细研究代码并阅读评论:)

using UnityEngine;
using System.Collections;
using Vuforia; //import Vuforia 
using System;

public class ButtonPopup : MonoBehaviour, ITrackableEventHandler 

{

    float native_width= 1920f;// Native Resolution to maintain resolution on different screen size
    float native_height= 1080f;
    public Texture btntexture;// drag and drop any texture in inspector window

    private TrackableBehaviour mTrackableBehaviour;

    private bool mShowGUIButton = false;


    void Start () {


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

    public void OnTrackableStateChanged(
        TrackableBehaviour.Status previousStatus,
        TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            mShowGUIButton = true;// Button Shown only when marker detected same as your cat
        }

        else
        {
            mShowGUIButton = false;
        }
    }

    void OnGUI() {

        //set up scaling
        float rx = Screen.width / native_width;
        float ry = Screen.height / native_height;

        GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 (rx, ry, 1));

        Rect mButtonRect = new Rect(1920-215,5,210,110);


        if (!btntexture) // This is the button that triggers AR and UI camera On/Off
        {
            Debug.LogError("Please assign a texture on the inspector");
            return;
        }

        if (mShowGUIButton) {

            // different screen position for your reference
            //GUI.Box (new Rect (0,0,100,50), "Top-left");
            //GUI.Box (new Rect (1920 - 100,0,100,50), "Top-right");
            //GUI.Box (new Rect (0,1080- 50,100,50), "Bottom-left");
            //GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Bottom right");

            // draw the GUI button
            if (GUI.Button(mButtonRect, btntexture)) {
                // do something on button click 
                OpenVideoActivity();
            }
        }
    }

    public void OpenVideoActivity()
    {
        var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer”);// any package name maintain same in android studio
        var jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
        // Accessing the class to call a static method on it
        var jc = new AndroidJavaClass("com.mobiliya.gepoc.StartVideoActivity”);//Name of android activity
        // Calling a Call method to which the current activity is passed
        jc.CallStatic("Call", jo);
    }

}

记住:在 Unity 中一切都是游戏对象,您可以编写脚本来操作任何游戏对象

编辑:虚拟按钮的信息

虚拟按钮检测目标图像的基本特征何时从相机视图中被遮挡。您需要将按钮放置在图像的一个功能丰富的区域上,以便它可靠地触发其 OnButtonPressed 事件。要确定图像中这些特征的位置,请在目标管理器中为图像使用显示特征 link。

选择图像中尺寸约为图像目标大小 10% 的区域。


这是我为您简化的图像示例:



注册虚拟按钮:

要将虚拟按钮添加到图像目标,请将 VirtualButton 元素及其属性添加到 .xml 文件中的 ImageTarget 元素。

XML 属性:

  1. 名称 - 按钮的唯一名称
  2. 矩形 - 由矩形中的四个角定义 目标坐标space
  3. Enabled - 一个布尔值,指示是否应启用该按钮 默认
  4. 灵敏度 - 对遮挡的高、中、低灵敏度

您可以在unity project .

中的streamingAsset文件夹中获取.Xml文件
 <ImageTarget size="247 173" name="wood">
  <VirtualButton name="red" sensitivity="HIGH" rectangle="-108.68 -53.52 -75.75 -65.87"
   enabled="true" />
  <VirtualButton name="blue" sensitivity="LOW" rectangle="-45.28 -53.52 -12.35 -65.87"
   enabled="true" />
  <VirtualButton name="yellow" sensitivity="MEDIUM" rectangle="14.82 -53.52 47.75 -65.87"
   enabled="true" />
  <VirtualButton name="green" rectangle="76.57 -53.52 109.50 -65.87"
   enabled="true" />
</ImageTarget>

注册虚拟按钮后代码很简单然后:

public class Custom_VirtualButton : MonoBehaviour, IVirtualButtonEventHandler
{
    // Use this for initialization
    void Start () {

// here it finds any VirtualButton Attached to the ImageTarget and register it's event handler and in the
//OnButtonPressed and OnButtonReleased methods you can handle different buttons Click state
//via "vb.VirtualButtonName" variable and do some really awesome stuff with it.
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
        foreach (VirtualButtonBehaviour item in vbs)
        {
            item.RegisterEventHandler(this);
        }

    }

    // Update is called once per frame
    void Update () {

    }


    #region VirtualButton

    public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
    {
        Debug.Log("Helllllloooooooooo");
    }

    public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
    {
        Debug.Log("Goooooodbyeeee");
    }

    #endregion //VirtualButton
}

写完这段代码后,你必须去 StreamingAsset/QCAR 找到你的 ImageTarget XML 关联并做这样的事情:

 <?xml version="1.0" encoding="UTF-8"?>
<QCARConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="qcar_config.xsd">
  <Tracking>
    <ImageTarget name="marker01" size="100.000000 100.000000">

      <VirtualButton name="red" rectangle="-49.00 -9.80 -18.82 -40.07" enabled="true" />
    </ImageTarget>
  </Tracking>
</QCARConfig>

祝你好运:) Bdw CAT 太可爱了:)