android - 从ImageTarget Android获取模型对象

标签 android unity3d augmented-reality

目前,我正在尝试在Android中使用扩增实境。对于此任务,我使用Unity + Vuforia

因此,当我从相机中查看特定对象时,我制作了一个正在运行的场景,它向我展示了我的模型(基本上是带有动画的3D猫模型)。我已经按照这样的教程完成了此操作:
text format tutorial和youtube上的视频是这样的:video tutroial

在此之后,我基于此scene创建了android应用程序,如下所示:
enter image description here

结果是Android项目,该项目基本上具有一个Activity以及一组assetslibs。到目前为止,我看到的与Unity的唯一连接是UnityPlayer类,但这只是从ViewGroup扩展的FrameLayout

public class UnityPlayer extends FrameLayout implements com.unity3d.player.a.a 


我的目标:我需要在我创建的onClick视图(我的3d猫)中覆盖Unity,就像在电话上单击cat那样,它会发出声音并设置一些动画单击后打开它。我在scene上有一个模型,从逻辑上讲,它已经转换为View内的Android类,并且我认为它只是UnityPlayer的子代,但是这样的代码:

mUnityPlayer.getChildAt(0).setOnClickListener


没有效果。

我想要一个包含所有动画和其他属性的对象,这些动画和其他属性可以统一建模,或者如果不可能,请学习如何在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的模型,但它没有隐藏

我已将此脚本附加到虚拟按钮对象,我将提供一个屏幕:

enter image description here

编辑:实际上,由于某种原因,我的错误是我不得不将VirtualButtonBehaviorHandler脚本附加到ImageTarget,对我来说这不是那么容易理解,但是我认为我现在看到了一些逻辑。
但是,由于某些未知原因,如果我要添加以下代码:

public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) {
        //Debug.Log(vb.VirtualButtonName);
        switch(vb.VirtualButtonName) {
        case "btn":
            kitten.setActive(true);
            break;
        }

    }


即使没有按下按钮,它也可以立即运行

最终编辑:这是发生的,因为我已经在数据库.xml中添加了按钮,当我从按钮中删除了按钮时-一切正常,我将唯一的答案标记为正确,因为它可以帮助我

最佳答案

兄弟只要我们做到,一切皆有可能。根据我的理解,您真正想做的是:

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


正如您提到的,白色Cyte :)猫渲染的对象是“标记”
在Unity Everything是游戏对象中,您可以编写脚本来使用脚本来操纵该游戏对象(CAT)。它将使用C#(Mono)或JavaScript进行此工作,您可以使用Visual Studio或Unity的MonoDevelop
但在此之前,请在Google上搜索关键字

a)Touchevent,RayCastMenu统一控制:处理Touch

b)Unity中的MonoBehaviour类,Start(),Update(),OnGUI()方法
您可以使用可以在“检查器”窗口中看到或更改的游戏名称或标签来标识任何游戏对象


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



现在:提出您的问题:
根据我的说法,你想在“猫咪的点击”中做一些事情。
它很简单,如果您只想单击cat即可启动android活动,则有2种可能的方式:


创建android项目并将其作为Unity中的Library项目统一导入。

要么
在C#脚本的帮助下从unity项目创建android活动。将此脚本附加到场景中的任何GameObject。


在这里,我提供第二个示例:单击Button它将启动Android Activity。
您要做的是:

用CAT GameObjectExport Project作为Android将Button GameObject替换为CAT GameObject,并使用C#代码中提到的名称和程序包编写活动,以执行您想要的任何操作

在我的示例中,我已经解释了:


使用Unity + Vuforia检测到标记时如何弹出GUI
如何在特定事件中从Unity Code启动android Activity
如何在Unity中处理事件
如何使用多种分辨率维护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中,一切都是GameObject,您可以编写脚本来操纵任何GameObject

编辑:虚拟按钮的信息

虚拟按钮可检测何时从相机视图中模糊了目标图像的基本功能。您需要将按钮放置在功能丰富的图像区域上,以使其可靠地触发其OnButtonPressed事件。要确定这些功能在图像中的位置,请在目标管理器中使用图像的“显示功能”链接。

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



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



enter image description here



注册虚拟按钮:

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

XML属性:


名称-按钮的唯一名称
矩形-由矩形中四个矩形的角定义
目标的坐标空间
Enabled-一个布尔值,指示是否应启用按钮
默认情况下
敏感性-高,中,低对咬合的敏感性




您可以在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是如此可爱:)

关于android - 从ImageTarget Android获取模型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071058/

相关文章:

android - Dagger 和 ButterKnife Android 的区别

java - 计数器计数不正确

c# - 无法将类型 int' 隐式转换为 unityengine.vector3'

swift - 用于对象识别的 ARKit 和 Vision 框架

android - 需要忘记在 Android 6.0 中以编程方式配置的 Wifi 网络

android - 应用程序仅适用于 mdpi (320x480...)

c# - Unity Input.getAxis 可以使用哪些参数?

c# - 我将如何每 5 到 7 秒实例化一个对象?

ios - ARKit 2 : Virtual Objects are floating when the ARWorldMap is reloaded from backend

ios - Cylinder Target + vuforia ios 上的视频播放