Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with Vuforia project

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

  1. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
  2. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    did you get the picture?
     
  3. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Yeah, I got it. I'm not sure what the problem is. Can you post the two scripts for me?
     
  4. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    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.  
    96.     public void Update()
    97.        
    98.     {
    99.        
    100.         // If the touch event results from a button press it is ignored.
    101.        
    102.         if (!mButtonPressed)
    103.            
    104.         {
    105.            
    106.             // If finger is removed from screen.
    107.            
    108.             if (Input.GetMouseButtonUp(0))
    109.                
    110.             {
    111.                
    112.                 // If menu is not rendered.
    113.                
    114.                 if (mMenuToShow == MenuMode.MENU_OFF)
    115.                    
    116.                 {
    117.                    
    118.                     // Show menu.
    119.                    
    120.                     //    mMenuToShow = MenuMode.MENU_CAMERA_OPTIONS;
    121.                    
    122.                 }
    123.                
    124.                 // If menu is already open.
    125.                
    126.                 else
    127.                    
    128.                 {
    129.                    
    130.                     // Close menu
    131.                    
    132.                     mMenuToShow = MenuMode.MENU_OFF;
    133.                    
    134.                 }
    135.                
    136.             }
    137.            
    138.         }
    139.        
    140.         else
    141.            
    142.         {
    143.            
    144.             mButtonPressed = false;
    145.            
    146.         }
    147.        
    148.     }
    149.  
    150.     public void ShowMenu()
    151.        
    152.     {
    153.        
    154.         mMenuToShow = MenuMode.MENU_CAMERA_OPTIONS;
    155.        
    156.     }
    157.  
    158.    
    159.     // Draw menus.
    160.     public void OnGUI()
    161.     {
    162.         switch (mMenuToShow)
    163.         {
    164.         case MenuMode.MENU_CAMERA_OPTIONS:
    165.             DrawMenu();
    166.             break;
    167.            
    168.         case MenuMode.MENU_FOCUS_MODES:
    169.             DrawFocusModes();
    170.             break;
    171.            
    172.         default:
    173.             break;
    174.         }
    175.     }
    176.    
    177.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    178.    
    179.    
    180.    
    181.     #region PRIVATE_METHODS
    182.    
    183.     // Draw menu to control camera device.
    184.     private void DrawMenu()
    185.     {
    186.         computePosition();
    187.        
    188.         // Setup style for buttons.
    189.         GUIStyle buttonGroupStyle = new GUIStyle(GUI.skin.button);
    190.         buttonGroupStyle.stretchWidth = true;
    191.         buttonGroupStyle.stretchHeight = true;
    192.        
    193.         GUILayout.BeginArea(mAreaRect);
    194.        
    195.         GUILayout.BeginHorizontal(buttonGroupStyle);
    196.        
    197.         // Turn flash on or off.
    198.         if (GUILayout.Button("Toggle Flash", buttonGroupStyle))
    199.         {
    200.             if (!mFlashEnabled)
    201.             {
    202.                 // Turn on flash if it is currently disabled.
    203.                 CameraDevice.Instance.SetFlashTorchMode(true);
    204.                 mFlashEnabled = true;
    205.             }
    206.             else
    207.             {
    208.                 // Turn off flash if it is currently enabled.
    209.                 CameraDevice.Instance.SetFlashTorchMode(false);
    210.                 mFlashEnabled = false;
    211.             }
    212.            
    213.             mMenuToShow = MenuMode.MENU_OFF;
    214.             mButtonPressed = true;
    215.         }
    216.        
    217.         // Triggers auto focus:
    218.         if (GUILayout.Button("Autofocus", buttonGroupStyle))
    219.         {
    220.             if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO))
    221.                 mFocusMode = CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO;
    222.            
    223.             mMenuToShow = MenuMode.MENU_OFF;
    224.             mButtonPressed = true;
    225.         }
    226.        
    227.         // Choose focus mode.
    228.         if (GUILayout.Button("Focus Modes", buttonGroupStyle))
    229.         {
    230.             mMenuToShow = MenuMode.MENU_FOCUS_MODES;
    231.             mButtonPressed = true;
    232.         }
    233.        
    234.         GUILayout.EndHorizontal();
    235.        
    236.         GUILayout.EndArea();
    237.     }
    238.    
    239.    
    240.     // Draw menu to let user choose a focus mode.
    241.     private void DrawFocusModes()
    242.     {
    243.         CameraDevice.FocusMode newMode;
    244.         newMode = EnumOptionList(mFocusMode);
    245.        
    246.         // We set the new value only if the mode has changed.
    247.         if (newMode != mFocusMode)
    248.         {
    249.             if (CameraDevice.Instance.SetFocusMode(newMode))
    250.                 mFocusMode = newMode;
    251.            
    252.             mMenuToShow = MenuMode.MENU_OFF;
    253.             mButtonPressed = true;
    254.         }
    255.     }
    256.    
    257.    
    258.     // Helper function to automatically create an option list of an enum object.
    259.     private static CameraDevice.FocusMode EnumOptionList(
    260.         CameraDevice.FocusMode setMode)
    261.     {
    262.         Type modeType = setMode.GetType();
    263.        
    264.         // Get possible enum values.
    265.         CameraDevice.FocusMode[] modes =
    266.             (CameraDevice.FocusMode[])Enum.GetValues(modeType);
    267.        
    268.         // Setup style for list.
    269.         GUIStyle optionListStyle = new GUIStyle(GUI.skin.button);
    270.         optionListStyle.stretchHeight = true;
    271.         optionListStyle.stretchWidth = true;
    272.        
    273.         // Setup style for toggles.
    274.         // We use "button" style as template because default toggles are too
    275.         // small.
    276.         GUIStyle toggleStyle = new GUIStyle(GUI.skin.button);
    277.         toggleStyle.stretchHeight = true;
    278.         toggleStyle.stretchWidth = true;
    279.         toggleStyle.normal.textColor = Color.gray;
    280.         toggleStyle.onNormal.textColor = Color.gray;
    281.         toggleStyle.focused.textColor = Color.gray;
    282.         toggleStyle.onFocused.textColor = Color.gray;
    283.         toggleStyle.active.textColor = Color.gray;
    284.         toggleStyle.onActive.textColor = Color.gray;
    285.         toggleStyle.hover.textColor = Color.gray;
    286.         toggleStyle.onHover.textColor = Color.gray;
    287.        
    288.         // Setup style for active toggle.
    289.         // Setting active values for the toggle Style does not work so we create
    290.         // another style.
    291.         GUIStyle activeToggleStyle = new GUIStyle(toggleStyle);
    292.         activeToggleStyle.normal.textColor = Color.white;
    293.         activeToggleStyle.onNormal.textColor = Color.white;
    294.         activeToggleStyle.focused.textColor = Color.white;
    295.         activeToggleStyle.onFocused.textColor = Color.white;
    296.         activeToggleStyle.active.textColor = Color.white;
    297.         activeToggleStyle.onActive.textColor = Color.white;
    298.         activeToggleStyle.hover.textColor = Color.white;
    299.         activeToggleStyle.onHover.textColor = Color.white;
    300.        
    301.        
    302.         CameraDevice.FocusMode newMode = setMode;
    303.        
    304.         // We render the menu over the full screen.
    305.         GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
    306.        
    307.         GUILayout.BeginVertical();
    308.        
    309.         foreach (CameraDevice.FocusMode mode in modes)
    310.         {
    311.             if (mode == setMode)
    312.             {
    313.                 GUILayout.Toggle(true, mode.ToString(), activeToggleStyle);
    314.             }
    315.             else
    316.             {
    317.                 if (GUILayout.Toggle(false, mode.ToString(), toggleStyle))
    318.                 {
    319.                     newMode = mode;
    320.                 }
    321.             }
    322.         }
    323.        
    324.         GUILayout.EndVertical();
    325.        
    326.         GUILayout.EndArea();
    327.        
    328.         return newMode;
    329.     }
    330.    
    331.    
    332.     /// Compute the coordinates of the menu depending on the current orientation.
    333.     private void computePosition()
    334.     {
    335.         int areaWidth = Screen.width;
    336.         int areaHeight = (Screen.height / 5) * 2;
    337.         int areaLeft = 0;
    338.         int areaTop = Screen.height - areaHeight;
    339.         mAreaRect = new Rect(areaLeft, areaTop, areaWidth, areaHeight);
    340.     }
    341.    
    342.     #endregion // PRIVATE_METHODS
    343. }
    344.  
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. using System.Collections;
    5.  
    6.  
    7.  
    8. public class GUI_settings : MonoBehaviour
    9.    
    10. {
    11.    
    12.     public Texture2D menu;
    13.    
    14.     public CameraDeviceMenu cameraMenu;
    15.    
    16.    
    17.    
    18.     private void OnGUI() {
    19.        
    20.         if (GUI.Button (new Rect (Screen.width - (15*(Screen.width)/100),0,15*(Screen.width)/100,10*(Screen.height)/100), menu)) {
    21.            
    22.             cameraMenu.ShowMenu();
    23.            
    24.         }
    25.        
    26.     }
    27.    
    28. }
    29.  
     
  5. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Alright, I modified the menu script:

    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.     public void ShowMenu()
    66.     {
    67.         if(mMenuToShow == MenuMode.MENU_OFF)
    68.         {
    69.             mMenuToShow = MenuMode.MENU_CAMERA_OPTIONS;
    70.         }
    71.         else
    72.         {
    73.             mMenuToShow = MenuMode.MENU_OFF;
    74.         }
    75.     }
    76.    
    77.     // Draw menus.
    78.     public void OnGUI()
    79.     {
    80.         switch (mMenuToShow)
    81.         {
    82.         case MenuMode.MENU_CAMERA_OPTIONS:
    83.             DrawMenu();
    84.             break;
    85.            
    86.         case MenuMode.MENU_FOCUS_MODES:
    87.             DrawFocusModes();
    88.             break;
    89.            
    90.         default:
    91.             break;
    92.         }
    93.     }
    94.    
    95.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    96.    
    97.     #region PRIVATE_METHODS
    98.    
    99.     // Draw menu to control camera device.
    100.     private void DrawMenu()
    101.     {
    102.         computePosition();
    103.        
    104.         // Setup style for buttons.
    105.         GUIStyle buttonGroupStyle = new GUIStyle(GUI.skin.button);
    106.         buttonGroupStyle.stretchWidth = true;
    107.         buttonGroupStyle.stretchHeight = true;
    108.        
    109.         GUILayout.BeginArea(mAreaRect);
    110.        
    111.         GUILayout.BeginHorizontal(buttonGroupStyle);
    112.        
    113.         // Turn flash on or off.
    114.         if (GUILayout.Button("Toggle Flash", buttonGroupStyle))
    115.         {
    116.             if (!mFlashEnabled)
    117.             {
    118.                 // Turn on flash if it is currently disabled.
    119.                 CameraDevice.Instance.SetFlashTorchMode(true);
    120.                 mFlashEnabled = true;
    121.             }
    122.             else
    123.             {
    124.                 // Turn off flash if it is currently enabled.
    125.                 CameraDevice.Instance.SetFlashTorchMode(false);
    126.                 mFlashEnabled = false;
    127.             }
    128.            
    129.             mMenuToShow = MenuMode.MENU_OFF;
    130.             mButtonPressed = true;
    131.         }
    132.        
    133.         // Triggers auto focus:
    134.         if (GUILayout.Button("Autofocus", buttonGroupStyle))
    135.         {
    136.             if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO))
    137.                 mFocusMode = CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO;
    138.            
    139.             mMenuToShow = MenuMode.MENU_OFF;
    140.             mButtonPressed = true;
    141.         }
    142.        
    143.         // Choose focus mode.
    144.         if (GUILayout.Button("Focus Modes", buttonGroupStyle))
    145.         {
    146.             mMenuToShow = MenuMode.MENU_FOCUS_MODES;
    147.             mButtonPressed = true;
    148.         }
    149.        
    150.         GUILayout.EndHorizontal();
    151.         GUILayout.EndArea();
    152.     }
    153.    
    154.     // Draw menu to let user choose a focus mode.
    155.     private void DrawFocusModes()
    156.     {
    157.         CameraDevice.FocusMode newMode;
    158.         newMode = EnumOptionList(mFocusMode);
    159.        
    160.         // We set the new value only if the mode has changed.
    161.         if (newMode != mFocusMode)
    162.         {
    163.             if (CameraDevice.Instance.SetFocusMode(newMode))
    164.                 mFocusMode = newMode;
    165.            
    166.             mMenuToShow = MenuMode.MENU_OFF;
    167.             mButtonPressed = true;
    168.         }
    169.     }
    170.    
    171.     // Helper function to automatically create an option list of an enum object.
    172.     private static CameraDevice.FocusMode EnumOptionList(
    173.         CameraDevice.FocusMode setMode)
    174.     {
    175.         Type modeType = setMode.GetType();
    176.        
    177.         // Get possible enum values.
    178.         CameraDevice.FocusMode[] modes =
    179.             (CameraDevice.FocusMode[])Enum.GetValues(modeType);
    180.        
    181.         // Setup style for list.
    182.         GUIStyle optionListStyle = new GUIStyle(GUI.skin.button);
    183.         optionListStyle.stretchHeight = true;
    184.         optionListStyle.stretchWidth = true;
    185.        
    186.         // Setup style for toggles.
    187.         // We use "button" style as template because default toggles are too
    188.         // small.
    189.         GUIStyle toggleStyle = new GUIStyle(GUI.skin.button);
    190.         toggleStyle.stretchHeight = true;
    191.         toggleStyle.stretchWidth = true;
    192.         toggleStyle.normal.textColor = Color.gray;
    193.         toggleStyle.onNormal.textColor = Color.gray;
    194.         toggleStyle.focused.textColor = Color.gray;
    195.         toggleStyle.onFocused.textColor = Color.gray;
    196.         toggleStyle.active.textColor = Color.gray;
    197.         toggleStyle.onActive.textColor = Color.gray;
    198.         toggleStyle.hover.textColor = Color.gray;
    199.         toggleStyle.onHover.textColor = Color.gray;
    200.        
    201.         // Setup style for active toggle.
    202.         // Setting active values for the toggle Style does not work so we create
    203.         // another style.
    204.         GUIStyle activeToggleStyle = new GUIStyle(toggleStyle);
    205.         activeToggleStyle.normal.textColor = Color.white;
    206.         activeToggleStyle.onNormal.textColor = Color.white;
    207.         activeToggleStyle.focused.textColor = Color.white;
    208.         activeToggleStyle.onFocused.textColor = Color.white;
    209.         activeToggleStyle.active.textColor = Color.white;
    210.         activeToggleStyle.onActive.textColor = Color.white;
    211.         activeToggleStyle.hover.textColor = Color.white;
    212.         activeToggleStyle.onHover.textColor = Color.white;
    213.        
    214.        
    215.         CameraDevice.FocusMode newMode = setMode;
    216.        
    217.         // We render the menu over the full screen.
    218.         GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
    219.         GUILayout.BeginVertical();
    220.        
    221.         foreach (CameraDevice.FocusMode mode in modes)
    222.         {
    223.             if (mode == setMode)
    224.             {
    225.                 GUILayout.Toggle(true, mode.ToString(), activeToggleStyle);
    226.             }
    227.             else
    228.             {
    229.                 if (GUILayout.Toggle(false, mode.ToString(), toggleStyle))
    230.                 {
    231.                     newMode = mode;
    232.                 }
    233.             }
    234.         }
    235.        
    236.         GUILayout.EndVertical();
    237.         GUILayout.EndArea();
    238.         return newMode;
    239.     }
    240.    
    241.    
    242.     /// Compute the coordinates of the menu depending on the current orientation.
    243.     private void computePosition()
    244.     {
    245.         int areaWidth = Screen.width;
    246.         int areaHeight = (Screen.height / 5) * 2;
    247.         int areaLeft = 0;
    248.         int areaTop = Screen.height - areaHeight;
    249.         mAreaRect = new Rect(areaLeft, areaTop, areaWidth, areaHeight);
    250.     }
    251.        
    252.     #endregion // PRIVATE_METHODS
    253. }
    254.  
     
  6. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    THanks it works
     
  7. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Hey, if you haven't selected my answer in Unity Answers, can you. Greatly Appreciated.
     
  8. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    I will try again. i think i tried before but it said i didn't have enough points or something
     
  9. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Lol, really. Hahaha. Well, if that's the case, don't worry.
     
  10. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    yea it says

    "We're sorry, but this cannot be voted up by you until you have at least 15 reputation."
     
  11. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    nevermind i just noticed the check mark
     
  12. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Thank you, I'm hoping high Karma will help me get noticed by game companies.
     
  13. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    no thank you for your help. I plan to have you on a credit page for this app
     
  14. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    That would be awesome!
     
  15. thekirant400

    thekirant400

    Joined:
    Apr 18, 2014
    Posts:
    1
    I am getting the same error How it was resolved ?