- Home /
Get status of GUI button
I want it when your finger is on the button, i want the variable to be true, and when its not, i want it to be false. But GUI button is a click function so it just is a trigger not a boolean. What do i do to make it true if finger is there, and false if it is not. Idk if it will help but here is my OnGUI function
function OnGUI(){
GUI.Label(Rect(15, 15, 32, 32), planeCrash.rings.ToString(), Style);
if(GUI.Button(Rect(Screen.width*0.9, Screen.height*0.9, 100, 50), buttonRight, Style)){
yaw = 75;
animator.SetBool("yawRight", true);
rightButton = true;
}else{
animator.SetBool("yawRight", false);
rightButton = false;
}
if(GUI.Button(Rect(Screen.width*0.1, Screen.height*0.9, 100, 50), buttonLeft, Style)){
yaw = -75;
animator.SetBool("yawLeft", true);
leftButton = true;
}else{
animator.SetBool("yawLeft", false);
leftButton = false;
}
if(leftButton == false && rightButton == false){
yaw = 0;
animator.SetBool("yawLeft", false);
animator.SetBool("yawRight", false);
}
}
Answer by zharik86 · Sep 01, 2014 at 08:00 AM
There are some solutions of your problem. The first, it to use GUI.tooltip and GUIContent. I will write a small example:
function OnGUI(){
GUI.Label(Rect(15, 15, 32, 32), planeCrash.rings.ToString(), Style);
GUI.Button(Rect(Screen.width*0.9, Screen.height*0.9, 100, 50), GUIContent(buttonRight, "button1"), Style);
GUI.Button(Rect(Screen.width*0.1, Screen.height*0.9, 100, 50), GUIContent(buttonLeft, "button2"), Style);
var lastTooltip : String = GUI.tooltip;
if(lastTooltip == "button1") {
yaw = 75;
animator.SetBool("yawRight", true);
rightButton = true;
} else if(lastTooltip == "button2") {
yaw = -75;
animator.SetBool("yawLeft", true);
leftButton = true;
} else {
yaw = 0;
animator.SetBool("yawRight", false);
rightButton = false;
animator.SetBool("yawLeft", false);
leftButton = false;
}
}
The second way, it to use Events. The example is lower:
function OnGUI(){
GUI.Label(Rect(15, 15, 32, 32), planeCrash.rings.ToString(), Style);
var button1Rect: Rect = new Rect(Screen.width*0.9, Screen.height*0.9, 100, 50);
var button2Rect: Rect = new Rect(Screen.width*0.1, Screen.height*0.9, 100, 50);
GUI.Button(button1Rect, buttonRight, Style);
GUI.Button(button2Rect, buttonLeft, Style);
if(Event.current.type == EventType.Repaint) {
if (button1Rect.Contains(Evenr.current.mousePosition)) {
yaw = 75;
animator.SetBool("yawRight", true);
rightButton = true;
} else if(button2Rect.Contains(Evenr.current.mousePosition)) {
yaw = -75;
animator.SetBool("yawLeft", true);
leftButton = true;
} else {
yaw = 0;
animator.SetBool("yawRight", false);
rightButton = false;
animator.SetBool("yawLeft", false);
leftButton = false;
}
}
}
I hope that it will help you.
Dang, you really put a hell of a lot of effort to help me fix my problem :D thanks man!
Your answer
![](https://koobas.hobune.stream/wayback/20220613162007im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can't move GUI 1 Answer
GUI.Window Problem Option Menu 1 Answer
Help with GuiText flickering code. 1 Answer
GUI Texture Changes Position Depending On Screen Resolution? 1 Answer