Hello!
My goal is to be able to click on a Menu UI Button (1, 2 or 3) which changes their image to be Selected and clicking on another Menu UI Button or the Close(X) Button changes their Image to be De Selected.
I was able to accomplish this using SetActive(true); and SetActive(false); but its very messy and I was wondering if there is a better way to accomplish this using less code.
Here is the code that I am using on my GameManager script.
Any advice you could offer would be greatly appreciated.
Thank you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
// ---------------------------------------------------------------------
// Variables
// ---------------------------------------------------------------------
public GameObject[] panelArray;
public PanelColour currentPanelColour;
// * VARIABLES FOR SELECTED AND DE-SELECTED STATES
public GameObject menu1Selected;
public GameObject menu1Deselected;
public GameObject menu2Selected;
public GameObject menu2Deselected;
public GameObject menu3Selected;
public GameObject menu3Deselected;
// Start is called before the first frame update
void Start()
{
SetPanelColour(PanelColour.Panel0); // initialize with a value
}
// ---------------------------------------------------------------------
// Menu Array
// ---------------------------------------------------------------------
public enum PanelColour
{
Panel0,
Panel1,
Panel2,
Panel3,
}
public void SetPanelColour(PanelColour newPanelColour)
{
panelArray[(int)currentPanelColour].SetActive(false); // deactivate the old panel
panelArray[(int)newPanelColour].SetActive(true); // activate the new one
currentPanelColour = newPanelColour; // remember the new one
}
// ---------------------------------------------------------------------
// Menu Buttons Swap Panel
// ---------------------------------------------------------------------
// Menu button 1
public void SwapPanel1()
{
SetPanelColour(PanelColour.Panel1);
// * SET MENU1 TO SELECTED SPRITE
menu1Selected.SetActive(true);
menu1Deselected.SetActive(false);
// * SET MENU2 AND MENU3 TO DESELECTED SPRITE
menu2Deselected.SetActive(true);
menu3Deselected.SetActive(true);
menu2Selected.SetActive(false);
menu3Selected.SetActive(false);
}
// Menu button 2
public void SwapPanel2()
{
SetPanelColour(PanelColour.Panel2);
// * SET MENU2 TO SELECTED SPRITE
menu2Selected.SetActive(true);
menu2Deselected.SetActive(false);
// * SET MENU1 AND MENU3 TO DESELECTED SPRITE
menu1Deselected.SetActive(true);
menu3Deselected.SetActive(true);
menu1Selected.SetActive(false);
menu3Selected.SetActive(false);
}
// Menu button 3
public void SwapPanel3()
{
SetPanelColour(PanelColour.Panel3);
// * SET MENU3 TO SELECTED SPRITE
menu3Selected.SetActive(true);
menu3Deselected.SetActive(false);
// * SET MENU1 AND MENU2 TO DESELECTED SPRITE
menu1Deselected.SetActive(true);
menu2Deselected.SetActive(true);
menu1Selected.SetActive(false);
menu2Selected.SetActive(false);
}
// Close button on Panel
public void SwapPanel0()
{
SetPanelColour(PanelColour.Panel0);
DeselectAllMenuButtons();
}
// ---------------------------------------------------------------------
// Menu Buttons Selected & Unselected States Activate On & Off
// ---------------------------------------------------------------------
public void DeselectAllMenuButtons()
{
// * SET MENU1, MENU2 & MENU3 TO DESELECTED SPRITE
menu1Deselected.SetActive(true);
menu2Deselected.SetActive(true);
menu3Deselected.SetActive(true);
menu1Selected.SetActive(false);
menu2Selected.SetActive(false);
menu3Selected.SetActive(false);
}
};