55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|