- Home /
GUI Buttons unresponsive in new window
I know I must be doing something very silly since this seems to be working in other parts of my code but..
void OnGUI() { int win_height; int win_width; if (rdc_requestConfirm) { win_height = 60; win_width = 360; win_left = Screen.width - win_width - 25; win_top = 80;
GUI.Window(2, new Rect(win_left, win_top, win_width, win_height), Show_ConfirmationWindow, "");
}
// more stuff }
void Show_ConfirmationWindow(int winID)
{
if (!Confirmation_Pending())
{
// return;
}
int btn_width = 180; int btn_height = 30;
int btn_left_1 = 0; int btn_left_2 = btn_width;
GUI.BringWindowToFront(winID);
GUI.FocusWindow(winID);
if (GUI.Button(new Rect(0, 0, 360, 60), "click")) { Debug.Log("click"); }
GUI.UnfocusWindow();
}
The new window shows up as desire, I can see the button, but when I click on it nothing happens. I've also tried it without setting the focus, with setting the focus in the OnGUI, I just can't seem to get it to do its click action. It does acknowledge that its clicked in the sense that the button changes its look for a moment (and then changes it whenever I mouse over it), just seems like it can't complete its self. I saw someone in the old forums with the same question from a few months ago but I can't seem to find anything with the solution.. So, am I being really silly? Should this be really obvious and I am missing it?
Is it possible that another component with a lower GUI.depth is swallowing your input? Note that OnGUI for all your components will run in order by GUI.depth. I've had trouble with this, not sure if it's your problem though.
Hmm.. not sure - shouldn't setting the Focus Window to that winID right before checking the button deal with a potential problem like that? I do have other windows in the code but there are not visible ones in the same part of the screen as this one, at the time that this one is running anyway. (And this is the only ID 2 window)
I have the same issue a gui.button in my gui.window won't recognize clicks.
I am getting the same effect, but on a draggable window. When making undraggable - the effect dissapears. - And not anymore after moving this line:
GUI.DragWindow (Rect (0,0,10000,10000));
to the end of the window function.
Answer by Daniel Cazan 1 · Jan 11, 2011 at 09:42 PM
I got it working - I moved the call to my window to the bottom of the OnGUI after everything else, and set the GUI.BringWindowToFront(winID); GUI.FocusWindow(winID);
after the call to the actual window creation. I am still not sure how the other windows were taking the focus away unless OnGUI processes everything before doing any of the actual displays / calls to the window functions.. but that seemed to work. As soon as I have some free time I'll start running experiments and maybe update this answer with the 'why' of why the solution worked.
Answer by Ouss · Jan 14, 2014 at 06:18 PM
It's because there is no focus on the window, if you are using multiple windows, you can set focus using GUI.FocusWindow(windowId). For my part, when using multiple windows having different buttons, I put a confition in the function that is creating the window, to check if the mouse cursor is on a window (Rect.Contains) then I set focus on that window.