从ImageTarget Android获取模型对象

时间:2016-05-06 11:18:29

标签: android unity3d augmented-reality

目前,我正在尝试使用android中的扩充现实。对于此任务,我使用Unity + Vuforia

所以,我制作了一个正在工作的场景,当我从相机中查看特定物体时,它向我展示了我的模型(基本上是带动画的3D猫模型)。我按照这样的教程完成了这个: text format tutorial和youtube上的视频如下:video tutroial

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

结果是Android项目,它基本上有一个Activity和一个assetslibs的banch。我到目前为止与Unity类似的唯一连接是UnityPlayer类,但它只是ViewGroup,从FrameLayout

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

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

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的模型,但它并未隐藏

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

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中添加了我的按钮,当我从中删除按钮时 - 一切正常,我只是标记了一个答案是正确的,因为它帮助了我

1 个答案:

答案 0 :(得分:2)

兄弟如果我们这样做,一切皆有可能。根据我的理解,你究竟想做的是:

第一:你需要通过阅读博客&amp ;;来清除一些基本概念。教程:

  1. 正如你所提到的那样,你的白色cyte :)猫呈现的对象是&#34; Marker&#34;

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

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

    b) MonoBehaviour 类,开始()更新(),Unity中的OnGUI()方法

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

  4. 中查看或更改

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

    现在:来看你的问题: 据我说,你想做点甜蜜的猫。 它很简单,如果只是想在点击cat上启动android活动那么有两种可能的方法:

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

      或者

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

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

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

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

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

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

      3. 如何处理Unity中的事件

      4. 如何使用多个分辨率维护GUI

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

        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属性:

        1. 名称 - 按钮的唯一名称
        2. 矩形 - 由矩形中的四个角定义 目标的坐标空间
        3. 已启用 - 一个布尔值,指示是否应启用该按钮 默认情况下
        4. 灵敏度 - 高,中,低对遮挡的敏感度
        5. 您可以在unity projec t中的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 Association&amp;做这样的事情:

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