Hi, I’ve got this grid of GUI buttons, and I basically want to execute some code whenever the player clicks anywhere BUT on one of these buttons, I’ve tried some things, but none worked so far, and I’m out of idea’s
So the question is:
Does anybody know of a way to check if no button was clicked?
Some sort of variable perhaps? Something that would check if the mouse is over a button would work as well, but I can’t seem to find anything. D:
If this is gonna be a hard to do thing I could just leave it out, but it would be a waste since it would mean some other functions I made wouldn’t work properly anymore.
var clicked = false;
function OnMouseDown(){
clicked = true;
}
//in the Update now
function Update(){
//check if you clicked on the button
if(!clicked){
DoSomething();
//going to make sure clicked doesnt stay true forever
}else{
clicked = false;
}
Right now if its false, which it is by default, it does something, if you want to click anywhere exept the buttons(of course you want that, srry, but this way it is more easy to understand, I think…)
then say :
For what it’s worth, I ran into this problem when making an “exit” button on a mobile phone app. The above solution didn’t work for me, as TouchPhase.Began seems to take precedence over everything.
My workaround was to make invisible buttons all around my primary button that detected if I clicked off of it. A blank GUI Style makes the buttons invisible.
//This button will quit the game
if (GUI.Button(Rect(Screen.width/2-120,Screen.height/2-60,240,121),exitTexture,customGuiStyle)){
Application.Quit();
}
//These invisible buttons will unpause the game if any other area is clicked
if (GUI.Button(Rect(0,0,Screen.width,Screen.height/2-60),blankTexture,customGuiStyle)){ //top
Time.timeScale = 1;
showExitButton = false;
}
if (GUI.Button(Rect(0,Screen.height/2+60,Screen.width,Screen.height/2-60),blankTexture,customGuiStyle)){//bottom
Time.timeScale = 1;
showExitButton = false;
}
if (GUI.Button(Rect(0,Screen.height/2-60,Screen.width/2-120,121),blankTexture,customGuiStyle)){//left
Time.timeScale = 1;
showExitButton = false;
}
if (GUI.Button(Rect(Screen.width/2+120,Screen.height/2-60,Screen.width/2-120,121),blankTexture,customGuiStyle)){//lef
Time.timeScale = 1;
showExitButton = false;
}