- Home /
Highlighting pressed button
Ive got problem: I want to hightlight button if it has been pressed, there are meant to be many buttons and one button represents a tool, so if a button is pressed then tool is selected... I just want to highlight one button, that has been selected. If one button is selected (highlighted) then other buttons cant be selected(highlighted)
Ive added a code, that doesnt work, but will say what i meant with all this....
if(GUI.Button (Rect (10,200,unactiveb, unactiveb),touch))
{
targetScript.SetTool("touch");
GUI.Button (Rect (10,200,activeb, activeb),touch);
}
else GUI.Button (Rect (10,200,unactiveb, unactiveb),touch);
Hoping for Your answers and solutions to this problem.
Answer by efge · Mar 31, 2011 at 01:27 PM
You could use GUIStyles. You can set the background image and text colors for the states Normal, Hover, Active and Focused (among others).
I admit this option is $$anonymous$$UCH better if you have loads of buttons and want an uniform style. In my opinion my solution is nicer if you have only a few and want maximum freedom to toggle and adjust and immediately see the changes.
Answer by Joshua · Mar 31, 2011 at 01:45 PM
I just use this script on all my buttons.Just create a guiTexture and add this script. It allows me to change size when I mouse over, change color/texture when it's selected and it allow me to expose all those variables so I can freely toggle them in my editor. If you don't want to toggle color but texture replace guiTexture.color with guiTexture.texture.
var normalColor : Color; var overColor : Color; var selectedColor : Color; var selectedOverColor : Color; var size : int var overSize : int private var selected : boolean = false; function Start () { guiTexture.color = normalColor; guiTexture.pixelInset.width = size; guiTexture.pixelInset.height = size; selected = false; } function OnMouseEnter () { if(selected){ guiTexture.color = selectedOverColor; } else if(!selected){ guiTexture.color = overColor; } guiTexture.pixelInset.width = overSize; guiTexture.pixelInset.height = overSize; } function OnMouseExit () { if (!selected) { guiTexture.color = normalColor; }
else if (selected) {
guiTexture.color = selectedColor;
}
guiTexture.pixelInset.width = size;
guiTexture.pixelInset.height = size;
} function OnMouseDown () { if (!selected) { selected = true; guiTexture.color = selectedColor; }
else if (selected) {
selected = false;
guiTexture.color = normalColor;
}
}
Answer by Ray-Pendergraph · Mar 31, 2011 at 01:27 PM
Use a GUI.Toggle but style it like a button. This will utilize the skin to differentiate the selected button(s) from the unselected ones and the toggle supports the behavior for what you want. You will want to keep the state of each button in an array or dictionary(with something that identifies the tool) so you know which buttons are selected.
Answer by ChupO · May 08, 2011 at 02:15 PM
This is really easier to do... It's in JavaScript. Script:
function OnMouseEnter() { //Change text color into green renderer.material.color = Color.green; }
function OnMouseExit() { //Change text color back into white renderer.material.color = Color.white; }
This is code to change white color into green by mouse over-it and reversed.......
I hope I understood the question and helped...
Answer by RyanWittbs · Nov 14, 2013 at 01:08 AM
For anyone who needs a very quick & simple solution, check out the GUI toolbar