Files
ichni_Official/Assets/Scripts/UI/Settings/SettingsButton.cs
SoulliesOfficial 810d019619 剧情+对话完善
2026-07-21 15:24:42 -04:00

55 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Ichni.UI;
using UnityEngine;
using UnityEngine.UI;
namespace Ichni.Menu
{
/// <summary>
/// 可挂载在任意菜单页面上的通用设置入口。
/// <para>按钮默认从父级自动查找 <see cref="UIPageBase"/> 作为来源页面;如按钮不在页面层级之下,
/// 可在 Inspector 中手动指定 sourcePage。点击后由 SettingsUIPage 记录来源,因此返回时会回到正确页面。</para>
/// </summary>
[RequireComponent(typeof(Button))]
public class SettingsButton : MonoBehaviour
{
[SerializeField, Tooltip("打开设置页前需要隐藏的来源页面。留空时自动从父级寻找 UIPageBase。")]
private UIPageBase sourcePage;
private Button _button;
private void Awake()
{
_button = GetComponent<Button>();
if (sourcePage == null)
sourcePage = GetComponentInParent<UIPageBase>();
}
private void OnEnable()
{
_button.onClick.AddListener(OpenSettings);
}
private void OnDisable()
{
_button.onClick.RemoveListener(OpenSettings);
}
/// <summary>
/// 打开全局 SettingsUIPage。设置页引用未配置时仅输出警告保留当前页面避免点击后丢失界面。
/// </summary>
private void OpenSettings()
{
SettingsUIPage settingsPage = MenuManager.instance != null
? MenuManager.instance.settingsUIPage
: null;
if (settingsPage == null)
{
Debug.LogWarning("[SettingsButton] SettingsUIPage 未配置,无法打开设置页。", this);
return;
}
settingsPage.OpenFrom(sourcePage);
}
}
}