124 lines
4.1 KiB
C#
124 lines
4.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace GameFramework
|
|
{
|
|
/// <summary>
|
|
/// 主菜单 UI 控制器。
|
|
/// 支持开始游戏、设置、制作人员、退出游戏。
|
|
/// 修复:场景重载后自动重新查找 UI 引用,避免 DontDestroyOnLoad 导致的空引用问题。
|
|
/// </summary>
|
|
public class MainMenuUIController : MonoBehaviour
|
|
{
|
|
[Header("Buttons")]
|
|
[SerializeField] Button startButton;
|
|
[SerializeField] Button settingsButton;
|
|
[SerializeField] Button creditsButton;
|
|
[SerializeField] Button quitButton;
|
|
|
|
[Header("Settings Panel")]
|
|
[SerializeField] GameObject settingsPanel;
|
|
[SerializeField] Button settingsBackButton;
|
|
|
|
[Header("Credits Panel")]
|
|
[SerializeField] GameObject creditsPanel;
|
|
[SerializeField] Button creditsCloseButton;
|
|
|
|
void Start()
|
|
{
|
|
// 自动查找(解决场景重载后引用失效问题)
|
|
if (startButton == null)
|
|
startButton = FindButton("StartButton");
|
|
if (settingsButton == null)
|
|
settingsButton = FindButton("SettingsButton");
|
|
if (creditsButton == null)
|
|
creditsButton = FindButton("CreditsButton");
|
|
if (quitButton == null)
|
|
quitButton = FindButton("QuitButton");
|
|
|
|
if (settingsPanel == null)
|
|
settingsPanel = GameObject.Find("SettingsPanel");
|
|
if (settingsBackButton == null && settingsPanel != null)
|
|
settingsBackButton = settingsPanel.GetComponentInChildren<Button>();
|
|
|
|
if (creditsPanel == null)
|
|
creditsPanel = GameObject.Find("CreditsPanel");
|
|
if (creditsCloseButton == null && creditsPanel != null)
|
|
creditsCloseButton = creditsPanel.GetComponentInChildren<Button>();
|
|
|
|
// 绑定按钮事件
|
|
if (startButton != null)
|
|
startButton.onClick.AddListener(OnStartClick);
|
|
if (settingsButton != null)
|
|
settingsButton.onClick.AddListener(OnSettingsClick);
|
|
if (creditsButton != null)
|
|
creditsButton.onClick.AddListener(OnCreditsClick);
|
|
if (quitButton != null)
|
|
quitButton.onClick.AddListener(OnQuitClick);
|
|
|
|
if (settingsBackButton != null)
|
|
settingsBackButton.onClick.AddListener(OnSettingsBackClick);
|
|
if (creditsCloseButton != null)
|
|
creditsCloseButton.onClick.AddListener(OnCreditsCloseClick);
|
|
|
|
// 确保面板初始关闭
|
|
if (settingsPanel != null)
|
|
settingsPanel.SetActive(false);
|
|
if (creditsPanel != null)
|
|
creditsPanel.SetActive(false);
|
|
}
|
|
|
|
Button FindButton(string name)
|
|
{
|
|
var btn = GameObject.Find(name);
|
|
return btn != null ? btn.GetComponent<Button>() : null;
|
|
}
|
|
|
|
void OnStartClick()
|
|
{
|
|
if (SceneLoader.Instance != null)
|
|
SceneLoader.Instance.LoadGameplayScene();
|
|
}
|
|
|
|
void OnSettingsClick()
|
|
{
|
|
if (settingsPanel != null)
|
|
settingsPanel.SetActive(true);
|
|
}
|
|
|
|
void OnSettingsBackClick()
|
|
{
|
|
if (settingsPanel != null)
|
|
settingsPanel.SetActive(false);
|
|
}
|
|
|
|
void OnCreditsClick()
|
|
{
|
|
if (creditsPanel != null)
|
|
{
|
|
creditsPanel.SetActive(true);
|
|
Debug.Log($"[MainMenu] Showing credits panel: {creditsPanel.name}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("[MainMenu] creditsPanel not found! Make sure a GameObject named 'CreditsPanel' exists in the scene.");
|
|
}
|
|
}
|
|
|
|
void OnCreditsCloseClick()
|
|
{
|
|
if (creditsPanel != null)
|
|
creditsPanel.SetActive(false);
|
|
}
|
|
|
|
void OnQuitClick()
|
|
{
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
#else
|
|
Application.Quit();
|
|
#endif
|
|
}
|
|
}
|
|
}
|