adapt main menu to MenuBar

This commit is contained in:
in0finite 2020-04-20 23:08:16 +02:00
parent 0aaa8b7769
commit 9993959810
4 changed files with 43 additions and 53 deletions

View file

@ -91,6 +91,7 @@ GameObject:
- component: {fileID: 9138667228417146571}
- component: {fileID: 4141083121869060371}
- component: {fileID: 7115660482297094336}
- component: {fileID: 1044688121435195929}
m_Layer: 5
m_Name: MainMenuButton
m_TagString: Untagged
@ -197,3 +198,16 @@ MonoBehaviour:
m_Calls: []
m_TypeName: UnityEngine.UI.Button+ButtonClickedEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
--- !u!114 &1044688121435195929
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9144763033384219215}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3c256f83f853fb44a8c14709ac6cf436, type: 3}
m_Name:
m_EditorClassIdentifier:
sortPriority: 0

View file

@ -684,11 +684,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4f5b556e077dc4e268ae212027bb0276, type: 3}
m_Name:
m_EditorClassIdentifier:
menuBar: {fileID: 8469116886705650871}
openedWindowTextColor: {r: 0, g: 1, b: 0, a: 1}
canvas: {fileID: 4580190521452898284}
buttonsContainer: {fileID: 8593699161208457630}
buttonPrefab: {fileID: 9144763033384219215, guid: c5e7e3e4ef714514090db34e6278e452,
type: 3}
--- !u!1 &1594478129176806
GameObject:
m_ObjectHideFlags: 0
@ -6753,6 +6751,7 @@ GameObject:
- component: {fileID: 8593699161208457630}
- component: {fileID: 8945428129181930115}
- component: {fileID: 87587368906071952}
- component: {fileID: 8469116886705650871}
m_Layer: 5
m_Name: ButtonsContainer
m_TagString: Untagged
@ -6810,6 +6809,21 @@ MonoBehaviour:
m_ChildForceExpandHeight: 1
m_ChildControlWidth: 1
m_ChildControlHeight: 0
--- !u!114 &8469116886705650871
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7440295692224049306}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3009b992d8a5e69499f649956b583a0c, type: 3}
m_Name:
m_EditorClassIdentifier:
buttonsContainer: {fileID: 8593699161208457630}
buttonPrefab: {fileID: 9144763033384219215, guid: c5e7e3e4ef714514090db34e6278e452,
type: 3}
--- !u!1 &7884037108542363516
GameObject:
m_ObjectHideFlags: 0

View file

@ -1,7 +1,7 @@
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Behaviours;
using UnityEngine.UI;
using SanAndreasUnity.Utilities;
namespace SanAndreasUnity.UI
{
@ -10,14 +10,12 @@ namespace SanAndreasUnity.UI
public static MainMenu Instance { get; private set; }
public Color openedWindowTextColor = Color.green;
public Color ClosedWindowTextColor => this.buttonPrefab.GetComponentInChildren<Text>().color;
public MenuBar menuBar;
static MenuEntry s_rootMenuEntry = new MenuEntry();
public Color openedWindowTextColor = Color.green;
public Color ClosedWindowTextColor => this.menuBar.DefaultMenuEntryTextColor;
public Canvas canvas;
public RectTransform buttonsContainer;
public GameObject buttonPrefab;
@ -27,8 +25,7 @@ namespace SanAndreasUnity.UI
Instance = this;
// add Exit button
RegisterMenuEntry(new MenuEntry { name = "Exit", sortPriority = int.MaxValue,
clickAction = () => GameManager.ExitApplication() });
this.menuBar.RegisterMenuEntry("Exit", int.MaxValue, () => GameManager.ExitApplication());
}
void OnSceneChanged(SceneChangedMessage sceneChangedMessage)
@ -36,37 +33,6 @@ namespace SanAndreasUnity.UI
this.canvas.enabled = GameManager.IsInStartupScene;
}
public static void RegisterMenuEntry (MenuEntry menuEntry)
{
int indexOfMenuEntry = s_rootMenuEntry.AddChild (menuEntry);
GameObject buttonGo = Instantiate(Instance.buttonPrefab);
buttonGo.name = menuEntry.name;
buttonGo.GetComponentInChildren<Text>().text = menuEntry.name;
buttonGo.transform.SetParent(Instance.buttonsContainer.transform, false);
buttonGo.transform.SetSiblingIndex(indexOfMenuEntry);
buttonGo.GetComponent<Button>().onClick.AddListener(() => menuEntry.clickAction());
}
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,8 +19,8 @@ namespace SanAndreasUnity.UI {
m_isOpened = value;
if (this.IsRegisteredInMainMenu)
MainMenu.SetEntryColor(m_mainMenuEntry, m_isOpened ? MainMenu.Instance.openedWindowTextColor : MainMenu.Instance.ClosedWindowTextColor);
if (m_mainMenuEntry != null)
MainMenu.Instance.menuBar.SetEntryColor(m_mainMenuEntry, m_isOpened ? MainMenu.Instance.openedWindowTextColor : MainMenu.Instance.ClosedWindowTextColor);
if (m_pauseMenuEntry != null)
PauseMenu.Instance.menuBar.SetEntryColor(m_pauseMenuEntry, m_isOpened ? PauseMenu.Instance.openedWindowTextColor : PauseMenu.Instance.ClosedWindowTextColor);
@ -91,9 +91,8 @@ namespace SanAndreasUnity.UI {
private Utilities.MenuBarEntry m_pauseMenuEntry;
[SerializeField] private bool m_registerInMainMenuOnStart = false;
public bool IsRegisteredInMainMenu { get; private set; }
private MenuEntry m_mainMenuEntry;
[SerializeField] int m_sortPriorityForMainMenu = 0;
[SerializeField] private int m_sortPriorityForMainMenu = 0;
private Utilities.MenuBarEntry m_mainMenuEntry;
private static GameObject s_windowsContainer;
@ -282,14 +281,11 @@ namespace SanAndreasUnity.UI {
public void RegisterInMainMenu ()
{
if (this.IsRegisteredInMainMenu)
if (m_mainMenuEntry != null)
return;
this.IsRegisteredInMainMenu = true;
m_mainMenuEntry = new MenuEntry(){ name = this.windowName, sortPriority = m_sortPriorityForMainMenu,
clickAction = this.OnButtonClickedInMainMenu };
MainMenu.RegisterMenuEntry (m_mainMenuEntry);
m_mainMenuEntry = MainMenu.Instance.menuBar.RegisterMenuEntry (this.windowName, m_sortPriorityForMainMenu,
this.OnButtonClickedInMainMenu);
}
private void OnButtonClickedInMainMenu()