Search Unity

Vuforia Virtual Buttons anyone?

Discussion in 'Android' started by jbarrett98121, Jul 23, 2012.

  1. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    Hi there! Anyone have any experience in working with vuforia's virtual buttons? I am looking to make a virtual button on the image object, that changed it's child's animation.

    ie, so once you pop in your image under the camera, a 3d model pops up, and then a virtual button is used to change that object's animation.

    so here is the script that i have attached to the virtual button (no effect is acheived right now) :

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class whaleAnimation : MonoBehaviour, IVirtualButtonEventHandler {
    5.    
    6.     public GameObject Character;
    7.    
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     Character = transform.FindChild("humpback").gameObject;
    13.  
    14.    
    15.    
    16.    
    17.     }
    18.    
    19.    
    20.    
    21.    
    22.    
    23.     public void OnButtonPressed(VirtualButtonBehaviour vb){
    24.         Character.animation.Play("death");
    25.         Debug.Log("pressed");
    26.     }
    27.    
    28.    
    29.    
    30.     public void OnButtonReleased(VirtualButtonBehaviour vb) {
    31.         Character.animation.Stop("death");
    32.        
    33.     }
    34.    
    35.    
    36.    
    37.    
    38.    
    39.    
    40.    
    41.    
    42.    
    43.     // Update is called once per frame
    44.     void Update () {
    45.    
    46.     }
    47. }
    48.  

    what am i leaving out?
     
  2. Mickman

    Mickman

    Joined:
    May 9, 2012
    Posts:
    153
    I'm interested in using V-Buttons as well .. So I will have a play around over the next few days let you know how things go.

    I'm also using NGUI .. so I hope it will operate within Vuforia.

    Regards,
    Mick
     
  3. VicM

    VicM

    Joined:
    Mar 14, 2012
    Posts:
    22
    Hi,

    I will try to help, bear with me.

    - Have you tried attaching your script to the ImageTarget instead of to the Virtual button?
    - Also, can you confirm if you are trying you app inside Unity or with the printed marker and the app installed on your device? There will be no effect when testing on inside Unity.

    I know you may already verified this, but also double check if your Character game object has been initialized?

    On the other Hand?, do you see your debug messages on the Console (if you are working with iOS devices)?

    Hope it helps, let me know.

    Also I that doesn´t help, try posting you question on the Vuforia forum as well!
     
    Last edited: Aug 1, 2012
  4. madg

    madg

    Joined:
    Sep 28, 2012
    Posts:
    1
    Hi guys

    Since I got this working, I thought I'd put my findings up.

    Basically the main thing is that each Virtual Button has a "Virtual Button Behaviour" (VBE) script component. This script is a C# class exposing various methods for controlling the execution of your Virtual Button. Whenever something happens, such as pressing the button and releasing it, an event is thrown, and this script calls event handlers. The event handlers are themselves scripts that need to be attached to a game object as a component. The trick is that before these event handlers can be invoked by the Virtual Button they need to be registered with that Virtual Button's VBE script component. These event handlers must implement IVirtualButtonEventHandler.

    What I did is I created a script to change the color of a spotlight. Here is my script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpotlightColorChangeVB : MonoBehaviour, IVirtualButtonEventHandler {
    5.    
    6.     private bool switchState = true;
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.         // Register yourself as a handler for a button
    11.         Object[] objects = FindSceneObjectsOfType(typeof(GameObject));
    12.         foreach (Object vObject in objects) {
    13.             GameObject gameObject = (GameObject) vObject;
    14.             if (gameObject.name.Equals("VirtualButton")) {
    15.                 Debug.Log("Found the VirtualButton GameObject!");
    16.                 VirtualButtonBehaviour vbuttonBehavior = (VirtualButtonBehaviour) gameObject.GetComponent(typeof(VirtualButtonBehaviour));
    17.                 vbuttonBehavior.RegisterEventHandler(this);
    18.                 Debug.Log("I'm registered!");
    19.             }
    20.             Debug.Log(gameObject.name);
    21.         }
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.    
    27.     }
    28.    
    29.     private void switchColor() {
    30.         if (switchState) {
    31.             switchState = false;
    32.             gameObject.light.color = Color.blue;
    33.         } else {
    34.             switchState = true;
    35.             gameObject.light.color = Color.red;
    36.         }
    37.     }
    38.    
    39.     #region IVirtualButtonEventHandler implementation
    40.     public void OnButtonPressed (VirtualButtonBehaviour vb)
    41.     {
    42.         Debug.Log("Button pressed!");
    43.         switchColor();
    44.         //throw new System.NotImplementedException ();
    45.     }
    46.  
    47.     public void OnButtonReleased (VirtualButtonBehaviour vb)
    48.     {
    49.        
    50.         //throw new System.NotImplementedException ();
    51.     }
    52.     #endregion
    53. }
    Excuse the messy coding, I just wanted to get it to work. As you can see, I use the Start() method inherited from the MonoBehaviour interface to look for my Virtual Button (must be the name you gave the Game Object, not the name attribute of the VBE script component), and then I attach this event handler to it. Then it's just a matter of putting some logic in the OnButtonPressed() method to do your bidding when the Virtual Button to which you attached this event handler is pressed (there's also a OnButtonReleased() method you could use, but I just left it blank).

    Just remember to attach this script as a component to a game object in your scene (drag and drop script onto game object in scene hierarchy). I attached mine to the spotlight game object, but I imagine you can attach it to pretty much any game object.

    Hope this helps someone!
    Cheers!
     
  5. moghazy

    moghazy

    Joined:
    Apr 3, 2011
    Posts:
    64
    it doesn't work with me
     
  6. sushil.blore

    sushil.blore

    Joined:
    Mar 26, 2013
    Posts:
    1
    Hey.. even I tried the above logic but it is not working for me. "OnButtonPressed" is not getting called for me. Can you tell if anything else needs to be done..
     
  7. saxelsen

    saxelsen

    Joined:
    May 22, 2013
    Posts:
    3
    Does not work for me either - any advice? I have no functionality in the OnButtonPressed/OnButtonReleased methods apart from a Debug.Log to check that the method gets called, but they don't.
     
  8. javadmajidi

    javadmajidi

    Joined:
    Feb 16, 2014
    Posts:
    1
    Hi There,,
    I Hope This would be informative and useful,,
    first of all i should mention that you have to assign this script to ImageTarget itself,,

    i tried to concisely write the code and not make any ambiguity but as a side note if you want to see the result working properly please
    set the sensivity setting to HIGH.

    :D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D
    _________________________________________
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Custom_VirtualButton : MonoBehaviour, IVirtualButtonEventHandler
    6. {
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10. // here it finds any VirtualButton Attached to the ImageTarget and register it's event handler and in the
    11. //OnButtonPressed and OnButtonReleased methods you can handle different buttons Click state
    12. //via "vb.VirtualButtonName" variable and do some really awesome stuff with it.
    13.         VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
    14.         foreach (VirtualButtonBehaviour item in vbs)
    15.         {
    16.             item.RegisterEventHandler(this);
    17.         }
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.    
    24.     }
    25.  
    26.  
    27.     #region VirtualButton
    28.  
    29.     public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
    30.     {
    31.         Debug.Log("Helllllloooooooooo");
    32.     }
    33.  
    34.     public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
    35.     {
    36.         Debug.Log("Goooooodbyeeee");
    37.     }
    38.  
    39.     #endregion //VirtualButton
    40. }
    41.  
    _________________________________________________________________________________________
    and after writing this code you have to go to StreamingAsset/QCAR and find your ImageTarget XML Association
    file and Open it ,, it should be something like this
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="UTF-8"?>
    3. <QCARConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="qcar_config.xsd">
    4.   <Tracking>
    5.     <ImageTarget name="marker01" size="100.000000 100.000000" />
    6.   </Tracking>
    7. </QCARConfig>
    8.  
    change that file to store the VirtualButton Coordinates in unity world like this:

    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="UTF-8"?>
    3. <QCARConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="qcar_config.xsd">
    4.   <Tracking>
    5.     <ImageTarget name="marker01" size="100.000000 100.000000">
    6.  
    7.       <VirtualButton name="red" rectangle="-49.00 -9.80 -18.82 -40.07" enabled="true" />
    8.     </ImageTarget>
    9.   </Tracking>
    10. </QCARConfig>
    11.  
    but be aware that when you are specifying the coordinate in "rectangle" it is made of 4 coordinate numbers
    the first TWO are TopLeft and The last TWO are ButtomRight of your Button,,
    and also be aware that these numbers specify the coordinates in "World Coordinate System"
    which means you can create some objects like a sphere and it shouldn't be child of any objects
    then place it in the right place(TopLeft of your button) to look at its "x" and "z" coordinate numbers and copy it to the first two number here in the XML file and do the latter for ButtomRight of your button and there you go the scripts you are writing should just work fine.
     
    Last edited: Mar 24, 2014
  9. vanyeezy

    vanyeezy

    Joined:
    Jan 25, 2016
    Posts:
    1
    please how do you get the virtual buttons coordinates?
     
  10. Tiaesstas

    Tiaesstas

    Joined:
    May 30, 2016
    Posts:
    1
    Create an empty game object make it child of your image target and vertex snap it to your virtual buttons, just multiplay the displayed number by 1k and you have what you need, unity usually runs with 1 unit = 1m and 1 millimeter is 1/1000m
     
  11. MikeRomeo

    MikeRomeo

    Joined:
    Feb 4, 2015
    Posts:
    1
    You need to include:

    using Vuforia;

    at the very top of your code just like that.
     
  12. AminaBatool

    AminaBatool

    Joined:
    Feb 1, 2018
    Posts:
    1
    error CS0029: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.Object[]'
    can anyone help how to resolve this
     
  13. kannuchauhan

    kannuchauhan

    Joined:
    Jan 23, 2018
    Posts:
    2
    how to open pdf file using UI button
     
    SANAEBARCHA likes this.
  14. walidabazo

    walidabazo

    Joined:
    Jul 8, 2017
    Posts:
    17
    to create virtual buttons with Vuforia 7 in Unity3D 2017.3

     
  15. walidabazo

    walidabazo

    Joined:
    Jul 8, 2017
    Posts:
    17
    to create virtual buttons with Vuforia 7 in Unity3D 2017.3

     
  16. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    388
    Thanks for the video, but why is it running so fast?1