- Home /
Gui.Button over another gui.button
Hi! I have a gui.button and another second over him to cancel the action. The problem is if I use this code:
if(GUI.Button(Rect(30, 100, 70, 70), mainBut))
{
Debug.Log("MAIN BUTTON");
}
if(GUI.Button (Rect(160,100,20,20), cancel))
{
Debug.Log("CANCEL");
}
shows the cancel button but doesn't work, and If I use this:
if(GUI.Button (Rect(160,100,20,20), cancel))
{
Debug.Log("CANCEL");
}
if(GUI.Button(Rect(30, 100, 70, 70), mainBut))
{
Debug.Log("MAIN BUTTON");
}
the cancel button works but dont shows like the image(second)
Some ideas to fix this?
Thanks in advance! :D
Answer by robertbu · Feb 22, 2014 at 07:10 PM
The coordinates you provide for your Rects in the code will not overlap like you have in your image. But here is a hack to solve your problem:
if(GUI.Button (Rect(160,100,20,20), cancel))
{
Debug.Log("CANCEL");
}
if(GUI.Button(Rect(30, 100, 70, 70), mainBut))
{
Debug.Log("MAIN BUTTON");
}
if(GUI.Button (Rect(160,100,20,20), cancel))
{
Debug.Log("CANCEL");
}
A more involved solution would be to test the Rect of the cancel button. If you need code for this instead, let me know.
I just tested it again, and it works for me. Last time I tested the buttons with strings. I used textures this time:
#pragma strict
var cancel : Texture;
var mainBut : Texture;
function OnGUI() {
if(GUI.Button (Rect(30,100,20,20), cancel))
{
Debug.Log("CANCEL");
}
if(GUI.Button(Rect(30, 100, 70, 70), mainBut))
{
Debug.Log("$$anonymous$$AIN BUTTON");
}
if(GUI.Button (Rect(30,100,20,20), cancel))
{
Debug.Log("CANCEL");
}
}
Again, this is a bit of a hack to work around the Unity issue of drawing last but capturing input first. There are other more complex approaches, but I don't understand why this is not working for you.
Your answer
Follow this Question
Related Questions
Camera being disabled by any GUI button 2 Answers
3D Button? 1 Answer
GUI.Label positioning for many device resolutions 1 Answer
detect mouseover with grid buttons? 1 Answer
Resolutions 1 Answer