- Home /
How to Change Menu Button Rollover States with GameObjects
Hello!
My goal is to have 4 different GameObjects that are SetActive(true); or SetActive(false); based on when the button is pressed or when it is rolled over with the mouse. They are all UI Images.
I currently have this working for 3 States (Normal, Normal-Rollover and Selected) in my script that I have pasted below but I can't figure out how to add the extra 4th State being "Selected-Rollover".
What I want to add to the code:
if Normal = true then the rollover image should be Normal-Rollover
else if
Selected = true then the rollover image should be Selected-Rollover
This is my Button Controller script:
public class ButtonController : MonoBehaviour
{
public GameObject normal;
public GameObject selected;
public GameObject normalRollover;
public GameObject selectedRollover;
public void ActiveButton()
{
// Sets the button to Selected when you click on it
selected.SetActive(true);
normal.SetActive(false);
}
public void DeActiveButton()
{
// Sets the button back to Normal when you click on another Button
normal.SetActive(true);
selected.SetActive(false);
}
public void OnPointerEnter()
{
normalRollover.SetActive(true);
normal.SetActive(false);
}
public void OnPointerExit()
{
normal.SetActive(true);
normalRollover.SetActive(false);
}
This is my GameManager script
public Button[] panelHatButtons;
// NORMAL ROLLOVER
public void PanelHatPointerEnter(int buttonNum)
{ // Gets sent the int index of the button from the OnPointerEnter Event Trigger
for (int i = 0; i < panelHatButtons.Length; i++)
{ // Loop through the array buttons
if (i == buttonNum)
{
panelHatButtons[i].GetComponent<ButtonController>().OnPointerEnter();
}
}
}
public void PanelHatPointerExit(int buttonNum)
{ // Gets sent the int index of the button from the OnPointerExit Event Trigger
for (int i = 0; i < panelHatButtons.Length; i++)
{ // Loop through the array buttons
if (i == buttonNum)
{
panelHatButtons[i].GetComponent<ButtonController>().OnPointerExit();
}
}
}
// BUTTON SELECTED
public void PanelHatButtonPressed(int buttonNum)
{ // Gets sent the int index of the button from the OnClick()
for (int i = 0; i < panelHatButtons.Length; i++)
{ // Loop through the array buttons
if (i == buttonNum)
{ /
panelHatButtons[i].GetComponent<ButtonController>().ActiveButton();
}
else
{ // This is every other button
panelHatButtons[i].GetComponent<ButtonController>().DeActiveButton();
}
}
}
Any advice/tips you could offer would be super appreciated! :-) Thank you so much!
@evskii Hello! I hope you don't mind me tagging you, I thought I would since I'm using the script you helped me with a few days ago and I have just attempted to add Rollover states to it. I got stuck on this part. Thank you!
Answer by TinyTurnip1 · Nov 13, 2021 at 11:40 AM
Okay! I managed to get it to work with a lot of fiddling but I'm not sure if this is the best way to do it.
public class ButtonController : MonoBehaviour
{
// ---------------------------------------------------------------------
// VARIABLES
// ---------------------------------------------------------------------
public GameObject normal;
public GameObject normalRollover;
public GameObject selected;
public GameObject selectedRollover;
// This is only on the Panel Buttons
public GameObject panelTick;
// bools
public bool buttonIsSelected;
// ---------------------------------------------------------------------
// METHODS
// ---------------------------------------------------------------------
void Start()
{
buttonIsSelected = false;
}
// Method for Selected & Unselected State (TopBar and the Panel)
#region Button Press Active + Deactive Methods
public void ActiveButton()
{
if (buttonIsSelected == false)
{
// Sets the button to Selected when you click on it
selected.SetActive(true);
normal.SetActive(false);
normalRollover.SetActive(false);
buttonIsSelected = true;
}
else if (buttonIsSelected == true)
{
normalRollover.SetActive(false);
normal.SetActive(false);
}
}
public void DeActiveButton()
{
normal.SetActive(true);
selected.SetActive(false);
buttonIsSelected = false;
}
#endregion
// Method for Rollover State
#region Button Rollover Methods
public void OnPointerEnter()
{
if (buttonIsSelected == true)
{
selectedRollover.SetActive(true);
normal.SetActive(false);
selected.SetActive(false);
}
else
{
normalRollover.SetActive(true);
normal.SetActive(false);
}
}
public void OnPointerExit()
{
if (buttonIsSelected == true)
{
selectedRollover.SetActive(false);
selected.SetActive(true);
normal.SetActive(false);
}
else
{
normalRollover.SetActive(false);
normal.SetActive(true);
}
}
Your answer
Follow this Question
Related Questions
How to change Menu Buttons to a Selected Image and Deselected Image 2 Answers
How do I highlight multiple buttons at once? 1 Answer
unity 2017 UI Delay bug 0 Answers
UI button mobile - want a button drag to register as a click 0 Answers
UI Button Focus 2 Answers