- Home /
GUI Button overlap problem
Two buttons position overlap in some area,when the Mouse over this area, The over effection will happen in the same time. But I want that when I over the Button,the "MouseOver Event" only happen in the foucus window or the top depth( even I set different depth ,it happen the same). So,How can I change that thing? Thx.....
here the image:
When you say "focus window", are you using Unity Windows?
If so, I may have something lying around in a test project.
Yeah,I test in window,but even without window,It happen the same, you can try(C#): GUI.Button(new Rect(10,10,100,100),"A"); GUI.Button(new Rect(20,20,100,100),"B");
And two buttons' over Effection will happen when your mouse over their share area.
Answer by Waz · Aug 03, 2011 at 04:06 AM
Below is a complete working example of windowing with control over window focusing.
#pragma strict
var windowRect0 : Rect = Rect (20, 20, 120, 150);
var windowRect1 : Rect = Rect (20, 100, 120, 150);
var focusedWindow = 0; // Initially focused window.
var show= [true,true]; // Initially shown windows.
var clickOnUnfocused = false; // What style of windowing do you want?
private var closeWindow = -1;
private var toDraw = 0;
private var neverFocused = true;
function OnGUI()
{
// Need to know how many we draw to work out which is on top.
toDraw = 0;
if (show[0]) { toDraw++; windowRect0 = GUILayout.Window (0, windowRect0, DoMyWindow, "Window 0"); }
if (show[1]) { toDraw++; windowRect1 = GUILayout.Window (1, windowRect1, DoMyWindow, "Window 1"); }
if (neverFocused) {
// Set initial focus (default is none!!)
neverFocused = false;
GUI.FocusWindow(focusedWindow);
}
if (closeWindow >= 0) {
// Close if instructed to in DoMyWindow.
show[closeWindow] = false;
closeWindow = -1;
}
}
function DoMyWindow(windowID : int)
{
if (Event.current.type == EventType.Repaint) {
// Are we the last (top) window, and so the focused one?
toDraw--;
if (toDraw == 0)
focusedWindow = windowID;
}
// Unfocused windows drawn disabled, and unless clickOnUnfocused also actually disabled.
if (focusedWindow != windowID && (Event.current.type == EventType.Repaint || !clickOnUnfocused))
GUI.enabled = false;
if (GUILayout.Button("Close")) {
// Just register the close, actually do it back in OnGUI, otherwise
// window closes mid-way through event processing, muddling toDraw.
Debug.Log("Close "+windowID);
closeWindow = windowID;
}
if (GUILayout.Button("Hello")) {
Debug.Log("Clicked "+windowID);
}
// Window-specific controls, here just a toggle for clickOnUnfocused.
if (windowID == 0)
clickOnUnfocused = GUILayout.Toggle(clickOnUnfocused,"Click On Unfocused");
// Always allow dragging.
GUI.enabled = true;
GUI.DragWindow (Rect (0,0,10000,10000));
}
I guess Unity doesn't enforce any policies like this so that you can implement any style you like. It would be nice though if the API at least had GUI.focusedWindow
(as inferred in the code above), since the GUI code clearly knows it, given the visualization.
Your answer
Follow this Question
Related Questions
Overlapping GUI buttons 1 Answer
button drawn last not triggered 0 Answers
Overlapping GUITexture UI Buttons 2 Answers
Overlapping buttons? 0 Answers
How to WaitforSeconds a GuiButton ? 0 Answers