Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Hiding buttons (GUI.Button)

Discussion in 'Immediate Mode GUI (IMGUI)' started by jeemly, Dec 18, 2014.

  1. jeemly

    jeemly

    Joined:
    Dec 18, 2014
    Posts:
    1
    Hello friends, someone who can help me ?? I'm working with augmented reality in unity and have 2 buttons (GUI.Button) which you want me to appear only when the camera detects the marker, and the buttons are hidden if the camera does not find the marker. Any suggestions?
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Try:
    Code (csharp):
    1. private bool cameraDetectsMarker = false;
    2.  
    3. void Update() {
    4.     cameraDetectsMarker = //(your code to check if camera detects marker)
    5. }
    6.  
    7. void OnGUI() {
    8.     if (cameraDetectsMarker) {
    9.         if (GUI.Button(rect1, "button 1")) {
    10.             //(code when button 1 is clicked)
    11.         }
    12.         if (GUI.Button(rect2, "button 2")) {
    13.             //(code when button 2 is clicked)
    14.         }
    15.     }
    16. }
    Detect the marker in a separate routine, like Update(), not in OnGUI(). This is because OnGUI() is called several times per frame and shouldn't do anything except GUI drawing and input.