【问题标题】:Get model object from ImageTarget Android从 ImageTarget Android 获取模型对象
【发布时间】:2016-05-06 11:18:28
【问题描述】:

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

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

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

结果是Android项目,它基本上有一个Activity和一个assetslibs的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


    【解决方案1】:

    Brother 如果我们这样做,一切皆有可能。根据我的理解,你到底想要做什么:

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

    1. 正如您提到的,您的白色 cyte :) 猫渲染的对象是“标记”

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

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

      b) MonoBehaviour 类、Start()Update()、Unity 中的 OnGUI() 方法

    3. 您可以使用其名称或标签来识别任何游戏对象,您可以在Inspector窗口

    4. 中查看或更改它们

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


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

    1. 创建android项目并在Unity中统一导入为Library项目。

    2. 借助 C# 脚本从 unity 项目创建 android 活动。将此脚本附加到场景中的任何 GameObject。

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

    将 CAT GameObjectExport Project 中的 Button GameObject 替换为 Android,并使用与 C# 代码中提及的名称和包相同的 Activity 来执行您想做的任何事情

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

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

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

    3. 如何在 Unity 中处理事件

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


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

    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 事件。要确定这些特征在您的图像中的位置,请在目标管理器中使用您的图像的显示特征链接。

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


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



    注册虚拟按钮:

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

    XML 属性:

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

    你可以在unity项目的streamingAsset文件夹中获取.Xml文件t .

     <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 太可爱了:)

    【讨论】:

    • 是的,你的回答很有帮助,正如你所提到的,一切都是一个基本的游戏对象,我已经为使用虚拟按钮制作了一个脚本,这里唯一的问题,它根本不起作用,我已经对我的问题进行了更改,请有空的时候看看
    • 你好,请检查编辑的答案,如果你有任何困难,请随时询问
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多