- Home /
GUI Button selected changes color and stays until another button is clicked
Like the long title says. I have a whole bunch of GUI buttons that the user can select particular items from. I have a GUI skin on these and I'm able to make 'normal' and 'hover' work correctly. Active also works, but only when the mouse is down. I'd like for what appears during 'active' to stay that way until another button is pressed.
I've read people suggesting to use GUI toggle. I tried this with no luck though.
using UnityEngine;
using System.Collections;
public class GUIsound : MonoBehaviour
{
public GameObject aav71a1;
public GameObject bv206;
public GameObject RG33;
public AudioClip track1;
public AudioClip track2;
public AudioClip track3;
public AudioClip track4;
private int buttonSelected;
public float hSbarValue;
public Vector2 scrollPosition = Vector2.zero;
public int buttonOffset;
private int buttonAreaWidth;
public GUIStyle customstyle;
public GUIStyle customstyle2;
public GUIStyle customstyle3;
public GUIStyle customstyle4;
public GUISkin skin1;
public GUISkin skin2;
public GUISkin skin3;
void Start()
{
aav71a1 = GameObject.Find ("aav71a1");
bv206 = GameObject.Find ("bv206");
RG33 = GameObject.Find ("RG33");
aav71a1.SetActiveRecursively(false);
bv206.SetActiveRecursively(false);
RG33.SetActiveRecursively(false);
buttonSelected = 0;
buttonOffset = 220;
buttonAreaWidth = 5985;
}
void OnGUI()
{
GUI.skin = skin1;
//Exit Button takes you back to main menu
if(GUI.Button (new Rect (Screen.width -190, 20,150,70), "EXIT"))
{
Application.LoadLevel("Main Menu");
}
switch(buttonSelected)
{
case 0:
GUI.Label(new Rect(Screen.width / 2 -150, Screen.height / 2 -100, 300, 20), "Select a Vehicle to Study");
break;
case 1:
GUI.Label(new Rect(Screen.width / 2 -200, Screen.height / 2 - 300, 400, 20), "aav71a1");
GUI.skin = skin2;
GUI.Label(new Rect(30, 200, 300, 20), "Country: United States");
GUI.Label(new Rect(30, 230, 300, 20), "Height: 3 Feet");
GUI.Label(new Rect(30, 260, 300, 20), "Weight: 95 LBS");
GUI.skin = skin1;
break;
case 2:
GUI.Label(new Rect(Screen.width / 2 -200, Screen.height / 2 - 300, 400, 20), "bv206");
break;
case 3:
GUI.Label(new Rect(Screen.width / 2 -200, Screen.height / 2 - 300, 400, 20), "RG33");
break;
default:
GUI.Label(new Rect(400, 200, 300, 20), "Select a Vehicle to Study");
break;
}
//keeps camera from moving while horizontally scrolling through vehicle menu.
Rect r = new Rect(- 400, Screen.height - 220, buttonAreaWidth, 350);
if (r.Contains(Event.current.mousePosition))
{
GetComponent<CameraMove>().enabled = false;
}
else
{
GetComponent<CameraMove>().enabled = true;
}
//creates horizontal scroll bar at the bottom of screen for vehicle button navigation.
scrollPosition = GUI.BeginScrollView(new Rect(10, Screen.height - 251, Screen.width -20, 250), scrollPosition, new Rect(10, Screen.height - 251, buttonAreaWidth, 0));
if(GUI.Button (new Rect (10,Screen.height - buttonOffset,180,145), "aav71a1"))
{
audio.clip = track1;
audio.loop = false;
audio.Play();
aav71a1.SetActiveRecursively(true);
bv206.SetActiveRecursively(false);
RG33.SetActiveRecursively(false);
buttonSelected = 1;
}
else if(GUI.Button (new Rect (210,Screen.height - buttonOffset,180,145), "bv206"))
{
audio.Stop();
audio.clip = track2;
audio.loop = false;
audio.Play();
aav71a1.SetActiveRecursively(false);
bv206.SetActiveRecursively(true);
RG33.SetActiveRecursively(false);
buttonSelected = 2;
}
else if(GUI.Button (new Rect (410,Screen.height - buttonOffset,180,145), "RG33"))
{
audio.Stop();
audio.clip = track3;
audio.loop = false;
audio.Play();
aav71a1.SetActiveRecursively(false);
bv206.SetActiveRecursively(false);
RG33.SetActiveRecursively(true);
buttonSelected = 3;
}
else if(GUI.Button (new Rect (610,Screen.height - buttonOffset,180,145), "UAV4"))
{
}
else if(GUI.Button (new Rect (810,Screen.height - buttonOffset,180,145), "UAV5"))
{
}
else if(GUI.Button (new Rect (Screen.width - 190,Screen.height - buttonOffset,180,145), "UAV6"))
{
}
else if(GUI.Button (new Rect (1210,Screen.height - buttonOffset,180,145), "UAV7"))
{
}
else if(GUI.Button (new Rect (1410,Screen.height - buttonOffset,180,145), "UAV8"))
{
}
else if(GUI.Button (new Rect (1610,Screen.height - buttonOffset,180,145), "UAV9"))
{
}
else if(GUI.Button (new Rect (1810,Screen.height - buttonOffset,180,145), "UAV10"))
{
}
else if(GUI.Button (new Rect (2010,Screen.height - buttonOffset,180,145), "UAV11"))
{
}
else if(GUI.Button (new Rect (2210,Screen.height - buttonOffset,180,145), "UAV12"))
{
}
else if(GUI.Button (new Rect (2410,Screen.height - buttonOffset,180,145), "UAV13"))
{
}
else if(GUI.Button (new Rect (2610,Screen.height - buttonOffset,180,145), "UAV14"))
{
}
else if(GUI.Button (new Rect (2810,Screen.height - buttonOffset,180,145), "UAV15"))
{
}
else if(GUI.Button (new Rect (3010,Screen.height - buttonOffset,180,145), "UAV16"))
{
}
else if(GUI.Button (new Rect (3210,Screen.height - buttonOffset,180,145), "UAV17"))
{
}
else if(GUI.Button (new Rect (3410,Screen.height - buttonOffset,180,145), "UAV18"))
{
}
else if(GUI.Button (new Rect (3610,Screen.height - buttonOffset,180,145), "UAV19"))
{
}
else if(GUI.Button (new Rect (3810,Screen.height - buttonOffset,180,145), "UAV20"))
{
}
else if(GUI.Button (new Rect (4010,Screen.height - buttonOffset,180,145), "UAV21"))
{
}
else if(GUI.Button (new Rect (4210,Screen.height - buttonOffset,180,145), "UAV22"))
{
}
else if(GUI.Button (new Rect (4410,Screen.height - buttonOffset,180,145), "UAV23"))
{
}
else if(GUI.Button (new Rect (4610,Screen.height - buttonOffset,180,145), "UAV24"))
{
}
else if(GUI.Button (new Rect (4810,Screen.height - buttonOffset,180,145), "UAV25"))
{
}
else if(GUI.Button (new Rect (5010,Screen.height - buttonOffset,180,145), "UAV26"))
{
}
else if(GUI.Button (new Rect (5210,Screen.height - buttonOffset,180,145), "UAV27"))
{
}
else if(GUI.Button (new Rect (5410,Screen.height - buttonOffset,180,145), "UAV28"))
{
}
else if(GUI.Button (new Rect (5610,Screen.height - buttonOffset,180,145), "UAV29"))
{
}
else if(GUI.Button (new Rect (5810,Screen.height - buttonOffset,180,145), "UAV30"))
{
}
GUI.EndScrollView();
}
}
You may have to script the button to change textures while active. I would use a variable to tell which button is down. Another choice may be using a toolbar or selection grid.
http://unity3d.com/support/documentation/ScriptReference/GUI.Toolbar.html
Answer by Berenger · Jun 07, 2012 at 07:59 PM
http://unity3d.com/support/documentation/ScriptReference/GUI.SelectionGrid.html
If you want to detect when one is clicked
int i = GUI.SelectGrid( R, selection, ... );
if( i != selection ) // Boum
Your answer
Follow this Question
Related Questions
Toggle and Skin 1 Answer
GUI Toggle Using Button Style Remain Active 1 Answer
Trouble getting GUI phone to open/close 1 Answer
Whats wrong with my GUI.Toggle? 2 Answers