修复无法点击按钮问题
This commit is contained in:
@@ -4,22 +4,19 @@ using UnityEngine.UI;
|
||||
namespace GameFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// 主菜单 UI 控制器(游戏封面)。
|
||||
/// 包含游戏标题 + 开始游戏/剧情回放/制作人员按钮。
|
||||
/// 主菜单 UI 控制器。
|
||||
/// 支持开始游戏、设置、制作人员、退出游戏。
|
||||
/// 修复:场景重载后自动重新查找 UI 引用,避免 DontDestroyOnLoad 导致的空引用问题。
|
||||
/// </summary>
|
||||
public class MainMenuUIController : MonoBehaviour
|
||||
{
|
||||
[Header("标题")]
|
||||
[SerializeField] Text titleText;
|
||||
[SerializeField] Image titleImage;
|
||||
|
||||
[Header("Buttons")]
|
||||
[SerializeField] Button startButton;
|
||||
[SerializeField] Button storyReplayButton;
|
||||
[SerializeField] Button settingsButton;
|
||||
[SerializeField] Button creditsButton;
|
||||
[SerializeField] Button quitButton;
|
||||
|
||||
[Header("Settings Panel (placeholder)")]
|
||||
[Header("Settings Panel")]
|
||||
[SerializeField] GameObject settingsPanel;
|
||||
[SerializeField] Button settingsBackButton;
|
||||
|
||||
@@ -29,23 +26,38 @@ namespace GameFramework
|
||||
|
||||
void Start()
|
||||
{
|
||||
Debug.Log("[MainMenu] creditsButton=" + (creditsButton != null ? creditsButton.name : "NULL") + " creditsPanel=" + (creditsPanel != null ? creditsPanel.name : "NULL"));
|
||||
// 自动查找(解决场景重载后引用失效问题)
|
||||
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 (storyReplayButton != null)
|
||||
storyReplayButton.onClick.AddListener(OnStoryReplayClick);
|
||||
|
||||
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);
|
||||
|
||||
@@ -56,29 +68,22 @@ namespace GameFramework
|
||||
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 OnStoryReplayClick()
|
||||
void OnSettingsClick()
|
||||
{
|
||||
// TODO: 实现剧情回放功能
|
||||
Debug.Log("[MainMenu] 剧情回放 - 待实现");
|
||||
}
|
||||
|
||||
void OnCreditsClick()
|
||||
{
|
||||
Debug.Log("[MainMenu] OnCreditsClick called! creditsPanel=" + (creditsPanel != null ? creditsPanel.name : "NULL"));
|
||||
if (creditsPanel != null)
|
||||
creditsPanel.SetActive(true);
|
||||
}
|
||||
|
||||
void OnCreditsCloseClick()
|
||||
{
|
||||
if (creditsPanel != null)
|
||||
creditsPanel.SetActive(false);
|
||||
if (settingsPanel != null)
|
||||
settingsPanel.SetActive(true);
|
||||
}
|
||||
|
||||
void OnSettingsBackClick()
|
||||
@@ -87,6 +92,25 @@ namespace GameFramework
|
||||
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
|
||||
@@ -95,15 +119,5 @@ namespace GameFramework
|
||||
Application.Quit();
|
||||
#endif
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (startButton != null) startButton.onClick.RemoveAllListeners();
|
||||
if (storyReplayButton != null) storyReplayButton.onClick.RemoveAllListeners();
|
||||
if (creditsButton != null) creditsButton.onClick.RemoveAllListeners();
|
||||
if (quitButton != null) quitButton.onClick.RemoveAllListeners();
|
||||
if (settingsBackButton != null) settingsBackButton.onClick.RemoveAllListeners();
|
||||
if (creditsCloseButton != null) creditsCloseButton.onClick.RemoveAllListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user