Search Unity

Pressing buttons through a window

Discussion in 'Scripting' started by Metal Head, Dec 1, 2010.

  1. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    When I create a GUI window and bring it over a button, I can click the button through the window. I've been searching the GUI class for something, that can be helpful, but no luck.

    How can I make it so when the window is over a button ,the button wouldn't work?
     
    Last edited: Dec 1, 2010
  2. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Not too clear with with your question, but the following script, these buttons are working:

    C#
    Code (csharp):
    1.  
    2. private Rect winRect = new Rect(150, 20, 120, 120);
    3. // Use this for initialization
    4. void Start ()
    5.  {
    6.    
    7. }
    8.  
    9.   void OnGUI()
    10.   {
    11.     if (GUI.Button(new Rect(10, 50, 100, 20), "Main Button"))
    12.       Debug.Log("Clicked on the Main Button!");
    13.  
    14.     winRect = GUI.Window(0, winRect, CreateWinContent, "New Window");
    15.   }
    16.    
    17. // Update is called once per frame
    18. void Update ()
    19. {
    20.    
    21. }
    22.  
    23.   private void CreateWinContent(int winHandleID)
    24.   {
    25.     if (GUI.Button(new Rect(10, 50, 100, 20), "Click Me Button"))
    26.       Debug.Log("You clicked Me!");
    27.  
    28.     // Make the windows be draggable.
    29.     GUI.DragWindow(new Rect(0, 0, 10000, 10000));
    30.   }
    31.  
    32.  
    JavaScript
    Code (csharp):
    1.  
    2. private var winRect: Rect(150, 20, 120, 120);
    3. // Use this for initialization
    4. function Start ()
    5. {
    6.    
    7. }
    8.  
    9. function OnGUI()
    10. {
    11.     if (GUI.Button(Rect(10, 50, 100, 20), "Main Button"))
    12.       Debug.Log("Clicked on the Main Button!");
    13.  
    14.     winRect = GUI.Window(0, winRect, CreateWinContent, "New Window");
    15. }
    16.    
    17. // Update is called once per frame
    18. function Update ()
    19. {
    20.    
    21. }
    22.  
    23. function CreateWinContent(var winHandleID: int)
    24.   {
    25.     if (GUI.Button(Rect(10, 50, 100, 20), "Click Me Button"))
    26.       Debug.Log("You clicked Me!");
    27.  
    28.     // Make the windows be draggable.
    29.     GUI.DragWindow(Rect(0, 0, 10000, 10000));
    30.   }
    31.  
    Important that you assign an unique window handle number for each window.
     
  3. Ezzerland

    Ezzerland

    Joined:
    Jul 1, 2010
    Posts:
    405
    This is a known bug in unity. There are a few workarounds, I'm certain you can google for. One option you could do is add a boolean variable so that when your window is open, so that your buttons cannot be clicked while that bool is true. However, that defeats the purpose of a draggable window. So you could similarly check to see if the window is being hit with your clicks prior to determining if the button is hit with the clicks so you know they are trying to click through the window.

    -Rob
     
  4. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Interesting! Now, I understand the question.
     
  5. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    Yes, that's what I need, to make the buttons not respond if a window is over them. The problem is that my windows are draggable. OK, so does anyone know if this bug is going to be fixed soon in the upcomming versions ? There's a lot of time 'till I finnish my game, so I can wait a little for that.
    The other problem is that I can have several windows opened in my game, and checking this will be a little complicated (hey, not saying impossible to me).
     
  6. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    OK, I tried checking if the cursor is in the window and here's what happens:

    Code (csharp):
    1. playerWindow = GUI.Window(0, playerWindow , PlayerSettings, "Player Settings");
    2. if(playerWindow.Contains) print("yes");
    This thing seems not correct. The GUI.Window function returns a Rect, right ?
    Well, then playerWindow.Contains should work just fine, but instead the console gives some strange message, that I couldn't even understand :D