230 lines
7.8 KiB
C#
230 lines
7.8 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
#if UNITY_EDITOR
|
||
using UnityEditor;
|
||
using TMPro;
|
||
#else
|
||
using TMPro;
|
||
#endif
|
||
|
||
namespace MegaKoop.UI
|
||
{
|
||
/// <summary>
|
||
/// UGUI Main Menu controller (no UI Toolkit). Handles switching panels and basic settings.
|
||
/// </summary>
|
||
public class UGUIMainMenuController : MonoBehaviour
|
||
{
|
||
// Panels
|
||
[Header("Optional Panel Refs (assign to override auto-find)")]
|
||
[SerializeField] private GameObject panelMainRef;
|
||
[SerializeField] private GameObject panelSettingsRef;
|
||
[SerializeField] private GameObject panelLobbyRef;
|
||
|
||
private GameObject panelMain;
|
||
private GameObject panelSettings;
|
||
private GameObject panelLobby;
|
||
|
||
// Main buttons
|
||
private Button btnNewGame;
|
||
private Button btnContinue;
|
||
private Button btnMultiplayer;
|
||
private Button btnSettings;
|
||
private Button btnQuit;
|
||
|
||
// Settings controls
|
||
private TMP_Dropdown ddQuality;
|
||
private Toggle tgFullscreen;
|
||
private TMP_Dropdown ddResolution;
|
||
private Button btnApplySettings;
|
||
private Button btnBackFromSettings;
|
||
|
||
private void Awake()
|
||
{
|
||
// Panels (created by UGUIMenuBuilder) – najdi i když jsou neaktivní
|
||
RefreshPanelReferences();
|
||
|
||
// Buttons
|
||
btnNewGame = FindButton("Button_NewGame");
|
||
btnContinue = FindButton("Button_Continue");
|
||
btnMultiplayer = FindButton("Button_Multiplayer");
|
||
btnSettings = FindButton("Button_Settings");
|
||
btnQuit = FindButton("Button_Quit");
|
||
|
||
// Settings
|
||
ddQuality = FindDropdown("Dropdown_Quality");
|
||
tgFullscreen = FindToggle("Toggle_Fullscreen");
|
||
ddResolution = FindDropdown("Dropdown_Resolution");
|
||
btnApplySettings = FindButton("Button_ApplySettings");
|
||
btnBackFromSettings = FindButton("Button_BackFromSettings");
|
||
|
||
WireEvents();
|
||
|
||
// Ensure initial panels
|
||
ShowMainMenu();
|
||
}
|
||
|
||
// Allow UGUIMenuBuilder to inject panel refs directly
|
||
public void SetPanels(GameObject main, GameObject settings, GameObject lobby)
|
||
{
|
||
panelMainRef = main;
|
||
panelSettingsRef = settings;
|
||
panelLobbyRef = lobby;
|
||
RefreshPanelReferences();
|
||
}
|
||
|
||
private void RefreshPanelReferences()
|
||
{
|
||
panelMain = panelMainRef ? panelMainRef : (FindAnyInScene("Panel_MainMenu") ?? panelMain);
|
||
panelSettings = panelSettingsRef ? panelSettingsRef : (FindAnyInScene("Panel_Settings") ?? panelSettings);
|
||
panelLobby = panelLobbyRef ? panelLobbyRef : (FindAnyInScene("Panel_Lobby") ?? panelLobby);
|
||
}
|
||
|
||
private GameObject FindAnyInScene(string name)
|
||
{
|
||
// Najde i neaktivní objekty
|
||
var all = Resources.FindObjectsOfTypeAll<Transform>();
|
||
foreach (var t in all)
|
||
{
|
||
if (t.hideFlags != HideFlags.None) continue;
|
||
var go = t.gameObject;
|
||
if (!go.scene.IsValid() || !go.scene.isLoaded) continue;
|
||
if (go.name == name) return go;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private Button FindButton(string name)
|
||
{
|
||
var go = GameObject.Find(name);
|
||
return go ? go.GetComponent<Button>() : null;
|
||
}
|
||
private TMP_Dropdown FindDropdown(string name)
|
||
{
|
||
var go = GameObject.Find(name);
|
||
return go ? go.GetComponent<TMP_Dropdown>() : null;
|
||
}
|
||
private Toggle FindToggle(string name)
|
||
{
|
||
var go = GameObject.Find(name);
|
||
return go ? go.GetComponent<Toggle>() : null;
|
||
}
|
||
|
||
private void WireEvents()
|
||
{
|
||
if (btnNewGame) btnNewGame.onClick.AddListener(OnNewGame);
|
||
if (btnContinue) btnContinue.onClick.AddListener(OnContinue);
|
||
if (btnMultiplayer) btnMultiplayer.onClick.AddListener(OnOpenMultiplayer);
|
||
if (btnSettings) btnSettings.onClick.AddListener(OnOpenSettings);
|
||
if (btnQuit) btnQuit.onClick.AddListener(OnQuit);
|
||
|
||
if (btnApplySettings) btnApplySettings.onClick.AddListener(ApplySettings);
|
||
if (btnBackFromSettings) btnBackFromSettings.onClick.AddListener(ShowMainMenu);
|
||
}
|
||
|
||
private void ShowMainMenu()
|
||
{
|
||
if (panelMain) panelMain.SetActive(true);
|
||
if (panelSettings) panelSettings.SetActive(false);
|
||
if (panelLobby) panelLobby.SetActive(false);
|
||
}
|
||
|
||
private void OnOpenMultiplayer()
|
||
{
|
||
RefreshPanelReferences();
|
||
bool showed = false;
|
||
if (panelLobby)
|
||
{
|
||
if (panelMain) panelMain.SetActive(false);
|
||
if (panelSettings) panelSettings.SetActive(false);
|
||
panelLobby.SetActive(true);
|
||
showed = true;
|
||
}
|
||
UGUIMultiplayerLobbyController lobby;
|
||
#if UNITY_2023_1_OR_NEWER
|
||
lobby = Object.FindFirstObjectByType<UGUIMultiplayerLobbyController>();
|
||
#else
|
||
lobby = Object.FindObjectOfType<UGUIMultiplayerLobbyController>();
|
||
#endif
|
||
if (lobby && showed) lobby.QuickHost(4, true);
|
||
if (!showed)
|
||
{
|
||
Debug.LogError("[UGUIMainMenu] Panel_Lobby not found in scene. Did you run Tools > MegaKoop > Generate UGUI Main Menu?" );
|
||
if (panelMain) panelMain.SetActive(true);
|
||
}
|
||
}
|
||
|
||
private void OnOpenSettings()
|
||
{
|
||
if (panelMain) panelMain.SetActive(false);
|
||
if (panelSettings) panelSettings.SetActive(true);
|
||
if (panelLobby) panelLobby.SetActive(false);
|
||
}
|
||
|
||
private void OnNewGame()
|
||
{
|
||
// Co-op only: quick host path
|
||
UGUIMultiplayerLobbyController lobby;
|
||
#if UNITY_2023_1_OR_NEWER
|
||
lobby = Object.FindFirstObjectByType<UGUIMultiplayerLobbyController>();
|
||
#else
|
||
lobby = Object.FindObjectOfType<UGUIMultiplayerLobbyController>();
|
||
#endif
|
||
if (lobby)
|
||
{
|
||
if (panelMain) panelMain.SetActive(false);
|
||
if (panelSettings) panelSettings.SetActive(false);
|
||
if (panelLobby) panelLobby.SetActive(true);
|
||
lobby.QuickHost(4, true);
|
||
}
|
||
}
|
||
|
||
private void OnContinue()
|
||
{
|
||
// Co-op only: join path
|
||
UGUIMultiplayerLobbyController lobby;
|
||
#if UNITY_2023_1_OR_NEWER
|
||
lobby = Object.FindFirstObjectByType<UGUIMultiplayerLobbyController>();
|
||
#else
|
||
lobby = Object.FindObjectOfType<UGUIMultiplayerLobbyController>();
|
||
#endif
|
||
if (lobby)
|
||
{
|
||
if (panelMain) panelMain.SetActive(false);
|
||
if (panelSettings) panelSettings.SetActive(false);
|
||
if (panelLobby) panelLobby.SetActive(true);
|
||
lobby.ShowJoinTabPublic();
|
||
}
|
||
}
|
||
|
||
private void OnQuit()
|
||
{
|
||
#if UNITY_EDITOR
|
||
EditorApplication.isPlaying = false;
|
||
#else
|
||
Application.Quit();
|
||
#endif
|
||
}
|
||
|
||
private void ApplySettings()
|
||
{
|
||
if (ddQuality)
|
||
{
|
||
QualitySettings.SetQualityLevel(ddQuality.value, true);
|
||
}
|
||
if (tgFullscreen)
|
||
{
|
||
Screen.fullScreen = tgFullscreen.isOn;
|
||
}
|
||
if (ddResolution)
|
||
{
|
||
var opt = ddResolution.options[ddResolution.value].text; // e.g., "1920x1080"
|
||
var parts = opt.ToLower().Split('x');
|
||
if (parts.Length == 2 && int.TryParse(parts[0], out int w) && int.TryParse(parts[1], out int h))
|
||
{
|
||
Screen.SetResolution(w, h, Screen.fullScreenMode);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|