- Home /
Mouse down on GUIButton
How do I make something happen when I click a GUIButon but not on mouse release.
If I use this code:
if ((GUI.Button(Rect(800,100,110,110),btnTexture)))
it reacts when I release it :(
Answer by Joshua · Feb 23, 2011 at 04:23 PM
I think if you make it work on function OnMouseDown () it should work :)
So don't put it in your function Update or whatever, make a seperate function:
function OnMouseDown(){ dowhatyouwantittodo; }
but you can't draw gui buttons in an On$$anonymous$$ouseDown function can you?
Answer by TheDemiurge · Feb 23, 2011 at 04:46 PM
If you need action while the mouse is down, you can use GUI.RepeatButton(..)
for it. If you want just one onmousedown, you can either add some manual checks or use a RepeatButton with a flag for being held down. Keep in mind, you should not be processing more than the bare minimum of code inside OnGUI()
since this can get called several times per frame.
Answer by Bill 2 · Oct 29, 2011 at 12:51 PM
private var leftMouseClicked : boolean = false;
function OnGUI() {
//used for on mousedown hack
ix = Input.mousePosition.x;
iy = Input.mousePosition.y;
var transMouse = GUI.matrix.inverse.MultiplyPoint3x4(Vector3(ix, Screen.height - iy, 1));
var closeButRect=Rect ((Screen.width/2) + 135,Screen.height - 470,14,14);
GUI.Button(closeButRect,"", "closeBtn");
if (leftMouseClicked){
if(closeButRect.Contains(Vector2(transMouse.x,transMouse.y))){
//mouse down on but - do something
leftMouseClicked=false;
}
}
}
//hack to get close buttons to respond on mouse down
function Update(){
if(Input.GetMouseButtonDown(0)){
leftMouseClicked=true;
}else{
leftMouseClicked=false;
}
}
Your answer
