Petition-to-the-Gods-V3/Assets/Scripts/Client/ClientGlobalMenuManager.cs
2025-11-18 14:54:11 +08:00

135 lines
3.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
public class ClientGlobalMenuManager : MonoBehaviour
{
public static ClientGlobalMenuManager Instance;
[Header("UI 元件")]
public GameObject menuButton;
public GameObject menuPanel;
public Button homeButton;
public Button quitButton;
public Button closeButton;
public Button skipButton;
void Awake()
{
if (Instance == null)
{
Instance = this;
// 將整個 Canvas 設為 DontDestroyOnLoad
if (transform.parent != null)
{
DontDestroyOnLoad(transform.root.gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
else
{
Destroy(gameObject);
return;
}
}
void Start()
{
InitializeMenu();
}
void InitializeMenu()
{
// 確保選單面板一開始是隱藏的
if (menuPanel != null)
menuPanel.SetActive(false);
// 綁定按鈕事件
if (menuButton != null)
{
Button btn = menuButton.GetComponent<Button>();
if (btn != null)
{
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(OpenMenu);
}
}
if (homeButton != null)
{
homeButton.onClick.RemoveAllListeners();
homeButton.onClick.AddListener(GoToHome);
}
if (quitButton != null)
{
quitButton.onClick.RemoveAllListeners();
quitButton.onClick.AddListener(QuitApplication);
}
if (closeButton != null)
{
closeButton.onClick.RemoveAllListeners();
closeButton.onClick.AddListener(CloseMenu);
}
if (skipButton != null)
{
skipButton.onClick.RemoveAllListeners();
skipButton.onClick.AddListener(SkipButton);
}
Debug.Log("Client GlobalMenuManager 初始化完成");
}
public void OpenMenu()
{
if (menuPanel != null)
{
menuPanel.SetActive(true);
Debug.Log("選單已打開");
}
}
public void CloseMenu()
{
if (menuPanel != null)
{
menuPanel.SetActive(false);
Debug.Log("選單已關閉");
}
}
void GoToHome()
{
Debug.Log("返回主頁");
SceneManager.LoadScene("ClientHomeScene");
CloseMenu();
}
void QuitApplication()
{
Debug.Log("離開程式");
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
private void SkipButton()
{
if (ClientIntroSceneController.Instance != null)
{
StartCoroutine(ClientIntroSceneController.Instance.LoadNextScene());
}
CloseMenu();
}
}