change button text color when window is opened

This commit is contained in:
in0finite 2020-04-19 00:41:09 +02:00
parent bea171a7e6
commit 19abce4017
2 changed files with 22 additions and 2 deletions

View file

@ -18,6 +18,7 @@ namespace SanAndreasUnity.UI
public float spaceBetweenButtons = 5f;
public Color openedWindowTextColor = Color.green;
public Color ClosedWindowTextColor => this.buttonPrefab.GetComponentInChildren<Text>().color;
static MenuEntry s_rootMenuEntry = new MenuEntry();
@ -53,6 +54,8 @@ namespace SanAndreasUnity.UI
int indexOfMenuEntry = s_rootMenuEntry.AddChild (menuEntry);
GameObject buttonGo = Instantiate(Instance.buttonPrefab);
buttonGo.name = menuEntry.name;
buttonGo.GetComponentInChildren<Text>().text = menuEntry.name;
@ -63,6 +66,19 @@ namespace SanAndreasUnity.UI
}
public static Button GetMenuEntryButton(MenuEntry entry)
{
Transform child = Instance.buttonsContainer.transform.Find(entry.name);
return child != null ? child.GetComponent<Button>() : null;
}
public static void SetEntryColor(MenuEntry entry, Color color)
{
var button = GetMenuEntryButton(entry);
if (button != null)
button.GetComponentInChildren<Text>().color = color;
}
}
}

View file

@ -19,6 +19,9 @@ namespace SanAndreasUnity.UI {
m_isOpened = value;
if (this.IsRegisteredInMainMenu)
MainMenu.SetEntryColor(m_mainMenuEntry, m_isOpened ? MainMenu.Instance.openedWindowTextColor : MainMenu.Instance.ClosedWindowTextColor);
if (m_isOpened)
{
this.OnWindowOpened ();
@ -84,6 +87,7 @@ namespace SanAndreasUnity.UI {
[SerializeField] private bool m_registerInMainMenuOnStart = false;
public bool IsRegisteredInMainMenu { get; private set; }
private MenuEntry m_mainMenuEntry;
[SerializeField] int m_sortPriorityForMainMenu = 0;
private static GameObject s_windowsContainer;
@ -296,9 +300,9 @@ namespace SanAndreasUnity.UI {
this.IsRegisteredInMainMenu = true;
MenuEntry menuEntry = new MenuEntry(){ name = this.windowName, sortPriority = m_sortPriorityForMainMenu,
m_mainMenuEntry = new MenuEntry(){ name = this.windowName, sortPriority = m_sortPriorityForMainMenu,
drawAction = () => this.OnMainMenuGUI(), clickAction = this.OnButtonClickedInMainMenu };
MainMenu.RegisterMenuEntry (menuEntry);
MainMenu.RegisterMenuEntry (m_mainMenuEntry);
}
private void OnMainMenuGUI ()