Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Help with Vuforia project

Discussion in 'Editor & General Support' started by Spinnernicholas, Dec 4, 2013.

  1. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    nevermind i fixed the animation part but i need the button to follow the image target
     
  2. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Ok, now we have to replace the OnGUI button with a Vuforia virtual button. In the Vuforia Target manager, can you add virtual buttons?
     
  3. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    the online target manager?
     
  4. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    do you mean on the imagetarget?
     
  5. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    The online one.
     
  6. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    a database?
     
  7. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    It looks to me, from reading the Vuforia Docs, like the definitions for virtual buttons can be added in the Online Target Manager, that includes the textures for the buttons as well as positions and other properties. Then, you download the target files and import them into Unity. Finally, you can just attach actions to the already defined button.

    But, I am not 100% sure.
     
  8. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    yea you can do that but how do i combine the gui button and the virtual button
     
  9. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Does the virtual button appear automatically?
     
  10. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    as to my knowledge a virtual button is invisible. it shows up in your scene but won't when you play it
     
  11. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    and i just retested that and it doesn't show up when you play it
     
  12. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    are there inspector settings when you select the virtual button in the editor?
     
  13. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Can you find and post this file for me: <database_name>.xml

    It should be in the target database you downloaded.
     
  14. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    yes
    script
    name
    sentivity
     
  15. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    the virtual target doesn't use the database. the imagetarget does.
     
  16. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    this is the database i am using
     
  17. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Can you click advanced and post it as code(the # sign), a section of it turned into a smiley.
     
  18. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Actually, don't worry about it. I'm guessing you are tracking stardust?
     
  19. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    crorrect
     
  20. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Change

    Code (csharp):
    1.  
    2. <ImageTarget name="ehneos" size="400.000000 587.000000" />
    3.  
    to

    Code (csharp):
    1.  
    2. <ImageTarget name="stardust" size="400.000000 578.553589">
    3.       <VirtualButton name="attack" rectangle="-108.68 -53.52 -75.75 -65.87" enabled="true" />
    4. </ImageTarget>
    5.  
    and then let me know if anything changes.
     
  21. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    Code (csharp):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <QCARConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="qcar_config.xsd">
    3.   <Tracking>
    4.     <ImageTarget name="ehneos" size="400.000000 587.000000" />
    5.     <ImageTarget name="stardust" size="400.000000 578.553589" />
    6.   </Tracking>
    7. </QCARConfig>
    8.  
     
  22. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    here is the virtualbuttonbehavior code. i figured you'd want it

    Code (csharp):
    1. /*==============================================================================
    2. Copyright (c) 2010-2013 QUALCOMM Austria Research Center GmbH.
    3. All Rights Reserved.
    4. Confidential and Proprietary - QUALCOMM Austria Research Center GmbH.
    5. ==============================================================================*/
    6.  
    7. using System;
    8. using System.Collections.Generic;
    9. using System.Runtime.InteropServices;
    10. using UnityEngine;
    11.  
    12. /// <summary>
    13. /// This behaviour associates a Virtual Button with a game object. Use the
    14. /// functionality in ImageTargetBehaviour to create and destroy Virtual Buttons
    15. /// at run-time.
    16. /// </summary>
    17. public class VirtualButtonBehaviour : MonoBehaviour, IEditorVirtualButtonBehaviour
    18. {
    19.    
    20.     #region PROPERTIES
    21.    
    22.     /// <summary>
    23.     /// The names of this virtual button.
    24.     /// </summary>
    25.     public string VirtualButtonName
    26.     {
    27.         get { return mName; }
    28.     }
    29.    
    30.    
    31.     /// <summary>
    32.     /// Returns true if this button is currently pressed.
    33.     /// </summary>
    34.     public bool Pressed
    35.     {
    36.         get { return mPressed; }
    37.     }
    38.    
    39.    
    40.     /// <summary>
    41.     /// if the pose has been updated once
    42.     /// </summary>
    43.     public bool HasUpdatedPose
    44.     {
    45.         get { return mHasUpdatedPose; }
    46.     }
    47.    
    48.    
    49.     /// <summary>
    50.     /// Unregistering Virtual Buttons should only be done if they have been
    51.     /// registered at runtime. This property is automatically set by
    52.     /// ImageTargetBehaviour on registration.
    53.     /// </summary>
    54.     public bool UnregisterOnDestroy
    55.     {
    56.         get
    57.         {
    58.             return mUnregisterOnDestroy;
    59.         }
    60.        
    61.         set
    62.         {
    63.             mUnregisterOnDestroy = value;
    64.         }
    65.     }
    66.    
    67.    
    68.     /// <summary>
    69.     /// The VirtualButton Object created at runtime
    70.     /// </summary>
    71.     public VirtualButton VirtualButton
    72.     {
    73.         get { return mVirtualButton; }
    74.     }
    75.    
    76.     #endregion // PROPERTIES
    77.    
    78.    
    79.    
    80.     #region CONSTANTS
    81.    
    82.     /// <summary>
    83.     /// The vertical offset of the graphic representation of a virtual button in respect to the target
    84.     /// </summary>
    85.     public const float TARGET_OFFSET = 0.001f;
    86.    
    87.     #endregion // CONSTANTS
    88.    
    89.    
    90.    
    91.     #region PRIVATE_MEMBER_VARIABLES
    92.    
    93.     [SerializeField]
    94.     [HideInInspector]
    95.     private string mName;
    96.    
    97.     [SerializeField]
    98.     [HideInInspector]
    99.     private VirtualButton.Sensitivity mSensitivity;
    100.    
    101.     [SerializeField]
    102.     [HideInInspector]
    103.     private bool mHasUpdatedPose = false;
    104.    
    105.     [SerializeField]
    106.     [HideInInspector]
    107.     private Matrix4x4 mPrevTransform = Matrix4x4.zero;
    108.    
    109.     [SerializeField]
    110.     [HideInInspector]
    111.     private GameObject mPrevParent = null;
    112.    
    113.     private bool mSensitivityDirty;
    114.     private bool mPreviouslyEnabled;
    115.     private bool mPressed;
    116.     private List<IVirtualButtonEventHandler> mHandlers = null;
    117.     private Vector2 mLeftTop;
    118.     private Vector2 mRightBottom;
    119.     private bool mUnregisterOnDestroy;
    120.    
    121.     VirtualButton mVirtualButton;
    122.    
    123.     #endregion // PRIVATE_MEMBER_VARIABLES
    124.    
    125.    
    126.    
    127.     #region CONSTRUCTION
    128.    
    129.     public VirtualButtonBehaviour()
    130.     {
    131.         mName = "";
    132.         mPressed = false;
    133.         mSensitivity = VirtualButton.DEFAULT_SENSITIVITY;
    134.         mSensitivityDirty = false;
    135.         mHandlers = new List<IVirtualButtonEventHandler>();
    136.         mHasUpdatedPose = false;
    137.     }
    138.    
    139.     #endregion // CONSTRUCTION
    140.    
    141.    
    142.    
    143.     #region PUBLIC_METHODS
    144.    
    145.     /// <summary>
    146.     /// Registers an event handler with this Virtual Button which will be called
    147.     /// when a state changed is detected.
    148.     /// </summary>
    149.     public void RegisterEventHandler(IVirtualButtonEventHandler eventHandler)
    150.     {
    151.         mHandlers.Add(eventHandler);
    152.     }
    153.    
    154.    
    155.     /// <summary>
    156.     /// Registers an event handler with this Virtual Button which will be called
    157.     /// when a state changed is detected.
    158.     /// Returns true on success. False otherwise.
    159.     /// </summary>
    160.     public bool UnregisterEventHandler(IVirtualButtonEventHandler eventHandler)
    161.     {
    162.         return mHandlers.Remove(eventHandler);
    163.     }
    164.    
    165.    
    166.     /// <summary>
    167.     /// Calculates the 2D button area that the Virtual Button currently occupies
    168.     /// in the Image Target.
    169.     /// Returns true if the area was computed successfully. False otherwise.
    170.     /// Passes out the top left and bottom right position of the rectangle area.
    171.     /// </summary>
    172.     public bool CalculateButtonArea(out Vector2 topLeft,
    173.                                     out Vector2 bottomRight)
    174.     {
    175.         // Error if we don't have an image target as a root:
    176.         ImageTargetBehaviour itb = this.GetImageTargetBehaviour();
    177.         if (itb == null)
    178.         {
    179.             topLeft = bottomRight = Vector2.zero;
    180.             return false;
    181.         }
    182.        
    183.         Vector3 vbPosITSpace = itb.transform.InverseTransformPoint(
    184.             this.transform.position);
    185.        
    186.         // The scale of the image Target:
    187.         float itScale = itb.transform.lossyScale[0];
    188.        
    189.         // Scale the button position:
    190.         Vector2 pos = new Vector2(vbPosITSpace[0] * itScale,
    191.                                   vbPosITSpace[2] * itScale);
    192.        
    193.         // Scale the button area:
    194.         Vector2 scale = new Vector2(this.transform.lossyScale[0],
    195.                                     this.transform.lossyScale[2]);
    196.        
    197.         // Calculate top left and bottom right points:
    198.         Vector2 radius = Vector2.Scale(scale * 0.5F, new Vector2(1.0f, -1.0f));
    199.        
    200.         topLeft = pos - radius;
    201.         bottomRight = pos + radius;
    202.        
    203.         // Done:
    204.         return true;
    205.     }
    206.    
    207.    
    208.     /// <summary>
    209.     /// Update the virtual button rect in native
    210.     /// </summary>
    211.     public bool UpdateAreaRectangle()
    212.     {
    213.         RectangleData rectData = new RectangleData();
    214.        
    215.         rectData.leftTopX = mLeftTop.x;
    216.         rectData.leftTopY = mLeftTop.y;
    217.         rectData.rightBottomX = mRightBottom.x;
    218.         rectData.rightBottomY = mRightBottom.y;
    219.        
    220.         if (mVirtualButton == null) return false;
    221.        
    222.         return mVirtualButton.SetArea(rectData);
    223.     }
    224.    
    225.    
    226.     /// <summary>
    227.     /// Update sensitivity in native
    228.     /// </summary>
    229.     public bool UpdateSensitivity()
    230.     {
    231.         if (mVirtualButton == null) return false;
    232.        
    233.         return mVirtualButton.SetSensitivity(mSensitivity);
    234.     }
    235.    
    236.    
    237.     /// <summary>
    238.     /// Update enabled status in native
    239.     /// </summary>
    240.     private bool UpdateEnabled()
    241.     {
    242.         return mVirtualButton.SetEnabled(enabled);
    243.     }
    244.    
    245.    
    246.     /// <summary>
    247.     /// UpdatePose() is called each frame to ensure the virtual button is clamped
    248.     /// to the image target plane and remains axis-aligned with respect to the
    249.     /// target. Return true if the defining area of the virtual button has
    250.     /// changed, false otherwise.
    251.     /// </summary>
    252.     public bool UpdatePose()
    253.     {
    254.         // The image target to which the button belongs:
    255.         ImageTargetBehaviour itb = this.GetImageTargetBehaviour();
    256.        
    257.         // If there is no image target we return:
    258.         if (itb == null)
    259.         {
    260.             return false;
    261.         }
    262.        
    263.         // We explicitly disallow any objects with non-uniform scaling in the
    264.         // object hierachy of the virtual button. Combined with a rotation
    265.         // this would result in skewing the virtual button.
    266.         Transform t = transform.parent;
    267.         while (t != null)
    268.         {
    269.             if (t.localScale[0] != t.localScale[1] ||
    270.                 t.localScale[0] != t.localScale[2])
    271.             {
    272.                 Debug.LogWarning("Detected non-uniform scale in virtual " +
    273.                                  " button object hierarchy. Forcing uniform scaling of " +
    274.                                  "object '" + t.name + "'.");
    275.                
    276.                 //  Force uniform scale:
    277.                 t.localScale = new Vector3(t.localScale[0], t.localScale[0],
    278.                                            t.localScale[0]);
    279.             }
    280.             t = t.parent;
    281.         }
    282.        
    283.         // Remember we have updated once:
    284.         mHasUpdatedPose = true;
    285.        
    286.         // Clamp to center of parent object:
    287.         if (transform.parent != null
    288.             transform.parent.gameObject != itb.gameObject)
    289.         {
    290.             transform.localPosition = Vector3.zero;
    291.         }
    292.        
    293.         // Clamp position to image target plane:
    294.         Vector3 vbPosITSpace = itb.transform.InverseTransformPoint(
    295.             this.transform.position);
    296.        
    297.         // Set the y offset in Image Target space:
    298.         vbPosITSpace.y = TARGET_OFFSET;
    299.         Vector3 vbPosWorldSpace = itb.transform.TransformPoint(vbPosITSpace);
    300.         this.transform.position = vbPosWorldSpace;
    301.        
    302.         // Clamp orientation to the image target plane:
    303.         this.transform.rotation = itb.transform.rotation;
    304.        
    305.         // Update the button area:
    306.         Vector2 leftTop, rightBottom;
    307.         CalculateButtonArea(out leftTop, out rightBottom);
    308.        
    309.         // Change the button area only if the change is larger than a fixed
    310.         // proportion of the image target size:
    311.         float threshold = itb.transform.localScale[0] * 0.001f;
    312.        
    313.         if (!Equals(leftTop, mLeftTop, threshold) ||
    314.             !Equals(rightBottom, mRightBottom, threshold))
    315.         {
    316.             // Area has changed significantly:
    317.             mLeftTop = leftTop;
    318.             mRightBottom = rightBottom;
    319.             return true;
    320.         }
    321.        
    322.         // Area has not changed significantly:
    323.         return false;
    324.     }
    325.    
    326.    
    327.     /// <summary>
    328.     /// Called after the QCARBehaviour has updated.
    329.     /// </summary>
    330.     public void OnTrackerUpdated(bool pressed)
    331.     {
    332.         if (mPreviouslyEnabled != enabled)
    333.         {
    334.             mPreviouslyEnabled = enabled;
    335.             UpdateEnabled();
    336.         }
    337.        
    338.         if (!enabled)
    339.         {
    340.             return;
    341.         }
    342.        
    343.         // Trigger the appropriate callback if there was state change:
    344.         if (mPressed != pressed  mHandlers != null)
    345.         {
    346.             if (pressed)
    347.             {
    348.                 foreach (IVirtualButtonEventHandler handler in mHandlers)
    349.                 {
    350.                     handler.OnButtonPressed(this);
    351.                 }
    352.             }
    353.             else
    354.             {
    355.                 foreach (IVirtualButtonEventHandler handler in mHandlers)
    356.                 {
    357.                     handler.OnButtonReleased(this);
    358.                 }
    359.             }
    360.         }
    361.        
    362.         // Cache pressed state:
    363.         mPressed = pressed;
    364.     }
    365.    
    366.    
    367.     /// <summary>
    368.     /// Returns the Image Target that this Virtual Button is associated with.
    369.     /// </summary>
    370.     public ImageTargetBehaviour GetImageTargetBehaviour()
    371.     {
    372.         if (transform.parent == null)
    373.             return null;
    374.        
    375.         GameObject p = transform.parent.gameObject;
    376.        
    377.         while (p != null)
    378.         {
    379.             ImageTargetBehaviour itb = p.GetComponent<ImageTargetBehaviour>();
    380.             if (itb != null)
    381.             {
    382.                 return itb;
    383.             }
    384.            
    385.             if (p.transform.parent == null)
    386.             {
    387.                 // Not found:
    388.                 return null;
    389.             }
    390.            
    391.             p = p.transform.parent.gameObject;
    392.         }
    393.        
    394.         // Not found:
    395.         return null;
    396.     }
    397.    
    398.     #endregion // PUBLIC_METHODS
    399.    
    400.    
    401.    
    402.     #region EDITOR_INTERFACE_IMPLEMENTATION
    403.    
    404.     // Initializes the Virtual Button name. Not allowed after runtime object has been created.
    405.     bool IEditorVirtualButtonBehaviour.SetVirtualButtonName(string virtualButtonName)
    406.     {
    407.         if (mVirtualButton == null)
    408.         {
    409.             mName = virtualButtonName;
    410.             return true;
    411.         }
    412.        
    413.         return false;
    414.     }
    415.    
    416.    
    417.     VirtualButton.Sensitivity IEditorVirtualButtonBehaviour.SensitivitySetting
    418.     {
    419.         get { return mSensitivity; }
    420.     }
    421.    
    422.     // sets the sensitivity. At runtime the VirtualButton object should be used to change sensibility.
    423.     bool IEditorVirtualButtonBehaviour.SetSensitivitySetting(VirtualButton.Sensitivity sensibility)
    424.     {
    425.         if (mVirtualButton == null)
    426.         {
    427.             mSensitivity = sensibility;
    428.             mSensitivityDirty = true;
    429.             return true;
    430.         }
    431.        
    432.         return false;
    433.     }
    434.    
    435.     Matrix4x4 IEditorVirtualButtonBehaviour.PreviousTransform
    436.     {
    437.         get { return mPrevTransform; }
    438.     }
    439.    
    440.     bool IEditorVirtualButtonBehaviour.SetPreviousTransform(Matrix4x4 transform)
    441.     {
    442.         if (mVirtualButton == null)
    443.         {
    444.             mPrevTransform = transform;
    445.             return true;
    446.         }
    447.        
    448.         return false;
    449.     }
    450.    
    451.     GameObject IEditorVirtualButtonBehaviour.PreviousParent
    452.     {
    453.         get { return mPrevParent; }
    454.     }
    455.    
    456.     bool IEditorVirtualButtonBehaviour.SetPreviousParent(GameObject parent)
    457.     {
    458.         if (mVirtualButton == null)
    459.         {
    460.             mPrevParent = parent;
    461.             return true;
    462.         }
    463.        
    464.         return false;
    465.     }
    466.    
    467.     // Initializes the Virtual Button runtime object
    468.     void IEditorVirtualButtonBehaviour.InitializeVirtualButton(VirtualButton virtualButton)
    469.     {
    470.         mVirtualButton = virtualButton;
    471.     }
    472.    
    473.     // Sets position and scale in the transform component of the Virtual Button
    474.     // game object. The values are calculated from rectangle values (top-left
    475.     // and bottom-right corners).
    476.     // Returns false if Virtual Button is not child of an Image Target.
    477.     bool IEditorVirtualButtonBehaviour.SetPosAndScaleFromButtonArea(Vector2 topLeft, Vector2 bottomRight)
    478.     {
    479.         // Error if we don't have an image target as a root:
    480.         ImageTargetBehaviour itb = this.GetImageTargetBehaviour();
    481.         if (itb == null)
    482.         {
    483.             return false;
    484.         }
    485.        
    486.         float itScale = itb.transform.lossyScale[0];
    487.        
    488.         Vector2 pos = (topLeft + bottomRight) * 0.5f;
    489.        
    490.         Vector2 scale = new Vector2(bottomRight[0] - topLeft[0],
    491.                                     topLeft[1] - bottomRight[1]);
    492.        
    493.         Vector3 vbPosITSpace =
    494.             new Vector3(pos[0] / itScale, VirtualButtonBehaviour.TARGET_OFFSET,
    495.                         pos[1] / itScale);
    496.        
    497.        
    498.         Vector3 vbScaleITSpace =
    499.             new Vector3(scale[0],
    500.                         (scale[0] + scale[1]) * 0.5f,
    501.                         scale[1]);
    502.        
    503.         this.transform.position = itb.transform.TransformPoint(vbPosITSpace);
    504.        
    505.         // Image Target scale is canceled out (included in both scales)
    506.         this.transform.localScale =
    507.             vbScaleITSpace / this.transform.parent.lossyScale[0];
    508.        
    509.         // Done:
    510.         return true;
    511.     }
    512.    
    513.     #endregion // EDITOR_INTERFACE_IMPLEMENTATION
    514.    
    515.    
    516.    
    517.     #region UNITY_MONOBEHAVIOUR_METHODS
    518.    
    519.     // Overriding standard Unity MonoBehaviour methods.
    520.    
    521.     void LateUpdate()
    522.     {
    523.         // Update the button pose:
    524.         if (UpdatePose())
    525.         {
    526.             // Area has changed, update the QCAR trackable:
    527.             UpdateAreaRectangle();
    528.         }
    529.        
    530.         // Update the sensitivity of the button if it has changed since the
    531.         // last update:
    532.         if (mSensitivityDirty)
    533.         {
    534.             if (UpdateSensitivity())
    535.             {
    536.                 mSensitivityDirty = false;
    537.             }
    538.         }
    539.     }
    540.    
    541.    
    542.     void OnDisable()
    543.     {
    544.         if (QCARRuntimeUtilities.IsQCAREnabled())
    545.         {
    546.             if (mPreviouslyEnabled != enabled)
    547.             {
    548.                 mPreviouslyEnabled = enabled;
    549.                 UpdateEnabled();
    550.             }
    551.            
    552.             // Trigger the appropriate callback if there was state change:
    553.             if (mPressed  mHandlers != null)
    554.             {
    555.                 foreach (IVirtualButtonEventHandler handler in mHandlers)
    556.                 {
    557.                     handler.OnButtonReleased(this);
    558.                 }
    559.             }
    560.            
    561.             // Cache pressed state:
    562.             mPressed = false;
    563.         }
    564.     }
    565.    
    566.    
    567.     void OnDestroy()
    568.     {
    569.         if (Application.isPlaying)
    570.         {
    571.             if (mUnregisterOnDestroy)
    572.             {
    573.                 ImageTargetBehaviour itb = GetImageTargetBehaviour();
    574.                 if (itb != null)
    575.                     itb.ImageTarget.DestroyVirtualButton(mVirtualButton);
    576.             }
    577.         }
    578.     }
    579.    
    580.     #endregion // UNITY_MONOBEHAVIOUR_METHODS
    581.    
    582.    
    583.    
    584.     #region PRIVATE_METHODS
    585.    
    586.     private static bool Equals(Vector2 vec1, Vector2 vec2, float threshold)
    587.     {
    588.         Vector2 diff = vec1 - vec2;
    589.         return (Math.Abs(diff.x) < threshold)  (Math.Abs(diff.y) < threshold);
    590.     }
    591.    
    592.     #endregion // PRIVATE_METHODS
    593. }
    594.  
     
  23. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    yeah, that's coming up, thank you.
     
  24. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    what is suppose to be different
     
  25. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    It added a button definition, I wasn't sure if it would do anything, but I was hoping it would add a virtual button object automatically and then we would just be able to replace the VirtualButtonBehaviour script. Or something like that. What does the image tracker hierarchy look like? Do you see a VirtualButtonBehaviour script attached anywhere?
     
  26. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    the only virtualbuttonbehavior script is the one in the inspector of the virtual button that i attached to the the imagetarget myself
     
  27. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
  28. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Ok, drag the virtual button prefab onto the scene and make it a child of the imagetarget.

    You might have already done that.
     
  29. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    I did that
     
  30. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    It looks like virtual buttons are not really what you want. They create virtual real world buttons. So, you would like tap on the card itself instead of on the screen.
     
  31. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    yea, i need a button that says attack that follows the imageTarget
     
  32. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    I have to stop for now, I'll be back in a few hours. It will give me some time to work out some details.
     
  33. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    ok i will be here waiting. but before you go do you think you can help me with a simple GUI thing

    this code right here

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class CameraDeviceMenu : MonoBehaviour
    7. {
    8.     #region NESTED
    9.    
    10.     // Defines which menu to show.
    11.     private enum MenuMode
    12.     {
    13.         MENU_OFF,            // Do not show a menu (default).
    14.         MENU_CAMERA_OPTIONS, // Show camera device options.
    15.         MENU_FOCUS_MODES     // Show focus mode menu.
    16.     }
    17.    
    18.     #endregion // NESTED
    19.    
    20.    
    21.    
    22.     #region PRIVATE_MEMBER_VARIABLES
    23.    
    24.     // Currently active menu.
    25.     private MenuMode mMenuToShow = MenuMode.MENU_OFF;
    26.    
    27.     // Check if a menu button has been pressed.
    28.     private bool mButtonPressed = false;
    29.    
    30.     // Check if flash is currently enabled.
    31.     private bool mFlashEnabled = false;
    32.    
    33.     // Contains the currently set auto focus mode.
    34.     private CameraDevice.FocusMode mFocusMode =
    35.         CameraDevice.FocusMode.FOCUS_MODE_NORMAL;
    36.    
    37.     // Contains the rectangle for the camera options menu.
    38.     private Rect mAreaRect;
    39.    
    40.     #endregion // PRIVATE_MEMBER_VARIABLES
    41.    
    42.    
    43.    
    44.     #region UNTIY_MONOBEHAVIOUR_METHODS
    45.    
    46.     public void Start()
    47.     {
    48.         // Setup position and size of the camera menu.
    49.         computePosition();
    50.     }
    51.    
    52.    
    53.     public void OnApplicationPause(bool pause)
    54.     {
    55.         // Upon resuming reactivate the torch if it was active before pausing:
    56.         if (!pause)
    57.         {
    58.             if (mFlashEnabled)
    59.             {
    60.                 mFlashEnabled = CameraDevice.Instance.SetFlashTorchMode(true);
    61.             }
    62.         }
    63.     }
    64.    
    65.    
    66.     public void Update()
    67.     {
    68.         // If the touch event results from a button press it is ignored.
    69.         if (!mButtonPressed)
    70.         {
    71.             // If finger is removed from screen.
    72.             if (Input.GetMouseButtonUp(0))
    73.             {
    74.                 // If menu is not rendered.
    75.                 if (mMenuToShow == MenuMode.MENU_OFF)
    76.                 {
    77.                     // Show menu.
    78.                     mMenuToShow = MenuMode.MENU_CAMERA_OPTIONS;
    79.                 }
    80.                 // If menu is already open.
    81.                 else
    82.                 {
    83.                     // Close menu
    84.                     mMenuToShow = MenuMode.MENU_OFF;
    85.                 }
    86.             }
    87.         }
    88.         else
    89.         {
    90.             mButtonPressed = false;
    91.         }
    92.     }
    93.    
    94.    
    95.     // Draw menus.
    96.     public void OnGUI()
    97.     {
    98.         switch (mMenuToShow)
    99.         {
    100.         case MenuMode.MENU_CAMERA_OPTIONS:
    101.             DrawMenu();
    102.             break;
    103.            
    104.         case MenuMode.MENU_FOCUS_MODES:
    105.             DrawFocusModes();
    106.             break;
    107.            
    108.         default:
    109.             break;
    110.         }
    111.     }
    112.    
    113.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    114.    
    115.    
    116.    
    117.     #region PRIVATE_METHODS
    118.    
    119.     // Draw menu to control camera device.
    120.     private void DrawMenu()
    121.     {
    122.         computePosition();
    123.        
    124.         // Setup style for buttons.
    125.         GUIStyle buttonGroupStyle = new GUIStyle(GUI.skin.button);
    126.         buttonGroupStyle.stretchWidth = true;
    127.         buttonGroupStyle.stretchHeight = true;
    128.        
    129.         GUILayout.BeginArea(mAreaRect);
    130.        
    131.         GUILayout.BeginHorizontal(buttonGroupStyle);
    132.        
    133.         // Turn flash on or off.
    134.         if (GUILayout.Button("Toggle Flash", buttonGroupStyle))
    135.         {
    136.             if (!mFlashEnabled)
    137.             {
    138.                 // Turn on flash if it is currently disabled.
    139.                 CameraDevice.Instance.SetFlashTorchMode(true);
    140.                 mFlashEnabled = true;
    141.             }
    142.             else
    143.             {
    144.                 // Turn off flash if it is currently enabled.
    145.                 CameraDevice.Instance.SetFlashTorchMode(false);
    146.                 mFlashEnabled = false;
    147.             }
    148.            
    149.             mMenuToShow = MenuMode.MENU_OFF;
    150.             mButtonPressed = true;
    151.         }
    152.        
    153.         // Triggers auto focus:
    154.         if (GUILayout.Button("Autofocus", buttonGroupStyle))
    155.         {
    156.             if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO))
    157.                 mFocusMode = CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO;
    158.            
    159.             mMenuToShow = MenuMode.MENU_OFF;
    160.             mButtonPressed = true;
    161.         }
    162.        
    163.         // Choose focus mode.
    164.         if (GUILayout.Button("Focus Modes", buttonGroupStyle))
    165.         {
    166.             mMenuToShow = MenuMode.MENU_FOCUS_MODES;
    167.             mButtonPressed = true;
    168.         }
    169.        
    170.         GUILayout.EndHorizontal();
    171.        
    172.         GUILayout.EndArea();
    173.     }
    174.    
    175.    
    176.     // Draw menu to let user choose a focus mode.
    177.     private void DrawFocusModes()
    178.     {
    179.         CameraDevice.FocusMode newMode;
    180.         newMode = EnumOptionList(mFocusMode);
    181.        
    182.         // We set the new value only if the mode has changed.
    183.         if (newMode != mFocusMode)
    184.         {
    185.             if (CameraDevice.Instance.SetFocusMode(newMode))
    186.                 mFocusMode = newMode;
    187.            
    188.             mMenuToShow = MenuMode.MENU_OFF;
    189.             mButtonPressed = true;
    190.         }
    191.     }
    192.    
    193.    
    194.     // Helper function to automatically create an option list of an enum object.
    195.     private static CameraDevice.FocusMode EnumOptionList(
    196.         CameraDevice.FocusMode setMode)
    197.     {
    198.         Type modeType = setMode.GetType();
    199.        
    200.         // Get possible enum values.
    201.         CameraDevice.FocusMode[] modes =
    202.             (CameraDevice.FocusMode[])Enum.GetValues(modeType);
    203.        
    204.         // Setup style for list.
    205.         GUIStyle optionListStyle = new GUIStyle(GUI.skin.button);
    206.         optionListStyle.stretchHeight = true;
    207.         optionListStyle.stretchWidth = true;
    208.        
    209.         // Setup style for toggles.
    210.         // We use "button" style as template because default toggles are too
    211.         // small.
    212.         GUIStyle toggleStyle = new GUIStyle(GUI.skin.button);
    213.         toggleStyle.stretchHeight = true;
    214.         toggleStyle.stretchWidth = true;
    215.         toggleStyle.normal.textColor = Color.gray;
    216.         toggleStyle.onNormal.textColor = Color.gray;
    217.         toggleStyle.focused.textColor = Color.gray;
    218.         toggleStyle.onFocused.textColor = Color.gray;
    219.         toggleStyle.active.textColor = Color.gray;
    220.         toggleStyle.onActive.textColor = Color.gray;
    221.         toggleStyle.hover.textColor = Color.gray;
    222.         toggleStyle.onHover.textColor = Color.gray;
    223.        
    224.         // Setup style for active toggle.
    225.         // Setting active values for the toggle Style does not work so we create
    226.         // another style.
    227.         GUIStyle activeToggleStyle = new GUIStyle(toggleStyle);
    228.         activeToggleStyle.normal.textColor = Color.white;
    229.         activeToggleStyle.onNormal.textColor = Color.white;
    230.         activeToggleStyle.focused.textColor = Color.white;
    231.         activeToggleStyle.onFocused.textColor = Color.white;
    232.         activeToggleStyle.active.textColor = Color.white;
    233.         activeToggleStyle.onActive.textColor = Color.white;
    234.         activeToggleStyle.hover.textColor = Color.white;
    235.         activeToggleStyle.onHover.textColor = Color.white;
    236.        
    237.        
    238.         CameraDevice.FocusMode newMode = setMode;
    239.        
    240.         // We render the menu over the full screen.
    241.         GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
    242.        
    243.         GUILayout.BeginVertical();
    244.        
    245.         foreach (CameraDevice.FocusMode mode in modes)
    246.         {
    247.             if (mode == setMode)
    248.             {
    249.                 GUILayout.Toggle(true, mode.ToString(), activeToggleStyle);
    250.             }
    251.             else
    252.             {
    253.                 if (GUILayout.Toggle(false, mode.ToString(), toggleStyle))
    254.                 {
    255.                     newMode = mode;
    256.                 }
    257.             }
    258.         }
    259.        
    260.         GUILayout.EndVertical();
    261.        
    262.         GUILayout.EndArea();
    263.        
    264.         return newMode;
    265.     }
    266.    
    267.    
    268.     /// Compute the coordinates of the menu depending on the current orientation.
    269.     private void computePosition()
    270.     {
    271.         int areaWidth = Screen.width;
    272.         int areaHeight = (Screen.height / 5) * 2;
    273.         int areaLeft = 0;
    274.         int areaTop = Screen.height - areaHeight;
    275.         mAreaRect = new Rect(areaLeft, areaTop, areaWidth, areaHeight);
    276.     }
    277.    
    278.     #endregion // PRIVATE_METHODS
    279. }
    280.  
    shows up whenever you touch the screen. it pops open a menu.
    I want to make it show up from a GUI Button press instead a touch of the screen.

    here is the code of the button i made
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [ExecuteInEditMode]
    6. public class GUI_settings : MonoBehaviour {
    7.     public Texture2D menu;
    8.    
    9.     // Use this for initialization
    10.     private void Start () {
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     private void Update () {
    16.        
    17.     }
    18.     private void OnGUI() {
    19.         if (GUI.Button (new Rect (Screen.width - (15*(Screen.width)/100),0,15*(Screen.width)/100,10*(Screen.height)/100), menu)) {
    20.             Application.LoadLevel(0);
    21.         }
    22.     }
    23. }
    24.  
    I know it will be a very common sense fix, but i can't figure it out.
     
  34. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    Have you been able to figure out how to attach the GUI?
     
  35. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Sorry, life got hectic for a little while. Yeah, I have a way to do it. You were on the right track before. Now that we have the animation working, it should be pretty straight forward.

    First off, if you still have that plane that you were going to use, you need to replace it with a quad. Quads have 2 triangles, Planes have 200. For a flat image, like a button, you only need 2.

    You are going to have to create a texture for what you want your button to look like. After, you have the texture, apply it to the quad and position and size the quad where you like it relative to the image target.

    Add a collider to it and position and size it.

    Finally, let me drum up some code to handle the mouse input.

    I'm still really busy, so give me a day.

    If you want to take a shot at it yourself, here's what you need to do:

    • Create a script and attach it to your button.
    • Create public variable to reference the animation preview script I gave you.
    • In update(), check for mouse click.
    • If mouse click, use the camera to create a ray cast from the mouse position.
    • Use physics to cast the ray, check if the return collider is equal to the button's collider.
    • If it is, then call the animation preview method.
     
  36. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    how do I create the collider
     
  37. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    select the gameobject and then find collider in menu on top menu bar:

    Components>Physics>Collider

    or Something like that.
     
  38. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    ok so i guess use the box collider. I couldn't find one with quad that is why i asked
     
  39. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    oh yeah, use a box collider.

    Just shrink it down so it is very thin. It needs a little thickness, but just a little.
     
  40. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    i did it
     
  41. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    any news?
     
  42. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Sorry, I've been super busy.
     
  43. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    its cool, take your time
     
  44. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Ok, Attach this to your camera:

    Code (csharp):
    1. public class MouseHandler: MonoBehavior
    2. {
    3.     void Update()
    4.     {
    5.         if(Input.touchCount > 0)
    6.         {
    7.             Touch touch = Input.touches[0];
    8.             if(touch.phase = TouchPhase.Began)
    9.             {
    10.                 Ray ray = camera.ScreenPointToRay(new Vector3(touch.position.X, touch.position.Y));
    11.                 RaycastHit hit;
    12.                 if(Physics.Raycast(ray, out hit, 100))
    13.                 {
    14.                     hit.collider.SendMessage("OnTouch");
    15.                 }
    16.             }
    17.         }
    18.     }
    19. }
    there might be a couple wrong letter caps, so post any errors you get.
    Also, I'm guessing you are testing on a device with a touch screen. If not, let me know.
     
  45. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Attach this to the object with the button collider.

    Code (csharp):
    1. public class buttonHandler : MonoBehavior
    2. {
    3.     public Previewanimation animatedObject;
    4.  
    5.     void OnTouch()
    6.     {
    7.         animatedObject.PreviewAnimation("stardust_waiting", "stardust_attack");
    8.     }
    9. }
    And then assign animatedObject in the preview.
     
  46. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    If both of those work, that should do it.
     
  47. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    ok thanks. I will attach that but since i update vuforia, there has been an error:

    Assets/Qualcomm Augmented Reality/Scripts/DefaultTrackableEventHandler.cs(50,52): error CS0117: `TrackableBehaviour.Status' does not contain a definition for `EXTENDED_TRACKED'

    I posted it in the Vuforia forum and I am waiting for the response
     
  48. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    I've gotten this error:
    error CS0246: The type or namespace name `MonoBehavior' could not be found. Are you missing a using directive or an assembly reference?

    retyping monobehavior fixed that but a lot of errors just appeared.

    error CS0200: Property or indexer `UnityEngine.Touch.phase' cannot be assigned to (it is read only)

    error CS1061: Type `UnityEngine.Vector2' does not contain a definition for `X' and no extension method `X' of type `UnityEngine.Vector2' could be found (are you missing a using directive or an assembly reference?)

    Assets/MouseHandler.cs(24,112): error CS1061: Type `UnityEngine.Vector2' does not contain a definition for `Y' and no extension method `Y' of type `UnityEngine.Vector2' could be found (are you missing a using directive or an assembly reference?)


    there is more and I will share them if neeeded
     
    Last edited: Dec 18, 2013
  49. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    I'll look into the first error.

    The second one is a typo on my part. Change it to MonoBehaviour(forgot u) at the top of both scripts.
     
  50. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    that extended track error that I posted about Vuforia already helped me with.

    but I am still getting those errors I posted last